# HG changeset patch # User Rik # Date 1321764089 28800 # Node ID 5b1ddcf5ede1593a123f142ea7d1c6294aa472de # Parent dd01f0bfd78d235d81ccd6539331d20b58dc8126 hadamard.m: Embed hadamard28 matrix as constant rather than computed value. * hadamard.m: Embed hadamard28 matrix as constant rather than computed value. diff --git a/scripts/special-matrix/hadamard.m b/scripts/special-matrix/hadamard.m --- a/scripts/special-matrix/hadamard.m +++ b/scripts/special-matrix/hadamard.m @@ -66,9 +66,9 @@ ## Find k if n = 2^k*p. k = 0; - while (n > 1 && floor (n/2) == n/2) + while (n > 1 && fix (n/2) == n/2) k++; - n = n/2; + n /= 2; endwhile ## Find base hadamard. @@ -98,15 +98,16 @@ ## Build H(2^k*n) from kron(H(2^k),H(n)). h2 = [1,1;1,-1]; while (true) - if (floor (k/2) != k/2) + if (fix (k/2) != k/2) h = kron (h2, h); endif - k = floor (k/2); + k = fix (k/2); if (k == 0) break; endif h2 = kron (h2, h2); endwhile + endfunction function h = h12 () @@ -117,7 +118,6 @@ h(2:end,2:end) = toeplitz (tu, tl); endfunction - function h = h20 () tu = [+1,-1,-1,+1,+1,+1,+1,-1,+1,-1,+1,-1,-1,-1,-1,+1,+1,-1,-1]; tl = [+1,-1,-1,+1,+1,-1,-1,-1,-1,+1,-1,+1,-1,+1,+1,+1,+1,-1,-1]; @@ -126,53 +126,50 @@ h(2:end,2:end) = fliplr (toeplitz (tu, tl)); endfunction -function h = hnormalize (h) - ## Make sure each row and column starts with +1. - h(h(:,1)==-1,:) *= -1; - h(:,h(1,:)==-1) *= -1; -endfunction - function h = h28 () ## Williamson matrix construction from ## http://www.research.att.com/~njas/hadamard/had.28.will.txt - s = ["+------++----++-+--+-+--++--"; - "-+-----+++-----+-+--+-+--++-"; - "--+-----+++---+-+-+----+--++"; - "---+-----+++---+-+-+-+--+--+"; - "----+-----+++---+-+-+++--+--"; - "-----+-----++++--+-+--++--+-"; - "------++----++-+--+-+--++--+"; - "--++++-+-------++--+++-+--+-"; - "---++++-+-----+-++--+-+-+--+"; - "+---+++--+----++-++--+-+-+--"; - "++---++---+----++-++--+-+-+-"; - "+++---+----+----++-++--+-+-+"; - "++++--------+-+--++-++--+-+-"; - "-++++--------+++--++--+--+-+"; - "-+-++-++--++--+--------++++-"; - "+-+-++--+--++--+--------++++"; - "-+-+-++--+--++--+----+---+++"; - "+-+-+-++--+--+---+---++---++"; - "++-+-+-++--+------+--+++---+"; - "-++-+-+-++--+------+-++++---"; - "+-++-+---++--+------+-++++--"; - "-++--++-+-++-+++----++------"; - "+-++--++-+-++-+++-----+-----"; - "++-++---+-+-++-+++-----+----"; - "-++-++-+-+-+-+--+++-----+---"; - "--++-++++-+-+----+++-----+--"; - "+--++-+-++-+-+----+++-----+-"; - "++--++-+-++-+-+----++------+"]; - ## Without this, the assignment of -1 will not work properly - ## (compatibility forces LHS(idx) = ANY_VAL to keep the LHS logical - ## instead of widening to a type that can represent ANY_VAL). - h = double (s == "+"); - h(!h) = -1; + ## Normalized so that each row and column starts with +1 + h = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 1 1 -1 1 -1 1 -1 + 1 -1 1 -1 -1 -1 -1 1 -1 1 1 -1 -1 1 -1 -1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 + 1 -1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 + 1 -1 -1 -1 1 -1 -1 1 1 -1 1 1 1 1 1 -1 -1 -1 1 1 1 -1 1 -1 1 -1 -1 -1 + 1 -1 -1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 -1 -1 1 1 1 1 1 1 -1 + 1 -1 -1 -1 -1 -1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 -1 1 -1 1 -1 1 + 1 -1 1 1 1 1 -1 -1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-1 1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 -1 + 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 1 -1 -1 1 -1 -1 1 1 + 1 -1 1 -1 -1 1 1 -1 1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 -1 -1 1 1 + 1 1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 1 -1 1 -1 -1 -1 1 -1 -1 + 1 -1 1 1 -1 1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 -1 -1 1 -1 -1 1 -1 -1 -1 + 1 1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 1 -1 -1 -1 1 + 1 -1 1 1 -1 -1 1 1 -1 -1 -1 1 -1 -1 1 1 -1 1 1 1 -1 -1 1 1 -1 -1 1 -1]; endfunction -%!assert(hadamard(1),1) -%!assert(hadamard(2),[1,1;1,-1]) + +%!assert (hadamard (1), 1) +%!assert (hadamard (2), [1,1;1,-1]) %!test -%! for n=[1,2,4,8,12,24,48,20,28,2^9] -%! h=hadamard(n); assert(norm(h*h'-n*eye(n)),0); -%! end +%! for n = [1,2,4,8,12,24,48,20,28,2^9] +%! h = hadamard(n); +%! assert (norm (h*h'-n*eye (n)), 0); +%! endfor + +%!error hadamard () +%!error hadamard (1,2) +%!error hadamard (5) +