]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/makelut.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / makelut.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
20 ##
21 ## lut = makelut(fun,n) returns a vector which can be used by applylut
22 ## as a lookup table. 
23 ##
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
28 ## vector).
29 ##
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.
33 ##
34 ## makelut also passes parameters @var{P1}, @var{P2}, .... to @var{fun}. 
35 ##
36 ## @seealso{applylut}
37 ## @end deftypefn
38
39 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
40
41 function lut = makelut(fun, n, varargin)
42   if (nargin < 2)
43     usage ("lut = makelut(fun, n [, ...])");
44   endif
45
46   if (n<2)
47     error ("makelut: n should be a natural number >= 2");
48   endif
49
50   nq=n^2;
51   c=2^nq;
52   lut=zeros(c,1);
53   w=reshape(2.^[nq-1:-1:0],n,n);
54   for i=0:c-1
55     idx=bitand(w,i)>0;
56     lut(i+1)= feval(fun, idx, varargin{:});
57   endfor
58 endfunction
59
60 %!demo
61 %! makelut(inline('sum(x(:))>=3','x'), 2)
62 %! % Returns '1' if one or more values
63 %! % in the input matrix are 1
64
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
70
71
72