Mercurial > hg > octave-nkf
annotate scripts/special-matrix/hadamard.m @ 12174:db1f49eaba6b
whitespace fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 26 Jan 2011 23:49:42 -0500 |
parents | c792872f8942 |
children | 4d777e05d47c |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1993-2011 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5827 | 18 ## |
19 ## Original version by Paul Kienzle distributed as free software in the | |
20 ## public domain. | |
21 | |
22 ## -*- texinfo -*- | |
23 ## @deftypefn {Function File} {} hadamard (@var{n}) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## Construct a Hadamard matrix @var{Hn} of size @var{n}-by-@var{n}. The |
6516 | 25 ## size @var{n} must be of the form @code{2 ^ @var{k} * @var{p}} in which |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @var{p} is one of 1, 12, 20 or 28. The returned matrix is normalized, |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
27 ## meaning @code{Hn(:,1) == 1} and @code{Hn(1,:) == 1}. |
5827 | 28 ## |
29 ## Some of the properties of Hadamard matrices are: | |
30 ## | |
31 ## @itemize @bullet | |
32 ## @item | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## @code{kron (@var{Hm}, @var{Hn})} is a Hadamard matrix of size |
5827 | 34 ## @var{m}-by-@var{n}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
35 ## |
5827 | 36 ## @item |
6248 | 37 ## @code{Hn * Hn' == @var{n} * eye (@var{n})}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
38 ## |
5827 | 39 ## @item |
40 ## The rows of @var{Hn} are orthogonal. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
41 ## |
5827 | 42 ## @item |
9117
efac34f78ea4
minor fix to documentation of hadamard function
Aravindh Krishnamoorthy <aravindh.k.dev@gmail.com>
parents:
9051
diff
changeset
|
43 ## @code{det (@var{A}) <= abs(det (@var{Hn}))} for all @var{A} with |
5827 | 44 ## @code{abs (@var{A} (@var{i}, @var{j})) <= 1}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
45 ## |
5827 | 46 ## @item |
47 ## Multiply any row or column by -1 and still have a Hadamard matrix. | |
48 ## @end itemize | |
49 ## | |
50 ## @end deftypefn | |
51 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
52 |
5827 | 53 ## Reference [1] contains a list of Hadamard matrices up to n=256. |
54 ## See code for h28 in hadamard.m for an example of how to extend | |
55 ## this function for additional p. | |
56 ## | |
57 ## References: | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
58 ## [1] A Library of Hadamard Matrices, N. J. A. Sloane |
5827 | 59 ## http://www.research.att.com/~njas/hadamard/ |
60 | |
61 function h = hadamard (n) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
62 |
5827 | 63 if (nargin != 1) |
64 print_usage (); | |
65 endif | |
66 | |
8507 | 67 ## Find k if n = 2^k*p. |
5827 | 68 k = 0; |
69 while (n > 1 && floor (n/2) == n/2) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
70 k++; |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
71 n = n/2; |
5827 | 72 endwhile |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
73 |
8507 | 74 ## Find base hadamard. |
75 ## Except for n=2^k, need a multiple of 4. | |
5827 | 76 if (n != 1) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
77 k -= 2; |
5827 | 78 endif |
79 | |
8507 | 80 ## Trigger error if not a multiple of 4. |
5827 | 81 if (k < 0) |
82 n =- 1; | |
83 endif | |
84 | |
85 switch (n) | |
86 case 1 | |
87 h = 1; | |
88 case 3 | |
89 h = h12 (); | |
90 case 5 | |
91 h = h20 (); | |
92 case 7 | |
93 h = hnormalize (h28 ()); | |
94 otherwise | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
95 error ("hadamard: N must be 2^k*p, for p = 1, 12, 20 or 28"); |
5827 | 96 endswitch |
97 | |
8507 | 98 ## Build H(2^k*n) from kron(H(2^k),H(n)). |
5827 | 99 h2 = [1,1;1,-1]; |
100 while (true) | |
101 if (floor (k/2) != k/2) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 h = kron (h2, h); |
5827 | 103 endif |
104 k = floor (k/2); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
105 if (k == 0) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
106 break; |
5827 | 107 endif |
108 h2 = kron (h2, h2); | |
109 endwhile | |
110 endfunction | |
111 | |
112 function h = h12 () | |
8507 | 113 tu = [-1,+1,-1,+1,+1,+1,-1,-1,-1,+1,-1]; |
114 tl = [-1,-1,+1,-1,-1,-1,+1,+1,+1,-1,+1]; | |
115 ## Note: assert (tu(2:end), tl(end:-1:2)). | |
5827 | 116 h = ones (12); |
117 h(2:end,2:end) = toeplitz (tu, tl); | |
118 endfunction | |
119 | |
120 | |
121 function h = h20 () | |
8507 | 122 tu = [+1,-1,-1,+1,+1,+1,+1,-1,+1,-1,+1,-1,-1,-1,-1,+1,+1,-1,-1]; |
123 tl = [+1,-1,-1,+1,+1,-1,-1,-1,-1,+1,-1,+1,-1,+1,+1,+1,+1,-1,-1]; | |
124 ## Note: assert (tu(2:end), tl(end:-1:2)). | |
5827 | 125 h = ones (20); |
126 h(2:end,2:end) = fliplr (toeplitz (tu, tl)); | |
127 endfunction | |
128 | |
129 function h = hnormalize (h) | |
8507 | 130 ## Make sure each row and column starts with +1. |
5827 | 131 h(h(:,1)==-1,:) *= -1; |
132 h(:,h(1,:)==-1) *= -1; | |
133 endfunction | |
134 | |
135 function h = h28 () | |
136 ## Williamson matrix construction from | |
137 ## http://www.research.att.com/~njas/hadamard/had.28.will.txt | |
8507 | 138 s = ["+------++----++-+--+-+--++--"; |
139 "-+-----+++-----+-+--+-+--++-"; | |
140 "--+-----+++---+-+-+----+--++"; | |
141 "---+-----+++---+-+-+-+--+--+"; | |
142 "----+-----+++---+-+-+++--+--"; | |
143 "-----+-----++++--+-+--++--+-"; | |
144 "------++----++-+--+-+--++--+"; | |
145 "--++++-+-------++--+++-+--+-"; | |
146 "---++++-+-----+-++--+-+-+--+"; | |
147 "+---+++--+----++-++--+-+-+--"; | |
148 "++---++---+----++-++--+-+-+-"; | |
149 "+++---+----+----++-++--+-+-+"; | |
150 "++++--------+-+--++-++--+-+-"; | |
151 "-++++--------+++--++--+--+-+"; | |
152 "-+-++-++--++--+--------++++-"; | |
153 "+-+-++--+--++--+--------++++"; | |
154 "-+-+-++--+--++--+----+---+++"; | |
155 "+-+-+-++--+--+---+---++---++"; | |
156 "++-+-+-++--+------+--+++---+"; | |
157 "-++-+-+-++--+------+-++++---"; | |
158 "+-++-+---++--+------+-++++--"; | |
159 "-++--++-+-++-+++----++------"; | |
160 "+-++--++-+-++-+++-----+-----"; | |
161 "++-++---+-+-++-+++-----+----"; | |
162 "-++-++-+-+-+-+--+++-----+---"; | |
163 "--++-++++-+-+----+++-----+--"; | |
164 "+--++-+-++-+-+----+++-----+-"; | |
165 "++--++-+-++-+-+----++------+"]; | |
5901 | 166 ## Without this, the assignment of -1 will not work properly |
167 ## (compatibility forces LHS(idx) = ANY_VAL to keep the LHS logical | |
168 ## instead of widening to a type that can represent ANY_VAL). | |
8507 | 169 h = double (s == "+"); |
5827 | 170 h(!h) = -1; |
171 endfunction | |
172 | |
173 %!assert(hadamard(1),1) | |
174 %!assert(hadamard(2),[1,1;1,-1]) | |
175 %!test | |
176 %! for n=[1,2,4,8,12,24,48,20,28,2^9] | |
177 %! h=hadamard(n); assert(norm(h*h'-n*eye(n)),0); | |
178 %! end |