Mercurial > hg > octave-avbm
changeset 13893:5b1ddcf5ede1
hadamard.m: Embed hadamard28 matrix as constant rather than computed value.
* hadamard.m: Embed hadamard28 matrix as constant rather than computed value.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 19 Nov 2011 20:41:29 -0800 |
parents | dd01f0bfd78d |
children | d4404589498c |
files | scripts/special-matrix/hadamard.m |
diffstat | 1 files changed, 46 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- 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 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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 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 <N must be 2^k*p> hadamard (5) +