1 ## Copyright (C) 2004 Josep Mones i Teixidor
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{lut} = } makelut (@var{fun},@var{n})
18 ## @deftypefnx {Function File} {@var{lut} = } makelut (@var{fun},@var{n},@var{P1},@var{P2},...)
19 ## Create a lookup table which can be used by applylut.
21 ## lut = makelut(fun,n) returns a vector which can be used by applylut
24 ## @var{fun} can be a function object as created by inline, or simply a
25 ## string which contains the name of a function. @var{fun} should accept a
26 ## @var{n}-by-@var{n} matrix whose elements are binary (0 or 1) and
27 ## returns an scalar (actually anything suitable to be included in a
30 ## makelut calls @var{fun} with all possible matrices and builds a
31 ## vector with its result, suitable to be used by applylut. The length
32 ## of this vector is 2^(@var{n}^2), so 16 for 2-by-2 and 512 for 3-by-3.
34 ## makelut also passes parameters @var{P1}, @var{P2}, .... to @var{fun}.
39 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
41 function lut = makelut(fun, n, varargin)
43 usage ("lut = makelut(fun, n [, ...])");
47 error ("makelut: n should be a natural number >= 2");
53 w=reshape(2.^[nq-1:-1:0],n,n);
56 lut(i+1)= feval(fun, idx, varargin{:});
61 %! makelut(inline('sum(x(:))>=3','x'), 2)
62 %! % Returns '1' if one or more values
63 %! % in the input matrix are 1
65 %!assert(prod(makelut(inline('sum(x(:))==2','x'),2)==makelut(inline('sum(x(:))==a*b*c*d','x','a','b','c','d'),2,2/(3*4*5),3,4,5))); # test multiple params
66 %!assert(prod(makelut(inline('x(1,1)==1','x'),2)==[zeros(2^3,1);ones(2^3,1)])==1); # test 2-by-2
67 %!assert(prod(makelut(inline('x(1,1)==1','x'),3)==[zeros(2^8,1);ones(2^8,1)])==1); # test 3-by-3
68 %!assert(prod(makelut(inline('x(1,1)==1','x'),4)==[zeros(2^15,1);ones(2^15,1)])==1); # test 4-by-4
69 %!assert(prod(makelut(inline('x(2,1)==1','x'),3)==[zeros(2^7,1);ones(2^7,1);zeros(2^7,1);ones(2^7,1)])==1); # another test for 3-by-3