Mercurial > octave
annotate scripts/special-matrix/gallery.m @ 20296:0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
* scripts/special-matrix/gallery.m (randsvd) was copied from the
Test Matrix toolbox by Nicholas J. Higham. It made use of qmult()
which was also part of that toolbox but was left behind. This qmult()
implementation is also recovered from the Test Matrix toolbox. Note
that Octave itself used to have qmult() which is part of the legacy
quaternion package (now also removed from the new quaternion package).
See cset 21904fe299c8 for when qmult() was removed from Octave.
Also fix the default value for KL and KU so that a 2 element vector
can be used as N.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Fri, 03 Jul 2015 16:18:33 +0100 |
parents | 557979395ca9 |
children | 26fc9bbb8762 |
rev | line source |
---|---|
16634 | 1 ## Copyright (C) 1989-1995 Nicholas .J. Higham |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
2 ## Copyright (C) 2013-2015 Carnë Draug |
16634 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
18 ## <http://www.gnu.org/licenses/>. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} gallery (@var{name}) | |
22 ## @deftypefnx {Function File} {} gallery (@var{name}, @var{args}) | |
23 ## Create interesting matrices for testing. | |
24 ## | |
25 ## @end deftypefn | |
26 ## | |
27 ## @deftypefn {Function File} {@var{c} =} gallery ("cauchy", @var{x}) | |
28 ## @deftypefnx {Function File} {@var{c} =} gallery ("cauchy", @var{x}, @var{y}) | |
29 ## Create a Cauchy matrix. | |
30 ## | |
31 ## @end deftypefn | |
32 ## | |
33 ## @deftypefn {Function File} {@var{c} =} gallery ("chebspec", @var{n}) | |
34 ## @deftypefnx {Function File} {@var{c} =} gallery ("chebspec", @var{n}, @var{k}) | |
35 ## Create a Chebyshev spectral differentiation matrix. | |
36 ## | |
37 ## @end deftypefn | |
38 ## | |
39 ## @deftypefn {Function File} {@var{c} =} gallery ("chebvand", @var{p}) | |
40 ## @deftypefnx {Function File} {@var{c} =} gallery ("chebvand", @var{m}, @var{p}) | |
41 ## Create a Vandermonde-like matrix for the Chebyshev polynomials. | |
42 ## | |
43 ## @end deftypefn | |
44 ## | |
45 ## @deftypefn {Function File} {@var{a} =} gallery ("chow", @var{n}) | |
46 ## @deftypefnx {Function File} {@var{a} =} gallery ("chow", @var{n}, @var{alpha}) | |
47 ## @deftypefnx {Function File} {@var{a} =} gallery ("chow", @var{n}, @var{alpha}, @var{delta}) | |
48 ## Create a Chow matrix -- a singular Toeplitz lower Hessenberg matrix. | |
49 ## | |
50 ## @end deftypefn | |
51 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
52 ## @deftypefn {Function File} {@var{c} =} gallery ("circul", @var{v}) |
16634 | 53 ## Create a circulant matrix. |
54 ## | |
55 ## @end deftypefn | |
56 ## | |
57 ## @deftypefn {Function File} {@var{a} =} gallery ("clement", @var{n}) | |
58 ## @deftypefnx {Function File} {@var{a} =} gallery ("clement", @var{n}, @var{k}) | |
59 ## Create a tridiagonal matrix with zero diagonal entries. | |
60 ## | |
61 ## @end deftypefn | |
62 ## | |
63 ## @deftypefn {Function File} {@var{c} =} gallery ("compar", @var{a}) | |
64 ## @deftypefnx {Function File} {@var{c} =} gallery ("compar", @var{a}, @var{k}) | |
65 ## Create a comparison matrix. | |
66 ## | |
67 ## @end deftypefn | |
68 ## | |
69 ## @deftypefn {Function File} {@var{a} =} gallery ("condex", @var{n}) | |
70 ## @deftypefnx {Function File} {@var{a} =} gallery ("condex", @var{n}, @var{k}) | |
71 ## @deftypefnx {Function File} {@var{a} =} gallery ("condex", @var{n}, @var{k}, @var{theta}) | |
72 ## Create a `counterexample' matrix to a condition estimator. | |
73 ## | |
74 ## @end deftypefn | |
75 ## | |
76 ## @deftypefn {Function File} {@var{a} =} gallery ("cycol", [@var{m} @var{n}]) | |
77 ## @deftypefnx {Function File} {@var{a} =} gallery ("cycol", @var{n}) | |
78 ## @deftypefnx {Function File} {@var{a} =} gallery (@dots{}, @var{k}) | |
79 ## Create a matrix whose columns repeat cyclically. | |
80 ## | |
81 ## @end deftypefn | |
82 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
83 ## @deftypefn {Function File} {[@var{c}, @var{d}, @var{e}] =} gallery ("dorr", @var{n}) |
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
84 ## @deftypefnx {Function File} {[@var{c}, @var{d}, @var{e}] =} gallery ("dorr", @var{n}, @var{theta}) |
16634 | 85 ## @deftypefnx {Function File} {@var{a} =} gallery ("dorr", @dots{}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
86 ## Create a diagonally dominant, ill-conditioned, tridiagonal matrix. |
16634 | 87 ## |
88 ## @end deftypefn | |
89 ## | |
90 ## @deftypefn {Function File} {@var{a} =} gallery ("dramadah", @var{n}) | |
91 ## @deftypefnx {Function File} {@var{a} =} gallery ("dramadah", @var{n}, @var{k}) | |
92 ## Create a (0, 1) matrix whose inverse has large integer entries. | |
93 ## | |
94 ## @end deftypefn | |
95 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
96 ## @deftypefn {Function File} {@var{a} =} gallery ("fiedler", @var{c}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
97 ## Create a symmetric @nospell{Fiedler} matrix. |
16634 | 98 ## |
99 ## @end deftypefn | |
100 ## | |
101 ## @deftypefn {Function File} {@var{a} =} gallery ("forsythe", @var{n}) | |
102 ## @deftypefnx {Function File} {@var{a} =} gallery ("forsythe", @var{n}, @var{alpha}) | |
103 ## @deftypefnx {Function File} {@var{a} =} gallery ("forsythe", @var{n}, @var{alpha}, @var{lambda}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
104 ## Create a @nospell{Forsythe} matrix (a perturbed Jordan block). |
16634 | 105 ## |
106 ## @end deftypefn | |
107 ## | |
108 ## @deftypefn {Function File} {@var{f} =} gallery ("frank", @var{n}) | |
109 ## @deftypefnx {Function File} {@var{f} =} gallery ("frank", @var{n}, @var{k}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
110 ## Create a Frank matrix (ill-conditioned eigenvalues). |
16634 | 111 ## |
112 ## @end deftypefn | |
113 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
114 ## @deftypefn {Function File} {@var{c} =} gallery ("gcdmat", @var{n}) |
16634 | 115 ## Create a greatest common divisor matrix. |
116 ## | |
117 ## @var{c} is an @var{n}-by-@var{n} matrix whose values correspond to the | |
118 ## greatest common divisor of its coordinate values, i.e., @var{c}(i,j) | |
119 ## correspond @code{gcd (i, j)}. | |
120 ## @end deftypefn | |
121 ## | |
122 ## @deftypefn {Function File} {@var{a} =} gallery ("gearmat", @var{n}) | |
123 ## @deftypefnx {Function File} {@var{a} =} gallery ("gearmat", @var{n}, @var{i}) | |
124 ## @deftypefnx {Function File} {@var{a} =} gallery ("gearmat", @var{n}, @var{i}, @var{j}) | |
125 ## Create a Gear matrix. | |
126 ## | |
127 ## @end deftypefn | |
128 ## | |
129 ## @deftypefn {Function File} {@var{g} =} gallery ("grcar", @var{n}) | |
130 ## @deftypefnx {Function File} {@var{g} =} gallery ("grcar", @var{n}, @var{k}) | |
131 ## Create a Toeplitz matrix with sensitive eigenvalues. | |
132 ## | |
133 ## @end deftypefn | |
134 ## | |
135 ## @deftypefn {Function File} {@var{a} =} gallery ("hanowa", @var{n}) | |
136 ## @deftypefnx {Function File} {@var{a} =} gallery ("hanowa", @var{n}, @var{d}) | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
137 ## Create a matrix whose eigenvalues lie on a vertical line in the complex |
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
138 ## plane. |
16634 | 139 ## |
140 ## @end deftypefn | |
141 ## | |
142 ## @deftypefn {Function File} {@var{v} =} gallery ("house", @var{x}) | |
143 ## @deftypefnx {Function File} {[@var{v}, @var{beta}] =} gallery ("house", @var{x}) | |
144 ## Create a householder matrix. | |
145 ## | |
146 ## @end deftypefn | |
147 ## | |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
148 ## @deftypefn {Function File} {@var{a} =} gallery ("integerdata", @var{imax}, [@var{M} @var{N} @dots{}], @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
149 ## @deftypefnx {Function File} {@var{a} =} gallery ("integerdata", @var{imax}, @var{M}, @var{N}, @dots{}, @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
150 ## @deftypefnx {Function File} {@var{a} =} gallery ("integerdata", [@var{imin}, @var{imax}], [@var{M} @var{N} @dots{}], @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
151 ## @deftypefnx {Function File} {@var{a} =} gallery ("integerdata", [@var{imin}, @var{imax}], @var{M}, @var{N}, @dots{}, @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
152 ## @deftypefnx {Function File} {@var{a} =} gallery ("integerdata", @dots{}, "@var{class}") |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
153 ## Create a matrix with random integers in the range [1, @var{imax}]. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
154 ## If @var{imin} is given then the integers are in the range |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
155 ## [@var{imin}, @var{imax}]. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
156 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
157 ## The second input is a matrix of dimensions describing the size of the output. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
158 ## The dimensions can also be input as comma-separated arguments. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
159 ## |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
160 ## The input @var{j} is an integer index in the range [0, 2^32-1]. The values |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
161 ## of the output matrix are always exactly the same (reproducibility) for a |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
162 ## given size input and @var{j} index. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
163 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
164 ## The final optional argument determines the class of the resulting matrix. |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
165 ## Possible values for @var{class}: @qcode{"uint8"}, @qcode{"uint16"}, |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
166 ## @qcode{"uint32"}, @qcode{"int8"}, @qcode{"int16"}, int32", @qcode{"single"}, |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
167 ## @qcode{"double"}. The default is @qcode{"double"}. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
168 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
169 ## @end deftypefn |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
170 ## |
16634 | 171 ## @deftypefn {Function File} {@var{a} =} gallery ("invhess", @var{x}) |
172 ## @deftypefnx {Function File} {@var{a} =} gallery ("invhess", @var{x}, @var{y}) | |
173 ## Create the inverse of an upper Hessenberg matrix. | |
174 ## | |
175 ## @end deftypefn | |
176 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
177 ## @deftypefn {Function File} {@var{a} =} gallery ("invol", @var{n}) |
16634 | 178 ## Create an involutory matrix. |
179 ## | |
180 ## @end deftypefn | |
181 ## | |
182 ## @deftypefn {Function File} {@var{a} =} gallery ("ipjfact", @var{n}) | |
183 ## @deftypefnx {Function File} {@var{a} =} gallery ("ipjfact", @var{n}, @var{k}) | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
184 ## Create a Hankel matrix with factorial elements. |
16634 | 185 ## |
186 ## @end deftypefn | |
187 ## | |
188 ## @deftypefn {Function File} {@var{a} =} gallery ("jordbloc", @var{n}) | |
189 ## @deftypefnx {Function File} {@var{a} =} gallery ("jordbloc", @var{n}, @var{lambda}) | |
190 ## Create a Jordan block. | |
191 ## | |
192 ## @end deftypefn | |
193 ## | |
194 ## @deftypefn {Function File} {@var{u} =} gallery ("kahan", @var{n}) | |
195 ## @deftypefnx {Function File} {@var{u} =} gallery ("kahan", @var{n}, @var{theta}) | |
196 ## @deftypefnx {Function File} {@var{u} =} gallery ("kahan", @var{n}, @var{theta}, @var{pert}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
197 ## Create a @nospell{Kahan} matrix (upper trapezoidal). |
16634 | 198 ## |
199 ## @end deftypefn | |
200 ## | |
201 ## @deftypefn {Function File} {@var{a} =} gallery ("kms", @var{n}) | |
202 ## @deftypefnx {Function File} {@var{a} =} gallery ("kms", @var{n}, @var{rho}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
203 ## Create a @nospell{Kac-Murdock-Szego} Toeplitz matrix. |
16634 | 204 ## |
205 ## @end deftypefn | |
206 ## | |
207 ## @deftypefn {Function File} {@var{b} =} gallery ("krylov", @var{a}) | |
208 ## @deftypefnx {Function File} {@var{b} =} gallery ("krylov", @var{a}, @var{x}) | |
209 ## @deftypefnx {Function File} {@var{b} =} gallery ("krylov", @var{a}, @var{x}, @var{j}) | |
210 ## Create a Krylov matrix. | |
211 ## | |
212 ## @end deftypefn | |
213 ## | |
214 ## @deftypefn {Function File} {@var{a} =} gallery ("lauchli", @var{n}) | |
215 ## @deftypefnx {Function File} {@var{a} =} gallery ("lauchli", @var{n}, @var{mu}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
216 ## Create a @nospell{Lauchli} matrix (rectangular). |
16634 | 217 ## |
218 ## @end deftypefn | |
219 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
220 ## @deftypefn {Function File} {@var{a} =} gallery ("lehmer", @var{n}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
221 ## Create a @nospell{Lehmer} matrix (symmetric positive definite). |
16634 | 222 ## |
223 ## @end deftypefn | |
224 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
225 ## @deftypefn {Function File} {@var{t} =} gallery ("lesp", @var{n}) |
16634 | 226 ## Create a tridiagonal matrix with real, sensitive eigenvalues. |
227 ## | |
228 ## @end deftypefn | |
229 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
230 ## @deftypefn {Function File} {@var{a} =} gallery ("lotkin", @var{n}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
231 ## Create a @nospell{Lotkin} matrix. |
16634 | 232 ## |
233 ## @end deftypefn | |
234 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
235 ## @deftypefn {Function File} {@var{a} =} gallery ("minij", @var{n}) |
16634 | 236 ## Create a symmetric positive definite matrix MIN(i,j). |
237 ## | |
238 ## @end deftypefn | |
239 ## | |
240 ## @deftypefn {Function File} {@var{a} =} gallery ("moler", @var{n}) | |
241 ## @deftypefnx {Function File} {@var{a} =} gallery ("moler", @var{n}, @var{alpha}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
242 ## Create a @nospell{Moler} matrix (symmetric positive definite). |
16634 | 243 ## |
244 ## @end deftypefn | |
245 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
246 ## @deftypefn {Function File} {[@var{a}, @var{t}] =} gallery ("neumann", @var{n}) |
16634 | 247 ## Create a singular matrix from the discrete Neumann problem (sparse). |
248 ## | |
249 ## @end deftypefn | |
250 ## | |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
251 ## @deftypefn {Function File} {@var{a} =} gallery ("normaldata", [@var{M} @var{N} @dots{}], @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
252 ## @deftypefnx {Function File} {@var{a} =} gallery ("normaldata", @var{M}, @var{N}, @dots{}, @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
253 ## @deftypefnx {Function File} {@var{a} =} gallery ("normaldata", @dots{}, "@var{class}") |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
254 ## Create a matrix with random samples from the standard normal distribution |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
255 ## (mean = 0, std = 1). |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
256 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
257 ## The first input is a matrix of dimensions describing the size of the output. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
258 ## The dimensions can also be input as comma-separated arguments. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
259 ## |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
260 ## The input @var{j} is an integer index in the range [0, 2^32-1]. The values |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
261 ## of the output matrix are always exactly the same (reproducibility) for a |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
262 ## given size input and @var{j} index. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
263 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
264 ## The final optional argument determines the class of the resulting matrix. |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
265 ## Possible values for @var{class}: @qcode{"single"}, @qcode{"double"}. |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
266 ## The default is @qcode{"double"}. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
267 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
268 ## @end deftypefn |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
269 ## |
16634 | 270 ## @deftypefn {Function File} {@var{q} =} gallery ("orthog", @var{n}) |
271 ## @deftypefnx {Function File} {@var{q} =} gallery ("orthog", @var{n}, @var{k}) | |
272 ## Create orthogonal and nearly orthogonal matrices. | |
273 ## | |
274 ## @end deftypefn | |
275 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
276 ## @deftypefn {Function File} {@var{a} =} gallery ("parter", @var{n}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
277 ## Create a @nospell{Parter} matrix (a Toeplitz matrix with singular values |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
278 ## near pi). |
16634 | 279 ## |
280 ## @end deftypefn | |
281 ## | |
282 ## @deftypefn {Function File} {@var{p} =} gallery ("pei", @var{n}) | |
283 ## @deftypefnx {Function File} {@var{p} =} gallery ("pei", @var{n}, @var{alpha}) | |
284 ## Create a Pei matrix. | |
285 ## | |
286 ## @end deftypefn | |
287 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
288 ## @deftypefn {Function File} {@var{a} =} gallery ("Poisson", @var{n}) |
16634 | 289 ## Create a block tridiagonal matrix from Poisson's equation (sparse). |
290 ## | |
291 ## @end deftypefn | |
292 ## | |
293 ## @deftypefn {Function File} {@var{a} =} gallery ("prolate", @var{n}) | |
294 ## @deftypefnx {Function File} {@var{a} =} gallery ("prolate", @var{n}, @var{w}) | |
295 ## Create a prolate matrix (symmetric, ill-conditioned Toeplitz matrix). | |
296 ## | |
297 ## @end deftypefn | |
298 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
299 ## @deftypefn {Function File} {@var{h} =} gallery ("randhess", @var{x}) |
16634 | 300 ## Create a random, orthogonal upper Hessenberg matrix. |
301 ## | |
302 ## @end deftypefn | |
303 ## | |
304 ## @deftypefn {Function File} {@var{a} =} gallery ("rando", @var{n}) | |
305 ## @deftypefnx {Function File} {@var{a} =} gallery ("rando", @var{n}, @var{k}) | |
306 ## Create a random matrix with elements -1, 0 or 1. | |
307 ## | |
308 ## @end deftypefn | |
309 ## | |
310 ## @deftypefn {Function File} {@var{a} =} gallery ("randsvd", @var{n}) | |
311 ## @deftypefnx {Function File} {@var{a} =} gallery ("randsvd", @var{n}, @var{kappa}) | |
312 ## @deftypefnx {Function File} {@var{a} =} gallery ("randsvd", @var{n}, @var{kappa}, @var{mode}) | |
313 ## @deftypefnx {Function File} {@var{a} =} gallery ("randsvd", @var{n}, @var{kappa}, @var{mode}, @var{kl}) | |
314 ## @deftypefnx {Function File} {@var{a} =} gallery ("randsvd", @var{n}, @var{kappa}, @var{mode}, @var{kl}, @var{ku}) | |
315 ## Create a random matrix with pre-assigned singular values. | |
316 ## | |
317 ## @end deftypefn | |
318 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
319 ## @deftypefn {Function File} {@var{a} =} gallery ("redheff", @var{n}) |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
320 ## Create a zero and ones matrix of @nospell{Redheffer} associated with the |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
321 ## Riemann hypothesis. |
16634 | 322 ## |
323 ## @end deftypefn | |
324 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
325 ## @deftypefn {Function File} {@var{a} =} gallery ("riemann", @var{n}) |
16634 | 326 ## Create a matrix associated with the Riemann hypothesis. |
327 ## | |
328 ## @end deftypefn | |
329 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
330 ## @deftypefn {Function File} {@var{a} =} gallery ("ris", @var{n}) |
16634 | 331 ## Create a symmetric Hankel matrix. |
332 ## | |
333 ## @end deftypefn | |
334 ## | |
335 ## @deftypefn {Function File} {@var{a} =} gallery ("smoke", @var{n}) | |
336 ## @deftypefnx {Function File} {@var{a} =} gallery ("smoke", @var{n}, @var{k}) | |
337 ## Create a complex matrix, with a `smoke ring' pseudospectrum. | |
338 ## | |
339 ## @end deftypefn | |
340 ## | |
341 ## @deftypefn {Function File} {@var{t} =} gallery ("toeppd", @var{n}) | |
342 ## @deftypefnx {Function File} {@var{t} =} gallery ("toeppd", @var{n}, @var{m}) | |
343 ## @deftypefnx {Function File} {@var{t} =} gallery ("toeppd", @var{n}, @var{m}, @var{w}) | |
344 ## @deftypefnx {Function File} {@var{t} =} gallery ("toeppd", @var{n}, @var{m}, @var{w}, @var{theta}) | |
345 ## Create a symmetric positive definite Toeplitz matrix. | |
346 ## | |
347 ## @end deftypefn | |
348 ## | |
349 ## @deftypefn {Function File} {@var{p} =} gallery ("toeppen", @var{n}) | |
350 ## @deftypefnx {Function File} {@var{p} =} gallery ("toeppen", @var{n}, @var{a}) | |
351 ## @deftypefnx {Function File} {@var{p} =} gallery ("toeppen", @var{n}, @var{a}, @var{b}) | |
352 ## @deftypefnx {Function File} {@var{p} =} gallery ("toeppen", @var{n}, @var{a}, @var{b}, @var{c}) | |
353 ## @deftypefnx {Function File} {@var{p} =} gallery ("toeppen", @var{n}, @var{a}, @var{b}, @var{c}, @var{d}) | |
354 ## @deftypefnx {Function File} {@var{p} =} gallery ("toeppen", @var{n}, @var{a}, @var{b}, @var{c}, @var{d}, @var{e}) | |
355 ## Create a pentadiagonal Toeplitz matrix (sparse). | |
356 ## | |
357 ## @end deftypefn | |
358 ## | |
359 ## @deftypefn {Function File} {@var{a} =} gallery ("tridiag", @var{x}, @var{y}, @var{z}) | |
360 ## @deftypefnx {Function File} {@var{a} =} gallery ("tridiag", @var{n}) | |
361 ## @deftypefnx {Function File} {@var{a} =} gallery ("tridiag", @var{n}, @var{c}, @var{d}, @var{e}) | |
362 ## Create a tridiagonal matrix (sparse). | |
363 ## | |
364 ## @end deftypefn | |
365 ## | |
366 ## @deftypefn {Function File} {@var{t} =} gallery ("triw", @var{n}) | |
367 ## @deftypefnx {Function File} {@var{t} =} gallery ("triw", @var{n}, @var{alpha}) | |
368 ## @deftypefnx {Function File} {@var{t} =} gallery ("triw", @var{n}, @var{alpha}, @var{k}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
369 ## Create an upper triangular matrix discussed by |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
370 ## @nospell{Kahan, Golub, and Wilkinson}. |
16634 | 371 ## |
372 ## @end deftypefn | |
373 ## | |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
374 ## @deftypefn {Function File} {@var{a} =} gallery ("uniformdata", [@var{M} @var{N} @dots{}], @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
375 ## @deftypefnx {Function File} {@var{a} =} gallery ("uniformdata", @var{M}, @var{N}, @dots{}, @var{j}) |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
376 ## @deftypefnx {Function File} {@var{a} =} gallery ("uniformdata", @dots{}, "@var{class}") |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
377 ## Create a matrix with random samples from the standard uniform distribution |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
378 ## (range [0,1]). |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
379 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
380 ## The first input is a matrix of dimensions describing the size of the output. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
381 ## The dimensions can also be input as comma-separated arguments. |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
382 ## |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
383 ## The input @var{j} is an integer index in the range [0, 2^32-1]. The values |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
384 ## of the output matrix are always exactly the same (reproducibility) for a |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19944
diff
changeset
|
385 ## given size input and @var{j} index. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
386 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
387 ## The final optional argument determines the class of the resulting matrix. |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
388 ## Possible values for @var{class}: @qcode{"single"}, @qcode{"double"}. |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
389 ## The default is @qcode{"double"}. |
16982
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
390 ## |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
391 ## @end deftypefn |
badc46a0a230
doc: Document 'uniformdata', 'normaldata', 'integerdata' matrices in gallery.m.
Rik <rik@octave.org>
parents:
16980
diff
changeset
|
392 ## |
16634 | 393 ## @deftypefn {Function File} {@var{a} =} gallery ("wathen", @var{nx}, @var{ny}) |
394 ## @deftypefnx {Function File} {@var{a} =} gallery ("wathen", @var{nx}, @var{ny}, @var{k}) | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18846
diff
changeset
|
395 ## Create the @nospell{Wathen} matrix. |
16634 | 396 ## |
397 ## @end deftypefn | |
398 ## | |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
399 ## @deftypefn {Function File} {[@var{a}, @var{b}] =} gallery ("wilk", @var{n}) |
16634 | 400 ## Create various specific matrices devised/discussed by Wilkinson. |
401 ## | |
402 ## @end deftypefn | |
403 | |
404 ## Code for most of the individual matrices (except binomial, gcdmat, | |
405 ## integerdata, leslie, normaldata, randcolu, randcorr, randjorth, sampling, | |
406 ## uniformdata) by Nicholas .J. Higham <Nicholas.J.Higham@manchester.ac.uk> | |
407 ## Adapted for Octave and into single gallery function by Carnë Draug | |
408 | |
409 function [varargout] = gallery (name, varargin) | |
410 | |
411 if (nargin < 1) | |
412 print_usage (); | |
413 elseif (! ischar (name)) | |
414 error ("gallery: NAME must be a string."); | |
415 endif | |
416 | |
417 ## NOTE: there isn't a lot of input check in the individual functions | |
418 ## that actually build the functions. This is by design. The original | |
419 ## code by Higham did not perform it and was propagated to Matlab, so | |
420 ## for compatibility, we also don't make it. For example, arguments | |
421 ## that behave as switches, and in theory accepting a value of 0 or 1, | |
422 ## will use a value of 0, for any value other than 1 (only check made | |
423 ## is if the value is equal to 1). It will often also accept string | |
424 ## values instead of numeric. Only input check added was where it | |
425 ## would be causing an error anyway. | |
426 | |
427 ## we will always want to return at least 1 output | |
428 n_out = nargout; | |
429 if (n_out == 0) | |
430 n_out = 1; | |
431 endif | |
432 | |
433 switch (tolower (name)) | |
434 case "binomial" | |
435 error ("gallery: matrix %s not implemented.", name); | |
436 case "cauchy" , [varargout{1:n_out}] = cauchy (varargin{:}); | |
437 case "chebspec" , [varargout{1:n_out}] = chebspec (varargin{:}); | |
438 case "chebvand" , [varargout{1:n_out}] = chebvand (varargin{:}); | |
439 case "chow" , [varargout{1:n_out}] = chow (varargin{:}); | |
440 case "circul" , [varargout{1:n_out}] = circul (varargin{:}); | |
441 case "clement" , [varargout{1:n_out}] = clement (varargin{:}); | |
442 case "compar" , [varargout{1:n_out}] = compar (varargin{:}); | |
443 case "condex" , [varargout{1:n_out}] = condex (varargin{:}); | |
444 case "cycol" , [varargout{1:n_out}] = cycol (varargin{:}); | |
445 case "dorr" , [varargout{1:n_out}] = dorr (varargin{:}); | |
446 case "dramadah" , [varargout{1:n_out}] = dramadah (varargin{:}); | |
447 case "fiedler" , [varargout{1:n_out}] = fiedler (varargin{:}); | |
448 case "forsythe" , [varargout{1:n_out}] = forsythe (varargin{:}); | |
449 case "frank" , [varargout{1:n_out}] = frank (varargin{:}); | |
450 case "gearmat" , [varargout{1:n_out}] = gearmat (varargin{:}); | |
451 case "gcdmat" , [varargout{1:n_out}] = gcdmat (varargin{:}); | |
452 case "grcar" , [varargout{1:n_out}] = grcar (varargin{:}); | |
453 case "hanowa" , [varargout{1:n_out}] = hanowa (varargin{:}); | |
454 case "house" , [varargout{1:n_out}] = house (varargin{:}); | |
16980
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
455 case "integerdata", [varargout{1:n_out}] = integerdata (varargin{:}); |
16634 | 456 case "invhess" , [varargout{1:n_out}] = invhess (varargin{:}); |
457 case "invol" , [varargout{1:n_out}] = invol (varargin{:}); | |
458 case "ipjfact" , [varargout{1:n_out}] = ipjfact (varargin{:}); | |
459 case "jordbloc" , [varargout{1:n_out}] = jordbloc (varargin{:}); | |
460 case "kahan" , [varargout{1:n_out}] = kahan (varargin{:}); | |
461 case "kms" , [varargout{1:n_out}] = kms (varargin{:}); | |
462 case "krylov" , [varargout{1:n_out}] = krylov (varargin{:}); | |
463 case "lauchli" , [varargout{1:n_out}] = lauchli (varargin{:}); | |
464 case "lehmer" , [varargout{1:n_out}] = lehmer (varargin{:}); | |
465 case "leslie" | |
466 error ("gallery: matrix %s not implemented.", name); | |
467 case "lesp" , [varargout{1:n_out}] = lesp (varargin{:}); | |
468 case "lotkin" , [varargout{1:n_out}] = lotkin (varargin{:}); | |
469 case "minij" , [varargout{1:n_out}] = minij (varargin{:}); | |
470 case "moler" , [varargout{1:n_out}] = moler (varargin{:}); | |
471 case "neumann" , [varargout{1:n_out}] = neumann (varargin{:}); | |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
472 case "normaldata" , [varargout{1:n_out}] = normaldata (varargin{:}); |
16634 | 473 case "orthog" , [varargout{1:n_out}] = orthog (varargin{:}); |
474 case "parter" , [varargout{1:n_out}] = parter (varargin{:}); | |
475 case "pei" , [varargout{1:n_out}] = pei (varargin{:}); | |
476 case "poisson" , [varargout{1:n_out}] = poisson (varargin{:}); | |
477 case "prolate" , [varargout{1:n_out}] = prolate (varargin{:}); | |
478 case "randcolu" | |
479 error ("gallery: matrix %s not implemented.", name); | |
480 case "randcorr" | |
481 error ("gallery: matrix %s not implemented.", name); | |
482 case "randhess" , [varargout{1:n_out}] = randhess (varargin{:}); | |
483 case "randjorth" | |
484 error ("gallery: matrix %s not implemented.", name); | |
485 case "rando" , [varargout{1:n_out}] = rando (varargin{:}); | |
486 case "randsvd" , [varargout{1:n_out}] = randsvd (varargin{:}); | |
487 case "redheff" , [varargout{1:n_out}] = redheff (varargin{:}); | |
488 case "riemann" , [varargout{1:n_out}] = riemann (varargin{:}); | |
489 case "ris" , [varargout{1:n_out}] = ris (varargin{:}); | |
490 case "sampling" | |
491 error ("gallery: matrix %s not implemented.", name); | |
492 case "smoke" , [varargout{1:n_out}] = smoke (varargin{:}); | |
493 case "toeppd" , [varargout{1:n_out}] = toeppd (varargin{:}); | |
494 case "toeppen" , [varargout{1:n_out}] = toeppen (varargin{:}); | |
495 case "tridiag" , [varargout{1:n_out}] = tridiag (varargin{:}); | |
496 case "triw" , [varargout{1:n_out}] = triw (varargin{:}); | |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
497 case "uniformdata" , [varargout{1:n_out}] = uniformdata (varargin{:}); |
16634 | 498 case "wathen" , [varargout{1:n_out}] = wathen (varargin{:}); |
499 case "wilk" , [varargout{1:n_out}] = wilk (varargin{:}); | |
500 otherwise | |
501 error ("gallery: unknown matrix with NAME %s", name); | |
502 endswitch | |
503 | |
504 endfunction | |
505 | |
506 function C = cauchy (x, y) | |
507 ##CAUCHY Cauchy matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
508 ## C = CAUCHY(X, Y), where X, Y are N-vectors, is the N-by-N matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
509 ## with C(i,j) = 1/(X(i)+Y(j)). By default, Y = X. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
510 ## Special case: if X is a scalar CAUCHY(X) is the same as CAUCHY(1:X). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
511 ## Explicit formulas are known for DET(C) (which is nonzero if X and Y |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
512 ## both have distinct elements) and the elements of INV(C). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
513 ## C is totally positive if 0 < X(1) < ... < X(N) and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
514 ## 0 < Y(1) < ... < Y(N). |
16634 | 515 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
516 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
517 ## N.J. Higham, Accuracy and Stability of Numerical Algorithms, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
518 ## Society for Industrial and Applied Mathematics, Philadelphia, PA, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
519 ## USA, 1996; sec. 26.1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
520 ## D.E. Knuth, The Art of Computer Programming, Volume 1, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
521 ## Fundamental Algorithms, second edition, Addison-Wesley, Reading, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
522 ## Massachusetts, 1973, p. 36. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
523 ## E.E. Tyrtyshnikov, Cauchy-Toeplitz matrices and some applications, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
524 ## Linear Algebra and Appl., 149 (1991), pp. 1-18. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
525 ## O. Taussky and M. Marcus, Eigenvalues of finite matrices, in |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
526 ## Survey of Numerical Analysis, J. Todd, ed., McGraw-Hill, New York, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
527 ## pp. 279-313, 1962. (States the totally positive property on p. 295.) |
16634 | 528 |
529 if (nargin < 1 || nargin > 2) | |
530 error ("gallery: 1 or 2 arguments are required for cauchy matrix."); | |
531 elseif (! isnumeric (x)) | |
532 error ("gallery: X must be numeric for cauchy matrix."); | |
533 elseif (nargin == 2 && ! isnumeric (y)) | |
534 error ("gallery: Y must be numeric for cauchy matrix."); | |
535 endif | |
536 | |
537 n = numel (x); | |
538 if (isscalar (x) && fix (x) == x) | |
539 n = x; | |
540 x = 1:n; | |
541 elseif (n > 1 && isvector (x)) | |
542 ## do nothing | |
543 else | |
544 error ("gallery: X be an integer or a vector for cauchy matrix."); | |
545 endif | |
546 | |
547 if (nargin == 1) | |
548 y = x; | |
549 endif | |
550 | |
551 ## Ensure x and y are column vectors | |
552 x = x(:); | |
553 y = y(:); | |
554 if (numel (x) != numel (y)) | |
555 error ("gallery: X and Y must be vectors of same length for cauchy matrix."); | |
556 endif | |
557 | |
558 C = x * ones (1, n) + ones (n, 1) * y.'; | |
559 C = ones (n) ./ C; | |
560 endfunction | |
561 | |
562 function C = chebspec (n, k = 0) | |
563 ## CHEBSPEC Chebyshev spectral differentiation matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
564 ## C = CHEBSPEC(N, K) is a Chebyshev spectral differentiation |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
565 ## matrix of order N. K = 0 (the default) or 1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
566 ## For K = 0 (`no boundary conditions'), C is nilpotent, with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
567 ## C^N = 0 and it has the null vector ONES(N,1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
568 ## C is similar to a Jordan block of size N with eigenvalue zero. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
569 ## For K = 1, C is nonsingular and well-conditioned, and its eigenvalues |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
570 ## have negative real parts. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
571 ## For both K, the computed eigenvector matrix X from EIG is |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
572 ## ill-conditioned (MESH(REAL(X)) is interesting). |
16634 | 573 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
574 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
575 ## C. Canuto, M.Y. Hussaini, A. Quarteroni and T.A. Zang, Spectral |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
576 ## Methods in Fluid Dynamics, Springer-Verlag, Berlin, 1988; p. 69. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
577 ## L.N. Trefethen and M.R. Trummer, An instability phenomenon in |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
578 ## spectral methods, SIAM J. Numer. Anal., 24 (1987), pp. 1008-1023. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
579 ## D. Funaro, Computing the inverse of the Chebyshev collocation |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
580 ## derivative, SIAM J. Sci. Stat. Comput., 9 (1988), pp. 1050-1057. |
16634 | 581 |
582 if (nargin < 1 || nargin > 2) | |
583 error ("gallery: 1 to 2 arguments are required for chebspec matrix."); | |
584 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
585 error ("gallery: N must be an integer for chebspec matrix."); | |
586 elseif (! isnumeric (k) || ! isscalar (k)) | |
587 error ("gallery: K must be a scalar for chebspec matrix."); | |
588 endif | |
589 | |
590 ## k = 1 case obtained from k = 0 case with one bigger n. | |
591 switch (k) | |
592 case (0), # do nothing | |
593 case (1), n = n + 1; | |
594 otherwise | |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
595 error ("gallery: K should be either 0 or 1 for chebspec matrix."); |
16634 | 596 endswitch |
597 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
598 n -= 1; |
16634 | 599 C = zeros (n+1); |
600 | |
601 one = ones (n+1, 1); | |
602 x = cos ((0:n)' * (pi/n)); | |
603 d = ones (n+1, 1); | |
604 d(1) = 2; | |
605 d(n+1) = 2; | |
606 | |
607 ## eye(size(C)) on next line avoids div by zero. | |
608 C = (d * (one./d)') ./ (x*one'-one*x' + eye (size (C))); | |
609 | |
610 ## Now fix diagonal and signs. | |
611 C(1,1) = (2*n^2+1)/6; | |
612 for i = 2:n+1 | |
613 if (rem (i, 2) == 0) | |
614 C(:,i) = -C(:,i); | |
615 C(i,:) = -C(i,:); | |
616 endif | |
617 if (i < n+1) | |
618 C(i,i) = -x(i)/(2*(1-x(i)^2)); | |
619 else | |
620 C(n+1,n+1) = -C(1,1); | |
621 endif | |
622 endfor | |
623 | |
624 if (k == 1) | |
625 C = C(2:n+1,2:n+1); | |
626 endif | |
627 endfunction | |
628 | |
629 function C = chebvand (m, p) | |
630 ## CHEBVAND Vandermonde-like matrix for the Chebyshev polynomials. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
631 ## C = CHEBVAND(P), where P is a vector, produces the (primal) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
632 ## Chebyshev Vandermonde matrix based on the points P, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
633 ## i.e., C(i,j) = T_{i-1}(P(j)), where T_{i-1} is the Chebyshev |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
634 ## polynomial of degree i-1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
635 ## CHEBVAND(M,P) is a rectangular version of CHEBVAND(P) with M rows. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
636 ## Special case: If P is a scalar then P equally spaced points on |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
637 ## [0,1] are used. |
16634 | 638 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
639 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
640 ## N.J. Higham, Stability analysis of algorithms for solving confluent |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
641 ## Vandermonde-like systems, SIAM J. Matrix Anal. Appl., 11 (1990), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
642 ## pp. 23-41. |
16634 | 643 |
644 if (nargin < 1 || nargin > 2) | |
645 error ("gallery: 1 or 2 arguments are required for chebvand matrix."); | |
646 endif | |
647 | |
648 ## because the order of the arguments changes if nargin is 1 or 2 ... | |
649 | |
650 if (nargin == 1) | |
651 p = m; | |
652 endif | |
653 | |
654 n = numel (p); | |
655 if (! isnumeric (p)) | |
656 error ("gallery: P must be numeric for chebvand matrix."); | |
657 elseif (isscalar (p) && fix (p) == p) | |
658 n = p; | |
659 p = linspace (0, 1, n); | |
660 elseif (n > 1 && isvector (p)) | |
661 ## do nothing | |
662 endif | |
663 p = p(:).'; # Ensure p is a row vector. | |
664 | |
665 if (nargin == 1) | |
666 m = n; | |
667 elseif (! isnumeric (m) || ! isscalar (m)) | |
668 error ("gallery: M must be a scalar for chebvand matrix."); | |
669 endif | |
670 | |
671 C = ones (m, n); | |
672 if (m != 1) | |
673 C(2,:) = p; | |
674 ## Use Chebyshev polynomial recurrence. | |
675 for i = 3:m | |
676 C(i,:) = 2.*p.*C(i-1,:) - C(i-2,:); | |
677 endfor | |
678 endif | |
679 endfunction | |
680 | |
681 function A = chow (n, alpha = 1, delta = 0) | |
682 ## CHOW Chow matrix - a singular Toeplitz lower Hessenberg matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
683 ## A = CHOW(N, ALPHA, DELTA) is a Toeplitz lower Hessenberg matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
684 ## A = H(ALPHA) + DELTA*EYE, where H(i,j) = ALPHA^(i-j+1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
685 ## H(ALPHA) has p = FLOOR(N/2) zero eigenvalues, the rest being |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
686 ## 4*ALPHA*COS( k*PI/(N+2) )^2, k=1:N-p. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
687 ## Defaults: ALPHA = 1, DELTA = 0. |
16634 | 688 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
689 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
690 ## T.S. Chow, A class of Hessenberg matrices with known |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
691 ## eigenvalues and inverses, SIAM Review, 11 (1969), pp. 391-395. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
692 ## G. Fairweather, On the eigenvalues and eigenvectors of a class of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
693 ## Hessenberg matrices, SIAM Review, 13 (1971), pp. 220-221. |
16634 | 694 |
695 if (nargin < 1 || nargin > 3) | |
696 error ("gallery: 1 to 3 arguments are required for chow matrix."); | |
697 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
698 error ("gallery: N must be an integer for chow matrix."); | |
699 elseif (! isnumeric (alpha) || ! isscalar (alpha)) | |
700 error ("gallery: ALPHA must be a scalar for chow matrix."); | |
701 elseif (! isnumeric (delta) || ! isscalar (delta)) | |
702 error ("gallery: DELTA must be a scalar for chow matrix."); | |
703 endif | |
704 | |
705 A = toeplitz (alpha.^(1:n), [alpha 1 zeros(1, n-2)]) + delta * eye (n); | |
706 endfunction | |
707 | |
708 function C = circul (v) | |
709 ## CIRCUL Circulant matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
710 ## C = CIRCUL(V) is the circulant matrix whose first row is V. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
711 ## (A circulant matrix has the property that each row is obtained |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
712 ## from the previous one by cyclically permuting the entries one step |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
713 ## forward; it is a special Toeplitz matrix in which the diagonals |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
714 ## `wrap round'.) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
715 ## Special case: if V is a scalar then C = CIRCUL(1:V). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
716 ## The eigensystem of C (N-by-N) is known explicitly. If t is an Nth |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
717 ## root of unity, then the inner product of V with W = [1 t t^2 ... t^N] |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
718 ## is an eigenvalue of C, and W(N:-1:1) is an eigenvector of C. |
16634 | 719 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
720 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
721 ## P.J. Davis, Circulant Matrices, John Wiley, 1977. |
16634 | 722 |
723 if (nargin != 1) | |
724 error ("gallery: 1 argument is required for circul matrix."); | |
725 elseif (! isnumeric (v)) | |
726 error ("gallery: V must be numeric for circul matrix."); | |
727 endif | |
728 | |
16734
67b67fc0969a
gallery: fix bug from typo in variable name
Carnë Draug <carandraug@octave.org>
parents:
16634
diff
changeset
|
729 n = numel (v); |
67b67fc0969a
gallery: fix bug from typo in variable name
Carnë Draug <carandraug@octave.org>
parents:
16634
diff
changeset
|
730 if (isscalar (v) && fix (v) == v) |
16634 | 731 n = v; |
732 v = 1:n; | |
16734
67b67fc0969a
gallery: fix bug from typo in variable name
Carnë Draug <carandraug@octave.org>
parents:
16634
diff
changeset
|
733 elseif (n > 1 && isvector (v)) |
16634 | 734 ## do nothing |
735 else | |
736 error ("gallery: X must be a scalar or a vector for circul matrix."); | |
737 endif | |
738 | |
739 v = v(:).'; # Make sure v is a row vector | |
740 C = toeplitz ([v(1) v(n:-1:2)], v); | |
741 endfunction | |
742 | |
743 function A = clement (n, k = 0) | |
744 ## CLEMENT Clement matrix - tridiagonal with zero diagonal entries. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
745 ## CLEMENT(N, K) is a tridiagonal matrix with zero diagonal entries |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
746 ## and known eigenvalues. It is singular if N is odd. About 64 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
747 ## percent of the entries of the inverse are zero. The eigenvalues |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
748 ## are plus and minus the numbers N-1, N-3, N-5, ..., (1 or 0). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
749 ## For K = 0 (the default) the matrix is unsymmetric, while for |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
750 ## K = 1 it is symmetric. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
751 ## CLEMENT(N, 1) is diagonally similar to CLEMENT(N). |
16634 | 752 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
753 ## Similar properties hold for TRIDIAG(X,Y,Z) where Y = ZEROS(N,1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
754 ## The eigenvalues still come in plus/minus pairs but they are not |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
755 ## known explicitly. |
16634 | 756 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
757 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
758 ## P.A. Clement, A class of triple-diagonal matrices for test |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
759 ## purposes, SIAM Review, 1 (1959), pp. 50-52. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
760 ## A. Edelman and E. Kostlan, The road from Kac's matrix to Kac's |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
761 ## random polynomials. In John~G. Lewis, editor, Proceedings of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
762 ## the Fifth SIAM Conference on Applied Linear Algebra Society |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
763 ## for Industrial and Applied Mathematics, Philadelphia, 1994, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
764 ## pp. 503-507. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
765 ## O. Taussky and J. Todd, Another look at a matrix of Mark Kac, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
766 ## Linear Algebra and Appl., 150 (1991), pp. 341-360. |
16634 | 767 |
768 if (nargin < 1 || nargin > 2) | |
769 error ("gallery: 1 or 2 arguments are required for clement matrix."); | |
770 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
771 error ("gallery: N must be an integer for clement matrix."); | |
772 elseif (! isnumeric (k) || ! isscalar (k)) | |
773 error ("gallery: K must be a numeric scalar for clement matrix."); | |
774 endif | |
775 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
776 n -= 1; |
16634 | 777 x = n:-1:1; |
778 z = 1:n; | |
779 | |
780 if (k == 0) | |
781 A = diag (x, -1) + diag (z, 1); | |
782 elseif (k == 1) | |
783 y = sqrt (x.*z); | |
784 A = diag (y, -1) + diag (y, 1); | |
785 else | |
786 error ("gallery: K must have a value of 0 or 1 for clement matrix."); | |
787 endif | |
788 endfunction | |
789 | |
790 function C = compar (A, k = 0) | |
791 ## COMP Comparison matrices. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
792 ## COMP(A) is DIAG(B) - TRIL(B,-1) - TRIU(B,1), where B = ABS(A). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
793 ## COMP(A, 1) is A with each diagonal element replaced by its |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
794 ## absolute value, and each off-diagonal element replaced by minus |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
795 ## the absolute value of the largest element in absolute value in |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
796 ## its row. However, if A is triangular COMP(A, 1) is too. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
797 ## COMP(A, 0) is the same as COMP(A). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
798 ## COMP(A) is often denoted by M(A) in the literature. |
16634 | 799 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
800 ## Reference (e.g.): |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
801 ## N.J. Higham, A survey of condition number estimation for |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
802 ## triangular matrices, SIAM Review, 29 (1987), pp. 575-596. |
16634 | 803 |
804 if (nargin < 1 || nargin > 2) | |
805 error ("gallery: 1 or 2 arguments are required for compar matrix."); | |
806 elseif (! isnumeric (A) || ndims (A) != 2) | |
17199 | 807 error ("gallery: A must be a 2-D matrix for compar matrix."); |
16634 | 808 elseif (! isnumeric (k) || ! isscalar (k)) |
809 error ("gallery: K must be a numeric scalar for compar matrix."); | |
810 endif | |
811 | |
812 [m, n] = size (A); | |
813 p = min (m, n); | |
814 | |
815 if (k == 0) | |
816 ## This code uses less temporary storage than | |
817 ## the `high level' definition above. | |
818 C = -abs (A); | |
819 for j = 1:p | |
820 C(j,j) = abs (A(j,j)); | |
821 endfor | |
822 | |
823 elseif (k == 1) | |
824 C = A'; | |
825 for j = 1:p | |
826 C(k,k) = 0; | |
827 endfor | |
828 mx = max (abs (C)); | |
829 C = -mx'*ones (1, n); | |
830 for j = 1:p | |
831 C(j,j) = abs (A(j,j)); | |
832 endfor | |
833 if (all (A == tril (A))), C = tril (C); endif | |
834 if (all (A == triu (A))), C = triu (C); endif | |
835 | |
836 else | |
837 error ("gallery: K must have a value of 0 or 1 for compar matrix."); | |
838 endif | |
839 | |
840 endfunction | |
841 | |
842 function A = condex (n, k = 4, theta = 100) | |
843 ## CONDEX `Counterexamples' to matrix condition number estimators. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
844 ## CONDEX(N, K, THETA) is a `counterexample' matrix to a condition |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
845 ## estimator. It has order N and scalar parameter THETA (default 100). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
846 ## If N is not equal to the `natural' size of the matrix then |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
847 ## the matrix is padded out with an identity matrix to order N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
848 ## The matrix, its natural size, and the estimator to which it applies |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
849 ## are specified by K (default K = 4) as follows: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
850 ## K = 1: 4-by-4, LINPACK (RCOND) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
851 ## K = 2: 3-by-3, LINPACK (RCOND) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
852 ## K = 3: arbitrary, LINPACK (RCOND) (independent of THETA) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
853 ## K = 4: N >= 4, SONEST (Higham 1988) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
854 ## (Note that in practice the K = 4 matrix is not usually a |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
855 ## counterexample because of the rounding errors in forming it.) |
16634 | 856 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
857 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
858 ## A.K. Cline and R.K. Rew, A set of counter-examples to three |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
859 ## condition number estimators, SIAM J. Sci. Stat. Comput., |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
860 ## 4 (1983), pp. 602-611. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
861 ## N.J. Higham, FORTRAN codes for estimating the one-norm of a real or |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
862 ## complex matrix, with applications to condition estimation |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
863 ## (Algorithm 674), ACM Trans. Math. Soft., 14 (1988), pp. 381-396. |
16634 | 864 |
865 if (nargin < 1 || nargin > 3) | |
866 error ("gallery: 1 to 3 arguments are required for condex matrix."); | |
867 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
868 error ("gallery: N must be an integer for condex matrix."); | |
869 elseif (! isnumeric (k) || ! isscalar (k)) | |
870 error ("gallery: K must be a numeric scalar for condex matrix."); | |
871 elseif (! isnumeric (theta) || ! isscalar (theta)) | |
872 error ("gallery: THETA must be a numeric scalar for condex matrix."); | |
873 endif | |
874 | |
875 if (k == 1) # Cline and Rew (1983), Example B. | |
876 A = [1 -1 -2*theta 0 | |
877 0 1 theta -theta | |
878 0 1 1+theta -(theta+1) | |
879 0 0 0 theta]; | |
880 | |
881 elseif (k == 2) # Cline and Rew (1983), Example C. | |
882 A = [1 1-2/theta^2 -2 | |
883 0 1/theta -1/theta | |
884 0 0 1]; | |
885 | |
886 elseif (k == 3) # Cline and Rew (1983), Example D. | |
887 A = gallery ("triw", n, -1)'; | |
888 A(n,n) = -1; | |
889 | |
890 elseif (k == 4) # Higham (1988), p. 390. | |
891 x = ones (n, 3); # First col is e | |
892 x(2:n,2) = zeros (n-1, 1); # Second col is e(1) | |
893 | |
894 ## Third col is special vector b in SONEST | |
895 x(:, 3) = (-1).^[0:n-1]' .* ( 1 + [0:n-1]'/(n-1) ); | |
896 | |
897 Q = orth (x); # Q*Q' is now the orthogonal projector onto span(e(1),e,b)). | |
898 P = eye (n) - Q*Q'; | |
899 A = eye (n) + theta*P; | |
900 | |
901 else | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
902 error ("gallery: unknown estimator K '%d' for condex matrix.", k); |
16634 | 903 endif |
904 | |
905 ## Pad out with identity as necessary. | |
906 m = columns (A); | |
907 if (m < n) | |
908 for i = n:-1:m+1 | |
909 A(i,i) = 1; | |
910 endfor | |
911 endif | |
912 endfunction | |
913 | |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
914 function A = cycol (n, k = max (round (n/4), 1)) |
16634 | 915 ## CYCOL Matrix whose columns repeat cyclically. |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
916 ## A = CYCOL([M N], K) is an M-by-N matrix of the form A = B(1:M,1:N) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
917 ## where B = [C C C...] and C = RANDN(M, K). Thus A's columns repeat |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
918 ## cyclically, and A has rank at most K. K need not divide N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
919 ## K defaults to ROUND(N/4). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
920 ## CYCOL(N, K), where N is a scalar, is the same as CYCOL([N N], K). |
16634 | 921 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
922 ## This type of matrix can lead to underflow problems for Gaussian |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
923 ## elimination: see NA Digest Volume 89, Issue 3 (January 22, 1989). |
16634 | 924 |
925 if (nargin < 1 || nargin > 2) | |
926 error ("gallery: 1 or 2 arguments are required for cycol matrix."); | |
927 elseif (! isnumeric (n) || all (numel (n) != [1 2]) || fix (n) != n) | |
928 error ("gallery: N must be a 1 or 2 element integer for cycol matrix."); | |
929 elseif (! isnumeric (k) || ! isscalar (k)) | |
930 error ("gallery: K must be a scalar for cycol matrix."); | |
931 endif | |
932 | |
933 ## Parameter n specifies dimension: m-by-n | |
934 m = n(1); | |
935 n = n(end); | |
936 | |
937 A = randn (m, k); | |
938 for i = 2:ceil (n/k) | |
939 A = [A A(:,1:k)]; | |
940 endfor | |
941 A = A(:,1:n); | |
942 endfunction | |
943 | |
944 function [c, d, e] = dorr (n, theta = 0.01) | |
945 ## DORR Dorr matrix - diagonally dominant, ill conditioned, tridiagonal. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
946 ## [C, D, E] = DORR(N, THETA) returns the vectors defining a row diagonally |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
947 ## dominant, tridiagonal M-matrix that is ill conditioned for small |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
948 ## values of the parameter THETA >= 0. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
949 ## If only one output parameter is supplied then |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
950 ## C = FULL(TRIDIAG(C,D,E)), i.e., the matrix iself is returned. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
951 ## The columns of INV(C) vary greatly in norm. THETA defaults to 0.01. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
952 ## The amount of diagonal dominance is given by (ignoring rounding errors): |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
953 ## COMP(C)*ONES(N,1) = THETA*(N+1)^2 * [1 0 0 ... 0 1]'. |
16634 | 954 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
955 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
956 ## F.W. Dorr, An example of ill-conditioning in the numerical |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
957 ## solution of singular perturbation problems, Math. Comp., 25 (1971), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
958 ## pp. 271-283. |
16634 | 959 |
960 if (nargin < 1 || nargin > 2) | |
961 error ("gallery: 1 or 2 arguments are required for dorr matrix."); | |
962 elseif (! isscalar (n) || ! isnumeric (n) || fix (n) != n) | |
963 error ("gallery: N must be an integer for dorr matrix."); | |
964 elseif (! isscalar (theta) || ! isnumeric (theta)) | |
965 error ("gallery: THETA must be a numeric scalar for dorr matrix."); | |
966 endif | |
967 | |
968 c = zeros (n, 1); | |
969 e = c; | |
970 d = c; | |
971 ## All length n for convenience. Make c, e of length n-1 later. | |
972 | |
973 h = 1/(n+1); | |
974 m = floor ((n+1)/2); | |
975 term = theta/h^2; | |
976 | |
977 i = (1:m)'; | |
978 c(i) = -term * ones (m, 1); | |
979 e(i) = c(i) - (0.5-i*h)/h; | |
980 d(i) = -(c(i) + e(i)); | |
981 | |
982 i = (m+1:n)'; | |
983 e(i) = -term * ones (n-m, 1); | |
984 c(i) = e(i) + (0.5-i*h)/h; | |
985 d(i) = -(c(i) + e(i)); | |
986 | |
987 c = c(2:n); | |
988 e = e(1:n-1); | |
989 | |
990 if (nargout <= 1) | |
991 c = tridiag (c, d, e); | |
992 endif | |
993 endfunction | |
994 | |
995 function A = dramadah (n, k = 1) | |
996 ## DRAMADAH A (0,1) matrix whose inverse has large integer entries. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
997 ## An anti-Hadamard matrix A is a matrix with elements 0 or 1 for |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
998 ## which MU(A) := NORM(INV(A),'FRO') is maximal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
999 ## A = DRAMADAH(N, K) is an N-by-N (0,1) matrix for which MU(A) is |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1000 ## relatively large, although not necessarily maximal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1001 ## Available types (the default is K = 1): |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1002 ## K = 1: A is Toeplitz, with ABS(DET(A)) = 1, and MU(A) > c(1.75)^N, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1003 ## where c is a constant. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1004 ## K = 2: A is upper triangular and Toeplitz. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1005 ## The inverses of both types have integer entries. |
16634 | 1006 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1007 ## Another interesting (0,1) matrix: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1008 ## K = 3: A has maximal determinant among (0,1) lower Hessenberg |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1009 ## matrices: det(A) = the n'th Fibonacci number. A is Toeplitz. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1010 ## The eigenvalues have an interesting distribution in the complex |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1011 ## plane. |
16634 | 1012 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1013 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1014 ## R.L. Graham and N.J.A. Sloane, Anti-Hadamard matrices, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1015 ## Linear Algebra and Appl., 62 (1984), pp. 113-137. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1016 ## L. Ching, The maximum determinant of an nxn lower Hessenberg |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1017 ## (0,1) matrix, Linear Algebra and Appl., 183 (1993), pp. 147-153. |
16634 | 1018 |
1019 if (nargin < 1 || nargin > 2) | |
1020 error ("gallery: 1 to 2 arguments are required for dramadah matrix."); | |
1021 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1022 error ("gallery: N must be an integer for dramadah matrix."); | |
1023 elseif (! isnumeric (k) || ! isscalar (k)) | |
1024 error ("gallery: K must be a numeric scalar for dramadah matrix."); | |
1025 endif | |
1026 | |
1027 switch (k) | |
1028 case (1) # Toeplitz | |
1029 c = ones (n, 1); | |
1030 for i = 2:4:n | |
1031 m = min (1, n-i); | |
1032 c(i:i+m) = zeros (m+1, 1); | |
1033 endfor | |
1034 r = zeros (n, 1); | |
1035 r(1:4) = [1 1 0 1]; | |
1036 if (n < 4) | |
1037 r = r(1:n); | |
1038 endif | |
1039 A = toeplitz (c, r); | |
1040 | |
1041 case (2) # Upper triangular and Toeplitz | |
1042 c = zeros (n, 1); | |
1043 c(1) = 1; | |
1044 r = ones (n, 1); | |
1045 for i= 3:2:n | |
1046 r(i) = 0; | |
1047 endfor | |
1048 A = toeplitz (c, r); | |
1049 | |
1050 case (3) # Lower Hessenberg | |
1051 c = ones (n, 1); | |
1052 for i= 2:2:n | |
1053 c(i) = 0; | |
1054 endfor | |
1055 A = toeplitz (c, [1 1 zeros(1,n-2)]); | |
1056 | |
1057 otherwise | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
1058 error ("gallery: unknown K '%d' for dramadah matrix.", k); |
16634 | 1059 endswitch |
1060 endfunction | |
1061 | |
1062 function A = fiedler (c) | |
1063 ## FIEDLER Fiedler matrix - symmetric. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1064 ## FIEDLER(C), where C is an n-vector, is the n-by-n symmetric |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1065 ## matrix with elements ABS(C(i)-C(j)). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1066 ## Special case: if C is a scalar, then A = FIEDLER(1:C) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1067 ## (i.e. A(i,j) = ABS(i-j)). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1068 ## Properties: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1069 ## FIEDLER(N) has a dominant positive eigenvalue and all the other |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1070 ## eigenvalues are negative (Szego, 1936). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1071 ## Explicit formulas for INV(A) and DET(A) are given by Todd (1977) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1072 ## and attributed to Fiedler. These indicate that INV(A) is |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1073 ## tridiagonal except for nonzero (1,n) and (n,1) elements. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1074 ## [I think these formulas are valid only if the elements of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1075 ## C are in increasing or decreasing order---NJH.] |
16634 | 1076 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1077 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1078 ## G. Szego, Solution to problem 3705, Amer. Math. Monthly, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1079 ## 43 (1936), pp. 246-259. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1080 ## J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1081 ## Birkhauser, Basel, and Academic Press, New York, 1977, p. 159. |
16634 | 1082 |
1083 if (nargin != 1) | |
1084 error ("gallery: 1 argument is required for fiedler matrix."); | |
1085 elseif (! isnumeric (c)) | |
1086 error ("gallery: C must be numeric for fiedler matrix."); | |
1087 endif | |
1088 | |
1089 n = numel (c); | |
1090 if (isscalar (c) && fix (c) == c) | |
1091 n = c; | |
1092 c = 1:n; | |
1093 elseif (n > 1 && isvector (c)) | |
1094 ## do nothing | |
1095 else | |
1096 error ("gallery: C must be an integer or a vector for fiedler matrix."); | |
1097 endif | |
1098 c = c(:).'; # Ensure c is a row vector. | |
1099 | |
1100 A = ones (n, 1) * c; | |
1101 A = abs (A - A.'); # NB. array transpose. | |
1102 endfunction | |
1103 | |
1104 function A = forsythe (n, alpha = sqrt (eps), lambda = 0) | |
1105 ## FORSYTHE Forsythe matrix - a perturbed Jordan block. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1106 ## FORSYTHE(N, ALPHA, LAMBDA) is the N-by-N matrix equal to |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1107 ## JORDBLOC(N, LAMBDA) except it has an ALPHA in the (N,1) position. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1108 ## It has the characteristic polynomial |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1109 ## DET(A-t*EYE) = (LAMBDA-t)^N - (-1)^N ALPHA. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1110 ## ALPHA defaults to SQRT(EPS) and LAMBDA to 0. |
16634 | 1111 |
1112 if (nargin < 1 || nargin > 3) | |
1113 error ("gallery: 1 to 3 arguments are required for forsythe matrix."); | |
1114 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1115 error ("gallery: N must be an integer for forsythe matrix."); | |
1116 elseif (! isnumeric (alpha) || ! isscalar (alpha)) | |
1117 error ("gallery: ALPHA must be a numeric scalar for forsythe matrix."); | |
1118 elseif (! isnumeric (lambda) || ! isscalar (lambda)) | |
1119 error ("gallery: LAMBDA must be a numeric scalar for forsythe matrix."); | |
1120 endif | |
1121 | |
1122 A = jordbloc (n, lambda); | |
1123 A(n,1) = alpha; | |
1124 endfunction | |
1125 | |
1126 function F = frank (n, k = 0) | |
1127 ## FRANK Frank matrix---ill conditioned eigenvalues. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1128 ## F = FRANK(N, K) is the Frank matrix of order N. It is upper |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1129 ## Hessenberg with determinant 1. K = 0 is the default; if K = 1 the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1130 ## elements are reflected about the anti-diagonal (1,N)--(N,1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1131 ## F has all positive eigenvalues and they occur in reciprocal pairs |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1132 ## (so that 1 is an eigenvalue if N is odd). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1133 ## The eigenvalues of F may be obtained in terms of the zeros of the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1134 ## Hermite polynomials. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1135 ## The FLOOR(N/2) smallest eigenvalues of F are ill conditioned, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1136 ## the more so for bigger N. |
16634 | 1137 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1138 ## DET(FRANK(N)') comes out far from 1 for large N---see Frank (1958) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1139 ## and Wilkinson (1960) for discussions. |
16634 | 1140 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1141 ## This version incorporates improvements suggested by W. Kahan. |
16634 | 1142 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1143 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1144 ## W.L. Frank, Computing eigenvalues of complex matrices by determinant |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1145 ## evaluation and by methods of Danilewski and Wielandt, J. Soc. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1146 ## Indust. Appl. Math., 6 (1958), pp. 378-392 (see pp. 385, 388). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1147 ## G.H. Golub and J.H. Wilkinson, Ill-conditioned eigensystems and the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1148 ## computation of the Jordan canonical form, SIAM Review, 18 (1976), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1149 ## pp. 578-619 (Section 13). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1150 ## H. Rutishauser, On test matrices, Programmation en Mathematiques |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1151 ## Numeriques, Editions Centre Nat. Recherche Sci., Paris, 165, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1152 ## 1966, pp. 349-365. Section 9. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1153 ## J.H. Wilkinson, Error analysis of floating-point computation, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1154 ## Numer. Math., 2 (1960), pp. 319-340 (Section 8). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1155 ## J.H. Wilkinson, The Algebraic Eigenvalue Problem, Oxford University |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1156 ## Press, 1965 (pp. 92-93). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1157 ## The next two references give details of the eigensystem, as does |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1158 ## Rutishauser (see above). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1159 ## P.J. Eberlein, A note on the matrices denoted by B_n, SIAM J. Appl. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1160 ## Math., 20 (1971), pp. 87-92. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1161 ## J.M. Varah, A generalization of the Frank matrix, SIAM J. Sci. Stat. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1162 ## Comput., 7 (1986), pp. 835-839. |
16634 | 1163 |
1164 if (nargin < 1 || nargin > 2) | |
1165 error ("gallery: 1 to 2 arguments are required for frank matrix."); | |
1166 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1167 error ("gallery: N must be an integer for frank matrix."); | |
1168 elseif (! isnumeric (k) || ! isscalar (k)) | |
1169 error ("gallery: K must be a numeric scalar for frank matrix."); | |
1170 endif | |
1171 | |
1172 p = n:-1:1; | |
1173 F = triu (p(ones (n, 1), :) - diag (ones (n-1, 1), -1), -1); | |
1174 | |
1175 switch (k) | |
1176 case (0), # do nothing | |
1177 case (1), F = F(p,p)'; | |
1178 otherwise | |
1179 error ("gallery: K must have a value of 0 or 1 for frank matrix."); | |
1180 endswitch | |
1181 endfunction | |
1182 | |
1183 function c = gcdmat (n) | |
1184 if (nargin != 1) | |
1185 error ("gallery: 1 argument is required for gcdmat matrix."); | |
1186 elseif (! isscalar (n) || ! isnumeric (n) || fix (n) != n) | |
1187 error ("gallery: N must be an integer for gcdmat matrix."); | |
1188 endif | |
1189 c = gcd (repmat ((1:n)', [1 n]), repmat (1:n, [n 1])); | |
1190 endfunction | |
1191 | |
1192 function A = gearmat (n, i = n, j = -n) | |
1193 ## NOTE: this function was named gearm in the original Test Matrix Toolbox | |
1194 ## GEARMAT Gear matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1195 ## A = GEARMAT(N,I,J) is the N-by-N matrix with ones on the sub- and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1196 ## super-diagonals, SIGN(I) in the (1,ABS(I)) position, SIGN(J) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1197 ## in the (N,N+1-ABS(J)) position, and zeros everywhere else. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1198 ## Defaults: I = N, j = -N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1199 ## All eigenvalues are of the form 2*COS(a) and the eigenvectors |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1200 ## are of the form [SIN(w+a), SIN(w+2a), ..., SIN(w+Na)]. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1201 ## The values of a and w are given in the reference below. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1202 ## A can have double and triple eigenvalues and can be defective. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1203 ## GEARMAT(N) is singular. |
16634 | 1204 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1205 ## (GEAR is a Simulink function, hence GEARMAT for Gear matrix.) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1206 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1207 ## C.W. Gear, A simple set of test matrices for eigenvalue programs, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1208 ## Math. Comp., 23 (1969), pp. 119-125. |
16634 | 1209 |
1210 if (nargin < 1 || nargin > 3) | |
1211 error ("gallery: 1 to 3 arguments are required for gearmat matrix."); | |
1212 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1213 error ("gallery: N must be an integer for gearmat matrix."); | |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
1214 elseif (! isnumeric (i) || ! isscalar (i) || i == 0 || abs (i) > n) |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
17386
diff
changeset
|
1215 error ("gallery: I must be a nonzero scalar, and abs (I) <= N for gearmat matrix."); |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
1216 elseif (! isnumeric (j) || ! isscalar (j) || i == 0 || abs (j) > n) |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
17386
diff
changeset
|
1217 error ("gallery: J must be a nonzero scalar, and abs (J) <= N for gearmat matrix."); |
16634 | 1218 endif |
1219 | |
1220 A = diag (ones (n-1, 1), -1) + diag (ones (n-1, 1), 1); | |
1221 A(1, abs (i)) = sign (i); | |
1222 A(n, n+1 - abs (j)) = sign (j); | |
1223 endfunction | |
1224 | |
1225 function G = grcar (n, k = 3) | |
1226 ## GRCAR Grcar matrix - a Toeplitz matrix with sensitive eigenvalues. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1227 ## GRCAR(N, K) is an N-by-N matrix with -1s on the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1228 ## subdiagonal, 1s on the diagonal, and K superdiagonals of 1s. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1229 ## The default is K = 3. The eigenvalues of this matrix form an |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1230 ## interesting pattern in the complex plane (try PS(GRCAR(32))). |
16634 | 1231 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1232 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1233 ## J.F. Grcar, Operator coefficient methods for linear equations, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1234 ## Report SAND89-8691, Sandia National Laboratories, Albuquerque, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1235 ## New Mexico, 1989 (Appendix 2). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1236 ## N.M. Nachtigal, L. Reichel and L.N. Trefethen, A hybrid GMRES |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1237 ## algorithm for nonsymmetric linear systems, SIAM J. Matrix Anal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1238 ## Appl., 13 (1992), pp. 796-825. |
16634 | 1239 |
1240 if (nargin < 1 || nargin > 2) | |
1241 error ("gallery: 1 to 2 arguments are required for grcar matrix."); | |
1242 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1243 error ("gallery: N must be an integer for grcar matrix."); | |
1244 elseif (! isnumeric (k) || ! isscalar (k)) | |
1245 error ("gallery: K must be a numeric scalar for grcar matrix."); | |
1246 endif | |
1247 | |
1248 G = tril (triu (ones (n)), k) - diag (ones (n-1, 1), -1); | |
1249 endfunction | |
1250 | |
1251 function A = hanowa (n, d = -1) | |
1252 ## HANOWA A matrix whose eigenvalues lie on a vertical line in the complex plane. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1253 ## HANOWA(N, d) is the N-by-N block 2x2 matrix (thus N = 2M must be even) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1254 ## [d*EYE(M) -DIAG(1:M) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1255 ## DIAG(1:M) d*EYE(M)] |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1256 ## It has complex eigenvalues lambda(k) = d +/- k*i (1 <= k <= M). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1257 ## Parameter d defaults to -1. |
16634 | 1258 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1259 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1260 ## E. Hairer, S.P. Norsett and G. Wanner, Solving Ordinary |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1261 ## Differential Equations I: Nonstiff Problems, Springer-Verlag, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1262 ## Berlin, 1987. (pp. 86-87) |
16634 | 1263 |
1264 if (nargin < 1 || nargin > 2) | |
1265 error ("gallery: 1 to 2 arguments are required for hanowa matrix."); | |
1266 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1267 error ("gallery: N must be an integer for hanowa matrix."); | |
1268 elseif (rem (n, 2) != 0) | |
1269 error ("gallery: N must be even for hanowa matrix."); | |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
1270 elseif (! isnumeric (d) || ! isscalar (d)) |
16634 | 1271 error ("gallery: D must be a numeric scalar for hanowa matrix."); |
1272 endif | |
1273 | |
1274 m = n/2; | |
1275 A = [ d*eye(m) -diag(1:m) | |
1276 diag(1:m) d*eye(m) ]; | |
1277 endfunction | |
1278 | |
1279 function [v, beta] = house (x) | |
1280 ## HOUSE Householder matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1281 ## If [v, beta] = HOUSE(x) then H = EYE - beta*v*v' is a Householder |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1282 ## matrix such that Hx = -sign(x(1))*norm(x)*e_1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1283 ## NB: If x = 0 then v = 0, beta = 1 is returned. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1284 ## x can be real or complex. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1285 ## sign(x) := exp(i*arg(x)) ( = x./abs(x) when x ~= 0). |
16634 | 1286 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1287 ## Theory: (textbook references Golub & Van Loan 1989, 38-43; |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1288 ## Stewart 1973, 231-234, 262; Wilkinson 1965, 48-50). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1289 ## Hx = y: (I - beta*v*v')x = -s*e_1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1290 ## Must have |s| = norm(x), v = x+s*e_1, and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1291 ## x'y = x'Hx =(x'Hx)' real => arg(s) = arg(x(1)). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1292 ## So take s = sign(x(1))*norm(x) (which avoids cancellation). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1293 ## v'v = (x(1)+s)^2 + x(2)^2 + ... + x(n)^2 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1294 ## = 2*norm(x)*(norm(x) + |x(1)|). |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1295 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1296 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1297 ## G.H. Golub and C.F. Van Loan, Matrix Computations, second edition, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1298 ## Johns Hopkins University Press, Baltimore, Maryland, 1989. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1299 ## G.W. Stewart, Introduction to Matrix Computations, Academic Press, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1300 ## New York, 1973, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1301 ## J.H. Wilkinson, The Algebraic Eigenvalue Problem, Oxford University |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1302 ## Press, 1965. |
16634 | 1303 |
1304 if (nargin != 1) | |
1305 error ("gallery: 1 argument is required for house matrix."); | |
20295
557979395ca9
gallery: allow single element vectors for 'house'.
Carnë Draug <carandraug@octave.org>
parents:
20294
diff
changeset
|
1306 elseif (! isnumeric (x) || ! isvector (x)) |
16634 | 1307 error ("gallery: X must be a vector for house matrix."); |
1308 endif | |
1309 | |
1310 ## must be a column vector | |
1311 x = x(:); | |
1312 | |
1313 s = norm (x) * (sign (x(1)) + (x(1) == 0)); # Modification for sign (0) == 1. | |
1314 v = x; | |
1315 if (s == 0) | |
1316 ## Quit if x is the zero vector. | |
1317 beta = 1; | |
1318 else | |
1319 v(1) = v(1) + s; | |
1320 beta = 1/(s'*v(1)); # NB the conjugated s. | |
1321 ## beta = 1/(abs (s) * (abs (s) +abs(x(1)) would guarantee beta real. | |
1322 ## But beta as above can be non-real (due to rounding) only when x is complex. | |
1323 endif | |
1324 endfunction | |
1325 | |
16980
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1326 function A = integerdata (varargin) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1327 |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1328 if (nargin < 3) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1329 error ("gallery: At least 3 arguments required for integerdata matrix."); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1330 endif |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1331 |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1332 if (isnumeric (varargin{end})) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1333 jidx = varargin{end}; |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1334 svec = [varargin{:}]; |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1335 varargin(end) = []; |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1336 elseif (ischar (varargin{end})) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1337 if (nargin < 4) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1338 error (["gallery: CLASS argument requires 4 inputs " ... |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1339 "for integerdata matrix."]); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1340 endif |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1341 jidx = varargin{end-1}; |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1342 svec = [varargin{1:end-1}]; |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1343 varargin(end-1) = []; |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1344 else |
16980
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1345 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1346 "for integerdata matrix"]); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1347 endif |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1348 |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1349 if (! (isnumeric (jidx) && isscalar (jidx) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1350 && jidx == fix (jidx) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1351 && jidx >= 0 && jidx <= 0xFFFFFFFF)) |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1352 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1353 "for integerdata matrix"]); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1354 endif |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1355 |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1356 ## Save and restore random state. Initialization done so that reproducible |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1357 ## data is available from gallery depending on the jidx and size vector. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1358 randstate = rand ("state"); |
16980
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1359 unwind_protect |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1360 rand ("state", svec); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1361 A = randi (varargin{:}); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1362 unwind_protect_cleanup |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1363 rand ("state", randstate); |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1364 end_unwind_protect |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1365 |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1366 endfunction |
1909e1ed63e6
gallery.m: Add 'integerdata' matrix to function.
Rik <rik@octave.org>
parents:
16979
diff
changeset
|
1367 |
16634 | 1368 function A = invhess (x, y) |
1369 ## INVHESS Inverse of an upper Hessenberg matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1370 ## INVHESS(X, Y), where X is an N-vector and Y an N-1 vector, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1371 ## is the matrix whose lower triangle agrees with that of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1372 ## ONES(N,1)*X' and whose strict upper triangle agrees with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1373 ## that of [1 Y]*ONES(1,N). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1374 ## The matrix is nonsingular if X(1) ~= 0 and X(i+1) ~= Y(i) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1375 ## for all i, and its inverse is an upper Hessenberg matrix. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1376 ## If Y is omitted it defaults to -X(1:N-1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1377 ## Special case: if X is a scalar INVHESS(X) is the same as |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1378 ## INVHESS(1:X). |
16634 | 1379 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1380 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1381 ## F.N. Valvi and V.S. Geroyannis, Analytic inverses and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1382 ## determinants for a class of matrices, IMA Journal of Numerical |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1383 ## Analysis, 7 (1987), pp. 123-128. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1384 ## W.-L. Cao and W.J. Stewart, A note on inverses of Hessenberg-like |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1385 ## matrices, Linear Algebra and Appl., 76 (1986), pp. 233-240. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1386 ## Y. Ikebe, On inverses of Hessenberg matrices, Linear Algebra and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1387 ## Appl., 24 (1979), pp. 93-97. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1388 ## P. Rozsa, On the inverse of band matrices, Integral Equations and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1389 ## Operator Theory, 10 (1987), pp. 82-95. |
16634 | 1390 |
1391 if (nargin < 1 || nargin > 2) | |
1392 error ("gallery: 1 to 2 arguments are required for invhess matrix."); | |
1393 elseif (! isnumeric (x)) | |
1394 error ("gallery: X must be numeric for invhess matrix."); | |
1395 endif | |
1396 | |
1397 if (isscalar (x) && fix (x) == x) | |
1398 n = x; | |
1399 x = 1:n; | |
1400 elseif (! isscalar (x) && isvector (x)) | |
1401 n = numel (n); | |
1402 else | |
1403 error ("gallery: X must be an integer scalar, or a vector for invhess matrix."); | |
1404 endif | |
1405 | |
1406 if (nargin < 2) | |
1407 y = -x(1:end-1); | |
1408 elseif (! isvector (y) || numel (y) != numel (x) -1) | |
1409 error ("gallery: Y must be a vector of length -1 than X for invhess matrix."); | |
1410 endif | |
1411 | |
1412 x = x(:); | |
1413 y = y(:); | |
1414 | |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1415 ## FIXME: On next line, z = x'; A = z(ones(n,1),:) would be more efficient. |
16634 | 1416 A = ones (n, 1) * x'; |
1417 for j = 2:n | |
1418 A(1:j-1,j) = y(1:j-1); | |
1419 endfor | |
1420 endfunction | |
1421 | |
1422 function A = invol (n) | |
1423 ## INVOL An involutory matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1424 ## A = INVOL(N) is an N-by-N involutory (A*A = EYE(N)) and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1425 ## ill-conditioned matrix. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1426 ## It is a diagonally scaled version of HILB(N). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1427 ## NB: B = (EYE(N)-A)/2 and B = (EYE(N)+A)/2 are idempotent (B*B = B). |
16634 | 1428 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1429 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1430 ## A.S. Householder and J.A. Carpenter, The singular values |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1431 ## of involutory and of idempotent matrices, Numer. Math. 5 (1963), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1432 ## pp. 234-237. |
16634 | 1433 |
1434 if (nargin != 1) | |
1435 error ("gallery: 1 argument is required for invol matrix."); | |
1436 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1437 error ("gallery: N must be an integer for invol matrix."); | |
1438 endif | |
1439 | |
1440 A = hilb (n); | |
1441 | |
1442 d = -n; | |
1443 A(:, 1) = d * A(:, 1); | |
1444 | |
1445 for i = 1:n-1 | |
1446 d = -(n+i)*(n-i)*d/(i*i); | |
1447 A(i+1,:) = d * A(i+1,:); | |
1448 endfor | |
1449 endfunction | |
1450 | |
1451 function [A, detA] = ipjfact (n, k = 0) | |
1452 ## IPJFACT A Hankel matrix with factorial elements. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1453 ## A = IPJFACT(N, K) is the matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1454 ## A(i,j) = (i+j)! (K = 0, default) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1455 ## A(i,j) = 1/(i+j)! (K = 1) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1456 ## Both are Hankel matrices. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1457 ## The determinant and inverse are known explicitly. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1458 ## If a second output argument is present, d = DET(A) is returned: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1459 ## [A, d] = IPJFACT(N, K); |
16634 | 1460 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1461 ## Suggested by P. R. Graves-Morris. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1462 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1463 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1464 ## M.J.C. Gover, The explicit inverse of factorial Hankel matrices, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1465 ## Dept. of Mathematics, University of Bradford, 1993. |
16634 | 1466 |
1467 if (nargin < 1 || nargin > 2) | |
1468 error ("gallery: 1 to 2 arguments are required for ipjfact matrix."); | |
1469 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1470 error ("gallery: N must be an integer for ipjfact matrix."); | |
1471 elseif (! isnumeric (k) || ! isscalar (k)) | |
1472 error ("gallery: K must be a numeric scalar for ipjfact matrix."); | |
1473 endif | |
1474 | |
1475 c = cumprod (2:n+1); | |
1476 d = cumprod (n+1:2*n) * c(n-1); | |
1477 | |
1478 A = hankel (c, d); | |
1479 | |
1480 switch (k) | |
1481 case (0), # do nothing | |
1482 case (1), A = ones (n) ./ A; | |
1483 otherwise | |
1484 error ("gallery: K must have a value of 0 or 1 for ipjfact matrix."); | |
1485 endswitch | |
1486 | |
1487 if (nargout == 2) | |
1488 d = 1; | |
1489 | |
1490 if (k == 0) | |
1491 for i = 1:n-1 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
1492 d *= prod (1:i+1) * prod (1:n-i); |
16634 | 1493 endfor |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
1494 d *= prod (1:n+1); |
16634 | 1495 |
1496 elseif (k == 1) | |
1497 for i = 0:n-1 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
1498 d *= prod (1:i) / prod (1:n+1+i); |
16634 | 1499 endfor |
1500 if (rem (n*(n-1)/2, 2)) | |
1501 d = -d; | |
1502 endif | |
1503 | |
1504 else | |
1505 error ("gallery: K must have a value of 0 or 1 for ipjfact matrix."); | |
1506 endif | |
1507 | |
1508 detA = d; | |
1509 endif | |
1510 endfunction | |
1511 | |
1512 function J = jordbloc (n, lambda = 1) | |
1513 ## JORDBLOC Jordan block. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1514 ## JORDBLOC(N, LAMBDA) is the N-by-N Jordan block with eigenvalue |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1515 ## LAMBDA. LAMBDA = 1 is the default. |
16634 | 1516 |
1517 if (nargin < 1 || nargin > 2) | |
1518 error ("gallery: 1 to 2 arguments are required for jordbloc matrix."); | |
1519 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1520 error ("gallery: N must be an integer for jordbloc matrix."); | |
1521 elseif (! isnumeric (lambda) || ! isscalar (lambda)) | |
1522 error ("gallery: LAMBDA must be a numeric scalar for jordbloc matrix."); | |
1523 endif | |
1524 | |
1525 J = lambda * eye (n) + diag (ones (n-1, 1), 1); | |
1526 endfunction | |
1527 | |
1528 function U = kahan (n, theta = 1.2, pert = 25) | |
1529 ## KAHAN Kahan matrix - upper trapezoidal. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1530 ## KAHAN(N, THETA) is an upper trapezoidal matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1531 ## that has some interesting properties regarding estimation of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1532 ## condition and rank. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1533 ## The matrix is N-by-N unless N is a 2-vector, in which case it |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1534 ## is N(1)-by-N(2). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1535 ## The parameter THETA defaults to 1.2. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1536 ## The useful range of THETA is 0 < THETA < PI. |
16634 | 1537 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1538 ## To ensure that the QR factorization with column pivoting does not |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1539 ## interchange columns in the presence of rounding errors, the diagonal |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1540 ## is perturbed by PERT*EPS*diag( [N:-1:1] ). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1541 ## The default is PERT = 25, which ensures no interchanges for KAHAN(N) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1542 ## up to at least N = 90 in IEEE arithmetic. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1543 ## KAHAN(N, THETA, PERT) uses the given value of PERT. |
16634 | 1544 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1545 ## The inverse of KAHAN(N, THETA) is known explicitly: see |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1546 ## Higham (1987, p. 588), for example. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1547 ## The diagonal perturbation was suggested by Christian Bischof. |
16634 | 1548 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1549 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1550 ## W. Kahan, Numerical linear algebra, Canadian Math. Bulletin, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1551 ## 9 (1966), pp. 757-801. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1552 ## N.J. Higham, A survey of condition number estimation for |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1553 ## triangular matrices, SIAM Review, 29 (1987), pp. 575-596. |
16634 | 1554 |
1555 if (nargin < 1 || nargin > 3) | |
1556 error ("gallery: 1 to 3 arguments are required for kahan matrix."); | |
1557 elseif (! isnumeric (n) || all (numel (n) != [1 2]) || fix (n) != n) | |
1558 error ("gallery: N must be a 1 or 2 element integer for kahan matrix."); | |
1559 elseif (! isnumeric (theta) || ! isscalar (theta)) | |
1560 error ("gallery: THETA must be a numeric scalar for kahan matrix."); | |
1561 elseif (! isnumeric (pert) || ! isscalar (pert)) | |
1562 error ("gallery: PERT must be a numeric scalar for kahan matrix."); | |
1563 endif | |
1564 | |
1565 ## Parameter n specifies dimension: r-by-n | |
1566 r = n(1); | |
1567 n = n(end); | |
1568 | |
1569 s = sin (theta); | |
1570 c = cos (theta); | |
1571 | |
1572 U = eye (n) - c * triu (ones (n), 1); | |
1573 U = diag (s.^[0:n-1]) * U + pert*eps* diag ([n:-1:1]); | |
1574 if (r > n) | |
1575 U(r,n) = 0; # Extend to an r-by-n matrix | |
1576 else | |
1577 U = U(1:r,:); # Reduce to an r-by-n matrix | |
1578 endif | |
1579 endfunction | |
1580 | |
1581 function A = kms (n, rho = 0.5) | |
1582 ## KMS Kac-Murdock-Szego Toeplitz matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1583 ## A = KMS(N, RHO) is the N-by-N Kac-Murdock-Szego Toeplitz matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1584 ## A(i,j) = RHO^(ABS((i-j))) (for real RHO). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1585 ## If RHO is complex, then the same formula holds except that elements |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1586 ## below the diagonal are conjugated. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1587 ## RHO defaults to 0.5. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1588 ## Properties: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1589 ## A has an LDL' factorization with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1590 ## L = INV(TRIW(N,-RHO,1)'), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1591 ## D(i,i) = (1-ABS(RHO)^2)*EYE(N) except D(1,1) = 1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1592 ## A is positive definite if and only if 0 < ABS(RHO) < 1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1593 ## INV(A) is tridiagonal. |
16634 | 1594 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1595 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1596 ## W.F. Trench, Numerical solution of the eigenvalue problem |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1597 ## for Hermitian Toeplitz matrices, SIAM J. Matrix Analysis and Appl., |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1598 ## 10 (1989), pp. 135-146 (and see the references therein). |
16634 | 1599 |
1600 if (nargin < 1 || nargin > 2) | |
1601 error ("gallery: 1 to 2 arguments are required for lauchli matrix."); | |
1602 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1603 error ("gallery: N must be an integer for lauchli matrix.") |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
1604 elseif (! isscalar (rho)) |
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
1605 error ("gallery: RHO must be a scalar for lauchli matrix.") |
16634 | 1606 endif |
1607 | |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1608 A = (1:n)'*ones (1,n); |
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1609 A = abs (A - A'); |
16634 | 1610 A = rho .^ A; |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1611 if (imag (rho)) |
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
1612 A = conj (tril (A,-1)) + triu (A); |
16634 | 1613 endif |
1614 endfunction | |
1615 | |
1616 function B = krylov (A, x, j) | |
1617 ## KRYLOV Krylov matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1618 ## KRYLOV(A, x, j) is the Krylov matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1619 ## [x, Ax, A^2x, ..., A^(j-1)x], |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1620 ## where A is an n-by-n matrix and x is an n-vector. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1621 ## Defaults: x = ONES(n,1), j = n. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1622 ## KRYLOV(n) is the same as KRYLOV(RANDN(n)). |
16634 | 1623 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1624 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1625 ## G.H. Golub and C.F. Van Loan, Matrix Computations, second edition, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1626 ## Johns Hopkins University Press, Baltimore, Maryland, 1989, p. 369. |
16634 | 1627 |
1628 if (nargin < 1 || nargin > 3) | |
1629 error ("gallery: 1 to 3 arguments are required for krylov matrix."); | |
1630 elseif (! isnumeric (A) || ! issquare (A) || ndims (A) != 2) | |
17199 | 1631 error ("gallery: A must be a square 2-D matrix for krylov matrix."); |
16634 | 1632 endif |
1633 | |
1634 n = length (A); | |
1635 if (isscalar (A)) | |
1636 n = A; | |
1637 A = randn (n); | |
1638 endif | |
1639 | |
1640 if (nargin < 2) | |
1641 x = ones (n, 1); | |
1642 elseif (! isvector (x) || numel (x) != n) | |
1643 error ("gallery: X must be a vector of length equal to A for krylov matrix."); | |
1644 endif | |
1645 | |
1646 if (nargin < 3) | |
1647 j = n; | |
1648 elseif (! isnumeric (j) || ! isscalar (j) || fix (j) != j) | |
1649 error ("gallery: J must be an integer for krylov matrix."); | |
1650 endif | |
1651 | |
1652 B = ones (n, j); | |
1653 B(:,1) = x(:); | |
1654 for i = 2:j | |
1655 B(:,i) = A*B(:,i-1); | |
1656 endfor | |
1657 endfunction | |
1658 | |
1659 function A = lauchli (n, mu = sqrt (eps)) | |
1660 ## LAUCHLI Lauchli matrix - rectangular. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1661 ## LAUCHLI(N, MU) is the (N+1)-by-N matrix [ONES(1,N); MU*EYE(N))]. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1662 ## It is a well-known example in least squares and other problems |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1663 ## that indicates the dangers of forming A'*A. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1664 ## MU defaults to SQRT(EPS). |
16634 | 1665 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1666 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1667 ## P. Lauchli, Jordan-Elimination und Ausgleichung nach |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1668 ## kleinsten Quadraten, Numer. Math, 3 (1961), pp. 226-240. |
16634 | 1669 |
1670 if (nargin < 1 || nargin > 2) | |
1671 error ("gallery: 1 to 2 arguments are required for lauchli matrix."); | |
1672 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1673 error ("gallery: N must be an integer for lauchli matrix."); | |
1674 elseif (! isscalar (mu)) | |
1675 error ("gallery: MU must be a scalar for lauchli matrix."); | |
1676 endif | |
1677 | |
1678 A = [ones(1, n) | |
1679 mu*eye(n) ]; | |
1680 endfunction | |
1681 | |
1682 function A = lehmer (n) | |
1683 ## LEHMER Lehmer matrix - symmetric positive definite. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1684 ## A = LEHMER(N) is the symmetric positive definite N-by-N matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1685 ## A(i,j) = i/j for j >= i. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1686 ## A is totally nonnegative. INV(A) is tridiagonal, and explicit |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1687 ## formulas are known for its entries. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1688 ## N <= COND(A) <= 4*N*N. |
16634 | 1689 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1690 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1691 ## M. Newman and J. Todd, The evaluation of matrix inversion |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1692 ## programs, J. Soc. Indust. Appl. Math., 6 (1958), pp. 466-476. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1693 ## Solutions to problem E710 (proposed by D.H. Lehmer): The inverse |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1694 ## of a matrix, Amer. Math. Monthly, 53 (1946), pp. 534-535. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1695 ## J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1696 ## Birkhauser, Basel, and Academic Press, New York, 1977, p. 154. |
16634 | 1697 |
1698 if (nargin != 1) | |
1699 error ("gallery: 1 argument is required for lehmer matrix."); | |
1700 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1701 error ("gallery: N must be an integer for lehmer matrix."); | |
1702 endif | |
1703 | |
1704 A = ones (n, 1) * (1:n); | |
1705 A = A./A'; | |
1706 A = tril (A) + tril (A, -1)'; | |
1707 endfunction | |
1708 | |
1709 function T = lesp (n) | |
1710 ## LESP A tridiagonal matrix with real, sensitive eigenvalues. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1711 ## LESP(N) is an N-by-N matrix whose eigenvalues are real and smoothly |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1712 ## distributed in the interval approximately [-2*N-3.5, -4.5]. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1713 ## The sensitivities of the eigenvalues increase exponentially as |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1714 ## the eigenvalues grow more negative. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1715 ## The matrix is similar to the symmetric tridiagonal matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1716 ## the same diagonal entries and with off-diagonal entries 1, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1717 ## via a similarity transformation with D = diag(1!,2!,...,N!). |
16634 | 1718 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1719 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1720 ## H.W.J. Lenferink and M.N. Spijker, On the use of stability regions in |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1721 ## the numerical analysis of initial value problems, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1722 ## Math. Comp., 57 (1991), pp. 221-237. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1723 ## L.N. Trefethen, Pseudospectra of matrices, in Numerical Analysis 1991, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1724 ## Proceedings of the 14th Dundee Conference, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1725 ## D.F. Griffiths and G.A. Watson, eds, Pitman Research Notes in |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1726 ## Mathematics, volume 260, Longman Scientific and Technical, Essex, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1727 ## UK, 1992, pp. 234-266. |
16634 | 1728 |
1729 if (nargin != 1) | |
1730 error ("gallery: 1 argument is required for lesp matrix."); | |
1731 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1732 error ("gallery: N must be an integer for lesp matrix."); | |
1733 endif | |
1734 | |
1735 x = 2:n; | |
1736 T = full (tridiag (ones (size (x)) ./x, -(2*[x n+1]+1), x)); | |
1737 endfunction | |
1738 | |
1739 function A = lotkin (n) | |
1740 ## LOTKIN Lotkin matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1741 ## A = LOTKIN(N) is the Hilbert matrix with its first row altered to |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1742 ## all ones. A is unsymmetric, ill-conditioned, and has many negative |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1743 ## eigenvalues of small magnitude. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1744 ## The inverse has integer entries and is known explicitly. |
16634 | 1745 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1746 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1747 ## M. Lotkin, A set of test matrices, MTAC, 9 (1955), pp. 153-161. |
16634 | 1748 |
1749 if (nargin != 1) | |
1750 error ("gallery: 1 argument is required for lotkin matrix."); | |
1751 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1752 error ("gallery: N must be an integer for lotkin matrix."); | |
1753 endif | |
1754 | |
1755 A = hilb (n); | |
1756 A(1,:) = ones (1, n); | |
1757 endfunction | |
1758 | |
1759 function A = minij (n) | |
1760 ## MINIJ Symmetric positive definite matrix MIN(i,j). | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1761 ## A = MINIJ(N) is the N-by-N symmetric positive definite matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1762 ## A(i,j) = MIN(i,j). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1763 ## Properties, variations: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1764 ## INV(A) is tridiagonal: it is minus the second difference matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1765 ## except its (N,N) element is 1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1766 ## 2*A-ONES(N) (Givens' matrix) has tridiagonal inverse and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1767 ## eigenvalues .5*sec^2([2r-1)PI/4N], r=1:N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1768 ## (N+1)*ONES(N)-A also has a tridiagonal inverse. |
16634 | 1769 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1770 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1771 ## J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1772 ## Birkhauser, Basel, and Academic Press, New York, 1977, p. 158. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1773 ## D.E. Rutherford, Some continuant determinants arising in physics and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1774 ## chemistry---II, Proc. Royal Soc. Edin., 63, A (1952), pp. 232-241. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1775 ## (For the eigenvalues of Givens' matrix.) |
16634 | 1776 |
1777 if (nargin != 1) | |
1778 error ("gallery: 1 argument is required for minij matrix."); | |
1779 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1780 error ("gallery: N must be an integer for minij matrix."); | |
1781 endif | |
1782 | |
19944
941e782d0429
gallery: performance increase for minij matrix.
Carnë Draug <carandraug@octave.org>
parents:
19833
diff
changeset
|
1783 A = bsxfun (@min, 1:n, (1:n)'); |
16634 | 1784 endfunction |
1785 | |
1786 function A = moler (n, alpha = -1) | |
1787 ## MOLER Moler matrix - symmetric positive definite. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1788 ## A = MOLER(N, ALPHA) is the symmetric positive definite N-by-N matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1789 ## U'*U where U = TRIW(N, ALPHA). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1790 ## For ALPHA = -1 (the default) A(i,j) = MIN(i,j)-2, A(i,i) = i. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1791 ## A has one small eigenvalue. |
16634 | 1792 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1793 ## Nash (1990) attributes the ALPHA = -1 matrix to Moler. |
16634 | 1794 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1795 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1796 ## J.C. Nash, Compact Numerical Methods for Computers: Linear |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1797 ## Algebra and Function Minimisation, second edition, Adam Hilger, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1798 ## Bristol, 1990 (Appendix 1). |
16634 | 1799 |
1800 if (nargin < 1 || nargin > 2) | |
1801 error ("gallery: 1 to 2 arguments are required for moler matrix."); | |
1802 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1803 error ("gallery: N must be an integer for moler matrix."); | |
1804 elseif (! isscalar (alpha)) | |
1805 error ("gallery: ALPHA must be a scalar for moler matrix."); | |
1806 endif | |
1807 | |
1808 A = triw (n, alpha)' * triw (n, alpha); | |
1809 endfunction | |
1810 | |
1811 function [A, T] = neumann (n) | |
1812 ## NEUMANN Singular matrix from the discrete Neumann problem (sparse). | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1813 ## NEUMANN(N) is the singular, row diagonally dominant matrix resulting |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1814 ## from discretizing the Neumann problem with the usual five point |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1815 ## operator on a regular mesh. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1816 ## It has a one-dimensional null space with null vector ONES(N,1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1817 ## The dimension N should be a perfect square, or else a 2-vector, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1818 ## in which case the dimension of the matrix is N(1)*N(2). |
16634 | 1819 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1820 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1821 ## R.J. Plemmons, Regular splittings and the discrete Neumann |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1822 ## problem, Numer. Math., 25 (1976), pp. 153-161. |
16634 | 1823 |
1824 if (nargin != 1) | |
1825 error ("gallery: 1 argument is required for neumann matrix."); | |
1826 elseif (! isnumeric (n) || all (numel (n) != [1 2]) || fix (n) != n) | |
1827 error ("gallery: N must be a 1 or 2 element integer for neumann matrix."); | |
1828 endif | |
1829 | |
1830 if (isscalar (n)) | |
1831 m = sqrt (n); | |
1832 if (m^2 != n) | |
1833 error ("gallery: N must be a perfect square for neumann matrix."); | |
1834 endif | |
1835 n(1) = m; | |
1836 n(2) = m; | |
1837 endif | |
1838 | |
1839 T = tridiag (n(1), -1, 2, -1); | |
1840 T(1,2) = -2; | |
1841 T(n(1),n(1)-1) = -2; | |
1842 | |
1843 A = kron (T, eye (n(2))) + kron (eye (n(2)), T); | |
1844 endfunction | |
1845 | |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1846 function A = normaldata (varargin) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1847 |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1848 if (nargin < 2) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1849 error ("gallery: At least 2 arguments required for normaldata matrix."); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1850 endif |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1851 if (isnumeric (varargin{end})) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1852 jidx = varargin{end}; |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1853 svec = [varargin{:}]; |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1854 varargin(end) = []; |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1855 elseif (ischar (varargin{end})) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1856 if (nargin < 3) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1857 error (["gallery: CLASS argument requires 3 inputs " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1858 "for normaldata matrix."]); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1859 endif |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1860 jidx = varargin{end-1}; |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1861 svec = [varargin{1:end-1}]; |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1862 varargin(end-1) = []; |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1863 else |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1864 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1865 "for normaldata matrix"]); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1866 endif |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1867 |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1868 if (! (isnumeric (jidx) && isscalar (jidx) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1869 && jidx == fix (jidx) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1870 && jidx >= 0 && jidx <= 0xFFFFFFFF)) |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1871 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1872 "for normaldata matrix"]); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1873 endif |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1874 |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1875 ## Save and restore random state. Initialization done so that reproducible |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1876 ## data is available from gallery depending on the jidx and size vector. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1877 randstate = randn ("state"); |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1878 unwind_protect |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1879 randn ("state", svec); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1880 A = randn (varargin{:}); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1881 unwind_protect_cleanup |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1882 randn ("state", randstate); |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1883 end_unwind_protect |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1884 |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1885 endfunction |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
1886 |
16634 | 1887 function Q = orthog (n, k = 1) |
1888 ## ORTHOG Orthogonal and nearly orthogonal matrices. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1889 ## Q = ORTHOG(N, K) selects the K'th type of matrix of order N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1890 ## K > 0 for exactly orthogonal matrices, K < 0 for diagonal scalings of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1891 ## orthogonal matrices. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1892 ## Available types: (K = 1 is the default) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1893 ## K = 1: Q(i,j) = SQRT(2/(n+1)) * SIN( i*j*PI/(n+1) ) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1894 ## Symmetric eigenvector matrix for second difference matrix. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1895 ## K = 2: Q(i,j) = 2/SQRT(2*n+1)) * SIN( 2*i*j*PI/(2*n+1) ) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1896 ## Symmetric. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1897 ## K = 3: Q(r,s) = EXP(2*PI*i*(r-1)*(s-1)/n) / SQRT(n) (i=SQRT(-1)) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1898 ## Unitary, the Fourier matrix. Q^4 is the identity. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1899 ## This is essentially the same matrix as FFT(EYE(N))/SQRT(N)! |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1900 ## K = 4: Helmert matrix: a permutation of a lower Hessenberg matrix, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1901 ## whose first row is ONES(1:N)/SQRT(N). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1902 ## K = 5: Q(i,j) = SIN( 2*PI*(i-1)*(j-1)/n ) + COS( 2*PI*(i-1)*(j-1)/n ). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1903 ## Symmetric matrix arising in the Hartley transform. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1904 ## K = -1: Q(i,j) = COS( (i-1)*(j-1)*PI/(n-1) ) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1905 ## Chebyshev Vandermonde-like matrix, based on extrema of T(n-1). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1906 ## K = -2: Q(i,j) = COS( (i-1)*(j-1/2)*PI/n) ) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1907 ## Chebyshev Vandermonde-like matrix, based on zeros of T(n). |
16634 | 1908 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1909 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1910 ## N.J. Higham and D.J. Higham, Large growth factors in Gaussian |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1911 ## elimination with pivoting, SIAM J. Matrix Analysis and Appl., |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1912 ## 10 (1989), pp. 155-164. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1913 ## P. Morton, On the eigenvectors of Schur's matrix, J. Number Theory, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1914 ## 12 (1980), pp. 122-127. (Re. ORTHOG(N, 3)) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1915 ## H.O. Lancaster, The Helmert Matrices, Amer. Math. Monthly, 72 (1965), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1916 ## pp. 4-12. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1917 ## D. Bini and P. Favati, On a matrix algebra related to the discrete |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1918 ## Hartley transform, SIAM J. Matrix Anal. Appl., 14 (1993), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1919 ## pp. 500-507. |
16634 | 1920 |
1921 if (nargin < 1 || nargin > 2) | |
1922 error ("gallery: 1 to 2 arguments are required for orthog matrix."); | |
1923 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1924 error ("gallery: N must be an integer for orthog matrix."); | |
1925 elseif (! isnumeric (k) || ! isscalar (k)) | |
1926 error ("gallery: K must be a numeric scalar for orthog matrix."); | |
1927 endif | |
1928 | |
1929 switch (k) | |
1930 case (1) | |
1931 ## E'vectors second difference matrix | |
1932 m = (1:n)'*(1:n) * (pi/(n+1)); | |
1933 Q = sin (m) * sqrt (2/(n+1)); | |
1934 | |
1935 case (2) | |
1936 m = (1:n)'*(1:n) * (2*pi/(2*n+1)); | |
1937 Q = sin (m) * (2/ sqrt (2*n+1)); | |
1938 | |
1939 case (3) | |
1940 ## Vandermonde based on roots of unity | |
1941 m = 0:n-1; | |
1942 Q = exp (m'*m*2*pi* sqrt (-1) / n) / sqrt (n); | |
1943 | |
1944 case (4) | |
1945 ## Helmert matrix | |
1946 Q = tril (ones (n)); | |
1947 Q(1,2:n) = ones (1, n-1); | |
1948 for i = 2:n | |
1949 Q(i,i) = -(i-1); | |
17306
09543e9c8f40
Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1950 endfor |
16634 | 1951 Q = diag (sqrt ([n 1:n-1] .* [1:n])) \ Q; |
1952 | |
1953 case (5) | |
1954 ## Hartley matrix | |
1955 m = (0:n-1)'*(0:n-1) * (2*pi/n); | |
1956 Q = (cos (m) + sin (m)) / sqrt (n); | |
1957 | |
1958 case (-1) | |
1959 ## extrema of T(n-1) | |
1960 m = (0:n-1)'*(0:n-1) * (pi/(n-1)); | |
1961 Q = cos (m); | |
1962 | |
1963 case (-2) | |
1964 ## zeros of T(n) | |
1965 m = (0:n-1)'*(.5:n-.5) * (pi/n); | |
1966 Q = cos (m); | |
1967 | |
1968 otherwise | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
1969 error ("gallery: unknown K '%d' for orthog matrix.", k); |
16634 | 1970 endswitch |
1971 endfunction | |
1972 | |
1973 function A = parter (n) | |
1974 ## PARTER Parter matrix - a Toeplitz matrix with singular values near PI. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1975 ## PARTER(N) is the matrix with (i,j) element 1/(i-j+0.5). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1976 ## It is a Cauchy matrix and a Toeplitz matrix. |
16634 | 1977 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1978 ## At the Second SIAM Conference on Linear Algebra, Raleigh, N.C., |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1979 ## 1985, Cleve Moler noted that most of the singular values of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1980 ## PARTER(N) are very close to PI. An explanation of the phenomenon |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1981 ## was given by Parter; see also the paper by Tyrtyshnikov. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1982 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1983 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1984 ## The MathWorks Newsletter, Volume 1, Issue 1, March 1986, page 2. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1985 ## S.V. Parter, On the distribution of the singular values of Toeplitz |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1986 ## matrices, Linear Algebra and Appl., 80 (1986), pp. 115-130. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1987 ## E.E. Tyrtyshnikov, Cauchy-Toeplitz matrices and some applications, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1988 ## Linear Algebra and Appl., 149 (1991), pp. 1-18. |
16634 | 1989 |
1990 if (nargin != 1) | |
1991 error ("gallery: 1 argument is required for parter matrix."); | |
1992 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
1993 error ("gallery: N must be an integer for parter matrix."); | |
1994 endif | |
1995 | |
1996 A = cauchy ((1:n) + 0.5, -(1:n)); | |
1997 endfunction | |
1998 | |
1999 function P = pei (n, alpha = 1) | |
2000 ## PEI Pei matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2001 ## PEI(N, ALPHA), where ALPHA is a scalar, is the symmetric matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2002 ## ALPHA*EYE(N) + ONES(N). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2003 ## If ALPHA is omitted then ALPHA = 1 is used. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2004 ## The matrix is singular for ALPHA = 0, -N. |
16634 | 2005 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2006 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2007 ## M.L. Pei, A test matrix for inversion procedures, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2008 ## Comm. ACM, 5 (1962), p. 508. |
16634 | 2009 |
2010 if (nargin < 1 || nargin > 2) | |
2011 error ("gallery: 1 to 2 arguments are required for pei matrix."); | |
2012 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2013 error ("gallery: N must be an integer for pei matrix."); | |
20294
2102c4582e32
gallery: fix chebspec, cycol, gearmat, hanowa, lauchli, and pei (bug #45466)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
20231
diff
changeset
|
2014 elseif (! isnumeric (alpha) || ! isscalar (alpha)) |
16634 | 2015 error ("gallery: ALPHA must be a scalar for pei matrix."); |
2016 endif | |
2017 | |
2018 P = alpha * eye (n) + ones (n); | |
2019 endfunction | |
2020 | |
2021 function A = poisson (n) | |
2022 ## POISSON Block tridiagonal matrix from Poisson's equation (sparse). | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2023 ## POISSON(N) is the block tridiagonal matrix of order N^2 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2024 ## resulting from discretizing Poisson's equation with the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2025 ## 5-point operator on an N-by-N mesh. |
16634 | 2026 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2027 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2028 ## G.H. Golub and C.F. Van Loan, Matrix Computations, second edition, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2029 ## Johns Hopkins University Press, Baltimore, Maryland, 1989 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2030 ## (Section 4.5.4). |
16634 | 2031 |
2032 if (nargin != 1) | |
2033 error ("gallery: 1 argument is required for poisson matrix."); | |
2034 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2035 error ("gallery: N must be an integer for poisson matrix."); | |
2036 endif | |
2037 | |
2038 S = tridiag (n, -1, 2, -1); | |
2039 I = speye (n); | |
2040 A = kron (I, S) + kron (S, I); | |
2041 endfunction | |
2042 | |
2043 function A = prolate (n, w = 0.25) | |
2044 ## PROLATE Prolate matrix - symmetric, ill-conditioned Toeplitz matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2045 ## A = PROLATE(N, W) is the N-by-N prolate matrix with parameter W. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2046 ## It is a symmetric Toeplitz matrix. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2047 ## If 0 < W < 0.5 then |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2048 ## - A is positive definite |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2049 ## - the eigenvalues of A are distinct, lie in (0, 1), and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2050 ## tend to cluster around 0 and 1. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2051 ## W defaults to 0.25. |
16634 | 2052 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2053 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2054 ## J.M. Varah. The Prolate matrix. Linear Algebra and Appl., |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2055 ## 187:269--278, 1993. |
16634 | 2056 |
2057 if (nargin < 1 || nargin > 2) | |
2058 error ("gallery: 1 to 2 arguments are required for prolate matrix."); | |
2059 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2060 error ("gallery: N must be an integer for prolate matrix."); | |
2061 elseif (! isnumeric (w) || ! isscalar (w)) | |
2062 error ("gallery: W must be a scalar for prolate matrix."); | |
2063 endif | |
2064 | |
2065 a = zeros (n, 1); | |
2066 a(1) = 2*w; | |
2067 a(2:n) = sin (2*pi*w*(1:n-1)) ./ (pi*(1:n-1)); | |
2068 | |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2069 A = toeplitz (a); |
16634 | 2070 endfunction |
2071 | |
2072 function H = randhess (x) | |
2073 ## NOTE: this function was named ohess in the original Test Matrix Toolbox | |
2074 ## RANDHESS Random, orthogonal upper Hessenberg matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2075 ## H = RANDHESS(N) is an N-by-N real, random, orthogonal |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2076 ## upper Hessenberg matrix. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2077 ## Alternatively, H = RANDHESS(X), where X is an arbitrary real |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2078 ## N-vector (N > 1) constructs H non-randomly using the elements |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2079 ## of X as parameters. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2080 ## In both cases H is constructed via a product of N-1 Givens rotations. |
16634 | 2081 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2082 ## Note: See Gragg (1986) for how to represent an N-by-N (complex) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2083 ## unitary Hessenberg matrix with positive subdiagonal elements in terms |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2084 ## of 2N-1 real parameters (the Schur parametrization). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2085 ## This M-file handles the real case only and is intended simply as a |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2086 ## convenient way to generate random or non-random orthogonal Hessenberg |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2087 ## matrices. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2088 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2089 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2090 ## W.B. Gragg, The QR algorithm for unitary Hessenberg matrices, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2091 ## J. Comp. Appl. Math., 16 (1986), pp. 1-8. |
16634 | 2092 |
2093 if (nargin != 1) | |
2094 error ("gallery: 1 argument is required for randhess matrix."); | |
2095 elseif (! isnumeric (x) || ! isreal (x)) | |
2096 error ("gallery: N or X must be numeric real values for randhess matrix."); | |
2097 endif | |
2098 | |
2099 if (isscalar (x)) | |
2100 n = x; | |
2101 x = rand (n-1, 1) * 2*pi; | |
2102 H = eye (n); | |
2103 H(n,n) = sign (randn); | |
2104 elseif (isvector (x)) | |
2105 n = numel (x); | |
2106 H = eye (n); | |
2107 H(n,n) = sign (x(n)) + (x(n) == 0); # Second term ensures H(n,n) nonzero. | |
2108 else | |
2109 error ("gallery: N or X must be a scalar or a vector for randhess matrix."); | |
2110 endif | |
2111 | |
2112 for i = n:-1:2 | |
2113 ## Apply Givens rotation through angle x(i-1). | |
2114 theta = x(i-1); | |
2115 c = cos (theta); | |
2116 s = sin (theta); | |
2117 H([i-1 i], :) = [ c*H(i-1,:)+s*H(i,:) | |
2118 -s*H(i-1,:)+c*H(i,:) ]; | |
2119 endfor | |
2120 endfunction | |
2121 | |
2122 function A = rando (n, k = 1) | |
2123 ## RANDO Random matrix with elements -1, 0 or 1. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2124 ## A = RANDO(N, K) is a random N-by-N matrix with elements from |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2125 ## one of the following discrete distributions (default K = 1): |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2126 ## K = 1: A(i,j) = 0 or 1 with equal probability, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2127 ## K = 2: A(i,j) = -1 or 1 with equal probability, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2128 ## K = 3: A(i,j) = -1, 0 or 1 with equal probability. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2129 ## N may be a 2-vector, in which case the matrix is N(1)-by-N(2). |
16634 | 2130 |
2131 if (nargin < 1 || nargin > 2) | |
2132 error ("gallery: 1 to 2 arguments are required for rando matrix."); | |
2133 elseif (! isnumeric (n) || all (numel (n) != [1 2]) || fix (n) != n) | |
2134 error ("gallery: N must be an integer for rando matrix."); | |
2135 elseif (! isnumeric (k) || ! isscalar (k)) | |
2136 error ("gallery: K must be a numeric scalar for smoke matrix."); | |
2137 endif | |
2138 | |
2139 ## Parameter n specifies dimension: m-by-n. | |
2140 m = n(1); | |
2141 n = n(end); | |
2142 | |
2143 switch (k) | |
2144 case (1), A = floor ( rand(m, n) + 0.5); # {0, 1} | |
2145 case (2), A = 2*floor ( rand(m, n) + 0.5) -1; # {-1, 1} | |
2146 case (3), A = round (3*rand(m, n) - 1.5); # {-1, 0, 1} | |
2147 otherwise | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
2148 error ("gallery: unknown K '%d' for smoke matrix.", k); |
16634 | 2149 endswitch |
2150 | |
2151 endfunction | |
2152 | |
20296
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2153 function A = randsvd (n, kappa = sqrt (1/eps), mode = 3, kl = max (n) -1, |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2154 ku = kl) |
16634 | 2155 ## RANDSVD Random matrix with pre-assigned singular values. |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2156 ## RANDSVD(N, KAPPA, MODE, KL, KU) is a (banded) random matrix of order N |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2157 ## with COND(A) = KAPPA and singular values from the distribution MODE. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2158 ## N may be a 2-vector, in which case the matrix is N(1)-by-N(2). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2159 ## Available types: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2160 ## MODE = 1: one large singular value, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2161 ## MODE = 2: one small singular value, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2162 ## MODE = 3: geometrically distributed singular values, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2163 ## MODE = 4: arithmetically distributed singular values, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2164 ## MODE = 5: random singular values with unif. dist. logarithm. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2165 ## If omitted, MODE defaults to 3, and KAPPA defaults to SQRT(1/EPS). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2166 ## If MODE < 0 then the effect is as for ABS(MODE) except that in the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2167 ## original matrix of singular values the order of the diagonal entries |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2168 ## is reversed: small to large instead of large to small. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2169 ## KL and KU are the lower and upper bandwidths respectively; if they |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2170 ## are omitted a full matrix is produced. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2171 ## If only KL is present, KU defaults to KL. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2172 ## Special case: if KAPPA < 0 then a random full symmetric positive |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2173 ## definite matrix is produced with COND(A) = -KAPPA and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2174 ## eigenvalues distributed according to MODE. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2175 ## KL and KU, if present, are ignored. |
16634 | 2176 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2177 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2178 ## N.J. Higham, Accuracy and Stability of Numerical Algorithms, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2179 ## Society for Industrial and Applied Mathematics, Philadelphia, PA, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2180 ## USA, 1996; sec. 26.3. |
16634 | 2181 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2182 ## This routine is similar to the more comprehensive Fortran routine xLATMS |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2183 ## in the following reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2184 ## J.W. Demmel and A. McKenney, A test matrix generation suite, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2185 ## LAPACK Working Note #9, Courant Institute of Mathematical Sciences, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2186 ## New York, 1989. |
16634 | 2187 |
2188 if (nargin < 1 || nargin > 5) | |
2189 error ("gallery: 1 to 5 arguments are required for randsvd matrix."); | |
2190 elseif (! isnumeric (n) || all (numel (n) != [1 2]) || fix (n) != n) | |
2191 error ("gallery: N must be a 1 or 2 element integer vector for randsvd matrix."); | |
2192 elseif (! isnumeric (kappa) || ! isscalar (kappa)) | |
2193 error ("gallery: KAPPA must be a numeric scalar for randsvd matrix."); | |
2194 elseif (abs (kappa) < 1) | |
2195 error ("gallery: KAPPA must larger than or equal to 1 for randsvd matrix."); | |
2196 elseif (! isnumeric (mode) || ! isscalar (mode)) | |
2197 error ("gallery: MODE must be a numeric scalar for randsvd matrix."); | |
2198 elseif (! isnumeric (kl) || ! isscalar (kl)) | |
2199 error ("gallery: KL must be a numeric scalar for randsvd matrix."); | |
2200 elseif (! isnumeric (ku) || ! isscalar (ku)) | |
2201 error ("gallery: KU must be a numeric scalar for randsvd matrix."); | |
2202 endif | |
2203 | |
2204 posdef = 0; | |
2205 if (kappa < 0) | |
2206 posdef = 1; | |
2207 kappa = -kappa; | |
2208 endif | |
2209 | |
2210 ## Parameter n specifies dimension: m-by-n. | |
2211 m = n(1); | |
2212 n = n(end); | |
2213 p = min ([m n]); | |
2214 | |
2215 ## If A will be a vector | |
2216 if (p == 1) | |
2217 A = randn (m, n); | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
2218 A /= norm (A); |
17306
09543e9c8f40
Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
2219 return; |
09543e9c8f40
Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
2220 endif |
16634 | 2221 |
2222 ## Set up vector sigma of singular values. | |
2223 switch (abs (mode)) | |
2224 case (1) | |
2225 sigma = ones (p, 1) ./ kappa; | |
2226 sigma(1) = 1; | |
2227 case (2) | |
2228 sigma = ones (p, 1); | |
2229 sigma(p) = 1 / kappa; | |
2230 case (3) | |
2231 factor = kappa^(-1/(p-1)); | |
2232 sigma = factor.^[0:p-1]; | |
2233 case (4) | |
2234 sigma = ones (p, 1) - (0:p-1)'/(p-1)*(1-1/kappa); | |
2235 case (5) | |
2236 ## In this case cond (A) <= kappa. | |
2237 rand ("uniform"); | |
2238 sigma = exp (-rand (p, 1) * log (kappa)); | |
2239 otherwise | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
2240 error ("gallery: unknown MODE '%d' for randsvd matrix.", mode); |
16634 | 2241 endswitch |
2242 | |
2243 ## Convert to diagonal matrix of singular values. | |
2244 if (mode < 0) | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2245 sigma = sigma(p:-1:1); |
17306
09543e9c8f40
Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
2246 endif |
16634 | 2247 sigma = diag (sigma); |
2248 | |
2249 if (posdef) | |
2250 ## handle case where KAPPA was negative | |
2251 Q = qmult (p); | |
2252 A = Q' * sigma * Q; | |
2253 A = (A + A') / 2; # Ensure matrix is symmetric. | |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17306
diff
changeset
|
2254 return; |
16634 | 2255 endif |
2256 | |
2257 if (m != n) | |
2258 ## Expand to m-by-n diagonal matrix | |
2259 sigma(m, n) = 0; | |
17306
09543e9c8f40
Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
2260 endif |
16634 | 2261 |
2262 if (kl == 0 && ku == 0) | |
2263 ## Diagonal matrix requested - nothing more to do. | |
2264 A = sigma; | |
2265 else | |
2266 ## A = U*sigma*V, where U, V are random orthogonal matrices from the | |
2267 ## Haar distribution. | |
2268 A = qmult (sigma'); | |
2269 A = qmult (A'); | |
2270 | |
2271 if (kl < n-1 || ku < n-1) | |
2272 ## Bandwidth reduction | |
2273 A = bandred (A, kl, ku); | |
2274 endif | |
2275 endif | |
2276 endfunction | |
2277 | |
2278 function A = redheff (n) | |
2279 ## REDHEFF A (0,1) matrix of Redheffer associated with the Riemann hypothesis. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2280 ## A = REDHEFF(N) is an N-by-N matrix of 0s and 1s defined by |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2281 ## A(i,j) = 1 if j = 1 or if i divides j, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2282 ## A(i,j) = 0 otherwise. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2283 ## It has N - FLOOR(LOG2(N)) - 1 eigenvalues equal to 1, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2284 ## a real eigenvalue (the spectral radius) approximately SQRT(N), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2285 ## a negative eigenvalue approximately -SQRT(N), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2286 ## and the remaining eigenvalues are provably ``small''. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2287 ## Barrett and Jarvis (1992) conjecture that |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2288 ## ``the small eigenvalues all lie inside the unit circle |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2289 ## ABS(Z) = 1'', |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2290 ## and a proof of this conjecture, together with a proof that some |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2291 ## eigenvalue tends to zero as N tends to infinity, would yield |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2292 ## a new proof of the prime number theorem. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2293 ## The Riemann hypothesis is true if and only if |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2294 ## DET(A) = O( N^(1/2+epsilon) ) for every epsilon > 0 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2295 ## (`!' denotes factorial). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2296 ## See also RIEMANN. |
16634 | 2297 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2298 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2299 ## W.W. Barrett and T.J. Jarvis, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2300 ## Spectral Properties of a Matrix of Redheffer, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2301 ## Linear Algebra and Appl., 162 (1992), pp. 673-683. |
16634 | 2302 |
2303 if (nargin != 1) | |
2304 error ("gallery: 1 argument is required for redheff matrix."); | |
2305 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2306 error ("gallery: N must be an integer for redheff matrix."); | |
2307 endif | |
2308 | |
2309 i = (1:n)' * ones (1, n); | |
2310 A = ! rem (i', i); | |
2311 A(:,1) = ones (n, 1); | |
2312 endfunction | |
2313 | |
2314 function A = riemann (n) | |
2315 ## RIEMANN A matrix associated with the Riemann hypothesis. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2316 ## A = RIEMANN(N) is an N-by-N matrix for which the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2317 ## Riemann hypothesis is true if and only if |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2318 ## DET(A) = O( N! N^(-1/2+epsilon) ) for every epsilon > 0 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2319 ## (`!' denotes factorial). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2320 ## A = B(2:N+1, 2:N+1), where |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2321 ## B(i,j) = i-1 if i divides j and -1 otherwise. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2322 ## Properties include, with M = N+1: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2323 ## Each eigenvalue E(i) satisfies ABS(E(i)) <= M - 1/M. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2324 ## i <= E(i) <= i+1 with at most M-SQRT(M) exceptions. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2325 ## All integers in the interval (M/3, M/2] are eigenvalues. |
16634 | 2326 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2327 ## See also REDHEFF. |
16634 | 2328 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2329 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2330 ## F. Roesler, Riemann's hypothesis as an eigenvalue problem, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2331 ## Linear Algebra and Appl., 81 (1986), pp. 153-198. |
16634 | 2332 |
2333 if (nargin != 1) | |
2334 error ("gallery: 1 argument is required for riemann matrix."); | |
2335 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2336 error ("gallery: N must be an integer for riemann matrix."); | |
2337 endif | |
2338 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
2339 n += 1; |
16634 | 2340 i = (2:n)' * ones (1, n-1); |
2341 j = i'; | |
2342 A = i .* (! rem (j, i)) - ones (n-1); | |
2343 endfunction | |
2344 | |
2345 function A = ris (n) | |
2346 ## NOTE: this function was named dingdong in the original Test Matrix Toolbox | |
2347 ## RIS Dingdong matrix - a symmetric Hankel matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2348 ## A = RIS(N) is the symmetric N-by-N Hankel matrix with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2349 ## A(i,j) = 0.5/(N-i-j+1.5). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2350 ## The eigenvalues of A cluster around PI/2 and -PI/2. |
16634 | 2351 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2352 ## Invented by F.N. Ris. |
16634 | 2353 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2354 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2355 ## J.C. Nash, Compact Numerical Methods for Computers: Linear |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2356 ## Algebra and Function Minimisation, second edition, Adam Hilger, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2357 ## Bristol, 1990 (Appendix 1). |
16634 | 2358 |
2359 if (nargin != 1) | |
2360 error ("gallery: 1 argument is required for ris matrix."); | |
2361 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2362 error ("gallery: N must be an integer for ris matrix."); | |
2363 endif | |
2364 | |
2365 p = -2*(1:n) + (n+1.5); | |
2366 A = cauchy (p); | |
2367 endfunction | |
2368 | |
2369 function A = smoke (n, k = 0) | |
2370 ## SMOKE Smoke matrix - complex, with a `smoke ring' pseudospectrum. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2371 ## SMOKE(N) is an N-by-N matrix with 1s on the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2372 ## superdiagonal, 1 in the (N,1) position, and powers of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2373 ## roots of unity along the diagonal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2374 ## SMOKE(N, 1) is the same except for a zero (N,1) element. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2375 ## The eigenvalues of SMOKE(N, 1) are the N'th roots of unity; |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2376 ## those of SMOKE(N) are the N'th roots of unity times 2^(1/N). |
16634 | 2377 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2378 ## Try PS(SMOKE(32)). For SMOKE(N, 1) the pseudospectrum looks |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2379 ## like a sausage folded back on itself. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2380 ## GERSH(SMOKE(N, 1)) is interesting. |
16634 | 2381 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2382 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2383 ## L. Reichel and L.N. Trefethen, Eigenvalues and pseudo-eigenvalues of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2384 ## Toeplitz matrices, Linear Algebra and Appl., 162-164:153-185, 1992. |
16634 | 2385 |
2386 if (nargin < 1 || nargin > 2) | |
2387 error ("gallery: 1 to 2 arguments are required for smoke matrix."); | |
2388 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2389 error ("gallery: N must be an integer for smoke matrix."); | |
2390 elseif (! isnumeric (n) || ! isscalar (n)) | |
2391 error ("gallery: K must be a numeric scalar for smoke matrix."); | |
2392 endif | |
2393 | |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2394 w = exp (2*pi*i/n); |
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2395 A = diag ( [w.^(1:n-1) 1] ) + diag (ones (n-1,1), 1); |
16634 | 2396 |
2397 switch (k) | |
2398 case (0), A(n,1) = 1; | |
2399 case (1), # do nothing | |
2400 otherwise, | |
2401 error ("gallery: K must have a value of 0 or 1 for smoke matrix."); | |
2402 endswitch | |
2403 endfunction | |
2404 | |
2405 function T = toeppd (n, m = n, w = rand (m,1), theta = rand (m,1)) | |
2406 ## NOTE: this function was named pdtoep in the original Test Matrix Toolbox | |
2407 ## TOEPPD Symmetric positive definite Toeplitz matrix. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2408 ## TOEPPD(N, M, W, THETA) is an N-by-N symmetric positive (semi-) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2409 ## definite (SPD) Toeplitz matrix, comprised of the sum of M rank 2 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2410 ## (or, for certain THETA, rank 1) SPD Toeplitz matrices. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2411 ## Specifically, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2412 ## T = W(1)*T(THETA(1)) + ... + W(M)*T(THETA(M)), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2413 ## where T(THETA(k)) has (i,j) element COS(2*PI*THETA(k)*(i-j)). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2414 ## Defaults: M = N, W = RAND(M,1), THETA = RAND(M,1). |
16634 | 2415 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2416 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2417 ## G. Cybenko and C.F. Van Loan, Computing the minimum eigenvalue of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2418 ## a symmetric positive definite Toeplitz matrix, SIAM J. Sci. Stat. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2419 ## Comput., 7 (1986), pp. 123-131. |
16634 | 2420 |
2421 if (nargin < 1 || nargin > 4) | |
2422 error ("gallery: 1 to 4 arguments are required for toeppd matrix."); | |
2423 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2424 error ("gallery: N must be a numeric integer for toeppd matrix."); | |
2425 elseif (! isnumeric (m) || ! isscalar (m) || fix (m) != m) | |
2426 error ("gallery: M must be a numeric integer for toeppd matrix."); | |
2427 elseif (numel (w) != m || numel (theta) != m) | |
2428 error ("gallery: W and THETA must be vectors of length M for toeppd matrix."); | |
2429 endif | |
2430 | |
2431 T = zeros (n); | |
2432 E = 2*pi * ((1:n)' * ones (1, n) - ones (n, 1) * (1:n)); | |
2433 | |
2434 for i = 1:m | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
2435 T += w(i) * cos (theta(i)*E); |
16634 | 2436 endfor |
2437 endfunction | |
2438 | |
2439 function P = toeppen (n, a = 1, b = -10, c = 0, d = 10, e = 1) | |
2440 ## NOTE: this function was named pentoep in the original Test Matrix Toolbox | |
2441 ## TOEPPEN Pentadiagonal Toeplitz matrix (sparse). | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2442 ## P = TOEPPEN(N, A, B, C, D, E) is the N-by-N pentadiagonal |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2443 ## Toeplitz matrix with diagonals composed of the numbers |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2444 ## A =: P(3,1), B =: P(2,1), C =: P(1,1), D =: P(1,2), E =: P(1,3). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2445 ## Default: (A,B,C,D,E) = (1,-10,0,10,1) (a matrix of Rutishauser). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2446 ## This matrix has eigenvalues lying approximately on |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2447 ## the line segment 2*cos(2*t) + 20*i*sin(t). |
16634 | 2448 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2449 ## Interesting plots are |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2450 ## PS(FULL(TOEPPEN(32,0,1,0,0,1/4))) - `triangle' |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2451 ## PS(FULL(TOEPPEN(32,0,1/2,0,0,1))) - `propeller' |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2452 ## PS(FULL(TOEPPEN(32,0,1/2,1,1,1))) - `fish' |
16634 | 2453 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2454 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2455 ## R.M. Beam and R.F. Warming, The asymptotic spectra of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2456 ## banded Toeplitz and quasi-Toeplitz matrices, SIAM J. Sci. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2457 ## Comput. 14 (4), 1993, pp. 971-1006. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2458 ## H. Rutishauser, On test matrices, Programmation en Mathematiques |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2459 ## Numeriques, Editions Centre Nat. Recherche Sci., Paris, 165, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2460 ## 1966, pp. 349-365. |
16634 | 2461 |
2462 if (nargin < 1 || nargin > 6) | |
2463 error ("gallery: 1 to 6 arguments are required for toeppen matrix."); | |
2464 elseif (! isnumeric (n) || ! isscalar (n) || fix (n) != n) | |
2465 error ("gallery: N must be a numeric integer for toeppen matrix."); | |
17386
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
2466 elseif (any (! cellfun ("isnumeric", {a b c d e})) || any (cellfun ("numel", {a b c d e}) != 1)) |
16634 | 2467 error ("gallery: A, B, C, D and E must be numeric scalars for toeppen matrix."); |
2468 endif | |
2469 | |
2470 P = spdiags ([a*ones(n,1) b*ones(n,1) c*ones(n,1) d*ones(n,1) e*ones(n,1)], | |
2471 -2:2, n, n); | |
2472 endfunction | |
2473 | |
2474 function T = tridiag (n, x = -1, y = 2, z = -1) | |
2475 ## TRIDIAG Tridiagonal matrix (sparse). | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2476 ## TRIDIAG(X, Y, Z) is the tridiagonal matrix with subdiagonal X, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2477 ## diagonal Y, and superdiagonal Z. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2478 ## X and Z must be vectors of dimension one less than Y. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2479 ## Alternatively TRIDIAG(N, C, D, E), where C, D, and E are all |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2480 ## scalars, yields the Toeplitz tridiagonal matrix of order N |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2481 ## with subdiagonal elements C, diagonal elements D, and superdiagonal |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2482 ## elements E. This matrix has eigenvalues (Todd 1977) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2483 ## D + 2*SQRT(C*E)*COS(k*PI/(N+1)), k=1:N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2484 ## TRIDIAG(N) is the same as TRIDIAG(N,-1,2,-1), which is |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2485 ## a symmetric positive definite M-matrix (the negative of the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2486 ## second difference matrix). |
16634 | 2487 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2488 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2489 ## J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2490 ## Birkhauser, Basel, and Academic Press, New York, 1977, p. 155. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2491 ## D.E. Rutherford, Some continuant determinants arising in physics and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2492 ## chemistry---II, Proc. Royal Soc. Edin., 63, A (1952), pp. 232-241. |
16634 | 2493 |
2494 if (nargin != 1 && nargin != 3 && nargin != 4) | |
2495 error ("gallery: 1, 3, or 4 arguments are required for tridiag matrix."); | |
2496 elseif (nargin == 3) | |
2497 z = y; | |
2498 y = x; | |
2499 x = n; | |
2500 endif | |
2501 | |
2502 ## Force column vectors | |
2503 x = x(:); | |
2504 y = y(:); | |
2505 z = z(:); | |
2506 | |
2507 if (isscalar (x) && isscalar (y) && isscalar (z)) | |
2508 x *= ones (n-1, 1); | |
2509 z *= ones (n-1, 1); | |
2510 y *= ones (n, 1); | |
2511 elseif (numel (y) != numel (x) + 1) | |
2512 error ("gallery: X must have one element less than Y for tridiag matrix."); | |
2513 elseif (numel (y) != numel (z) + 1) | |
2514 error ("gallery: Z must have one element less than Y for tridiag matrix."); | |
2515 endif | |
2516 | |
2517 ## T = diag (x, -1) + diag (y) + diag (z, 1); # For non-sparse matrix. | |
2518 n = numel (y); | |
2519 T = spdiags ([[x;0] y [0;z]], -1:1, n, n); | |
2520 endfunction | |
2521 | |
18846
3d33fe79816c
gallery.m: Return correct matrix if 3rd argument is not specified.
Rik <rik@octave.org>
parents:
18812
diff
changeset
|
2522 function t = triw (n, alpha = -1, k = n(end) - 1) |
16634 | 2523 ## TRIW Upper triangular matrix discussed by Wilkinson and others. |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2524 ## TRIW(N, ALPHA, K) is the upper triangular matrix with ones on |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2525 ## the diagonal and ALPHAs on the first K >= 0 superdiagonals. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2526 ## N may be a 2-vector, in which case the matrix is N(1)-by-N(2) and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2527 ## upper trapezoidal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2528 ## Defaults: ALPHA = -1, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2529 ## K = N - 1 (full upper triangle). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2530 ## TRIW(N) is a matrix discussed by Kahan, Golub and Wilkinson. |
16634 | 2531 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2532 ## Ostrowski (1954) shows that |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2533 ## COND(TRIW(N,2)) = COT(PI/(4*N))^2, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2534 ## and for large ABS(ALPHA), |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2535 ## COND(TRIW(N,ALPHA)) is approximately ABS(ALPHA)^N*SIN(PI/(4*N-2)). |
16634 | 2536 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2537 ## Adding -2^(2-N) to the (N,1) element makes TRIW(N) singular, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2538 ## as does adding -2^(1-N) to all elements in the first column. |
16634 | 2539 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2540 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2541 ## G.H. Golub and J.H. Wilkinson, Ill-conditioned eigensystems and the |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2542 ## computation of the Jordan canonical form, SIAM Review, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2543 ## 18(4), 1976, pp. 578-619. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2544 ## W. Kahan, Numerical linear algebra, Canadian Math. Bulletin, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2545 ## 9 (1966), pp. 757-801. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2546 ## A.M. Ostrowski, On the spectrum of a one-parametric family of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2547 ## matrices, J. Reine Angew. Math., 193 (3/4), 1954, pp. 143-160. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2548 ## J.H. Wilkinson, Singular-value decomposition---basic aspects, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2549 ## in D.A.H. Jacobs, ed., Numerical Software---Needs and Availability, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2550 ## Academic Press, London, 1978, pp. 109-135. |
16634 | 2551 |
2552 if (nargin < 1 || nargin > 3) | |
2553 error ("gallery: 1 to 3 arguments are required for triw matrix."); | |
2554 elseif (! isnumeric (n) || all (numel (n) != [1 2])) | |
2555 error ("gallery: N must be a 1 or 2 elements vector for triw matrix."); | |
2556 elseif (! isscalar (alpha)) | |
2557 error ("gallery: ALPHA must be a scalar for triw matrix."); | |
18846
3d33fe79816c
gallery.m: Return correct matrix if 3rd argument is not specified.
Rik <rik@octave.org>
parents:
18812
diff
changeset
|
2558 elseif (! isscalar (k) || ! isnumeric (k) || fix (k) != k || k < 0) |
3d33fe79816c
gallery.m: Return correct matrix if 3rd argument is not specified.
Rik <rik@octave.org>
parents:
18812
diff
changeset
|
2559 error ("gallery: K must be a numeric integer >= 0 for triw matrix."); |
16634 | 2560 endif |
2561 | |
2562 m = n(1); # Parameter n specifies dimension: m-by-n. | |
2563 n = n(end); | |
2564 | |
2565 t = tril (eye (m, n) + alpha * triu (ones (m, n), 1), k); | |
2566 endfunction | |
2567 | |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2568 function A = uniformdata (varargin) |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2569 |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2570 if (nargin < 2) |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2571 error ("gallery: At least 2 arguments required for uniformdata matrix."); |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2572 endif |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2573 if (isnumeric (varargin{end})) |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2574 jidx = varargin{end}; |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2575 svec = [varargin{:}]; |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2576 varargin(end) = []; |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2577 elseif (ischar (varargin{end})) |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2578 if (nargin < 3) |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2579 error (["gallery: CLASS argument requires 3 inputs " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2580 "for uniformdata matrix."]); |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2581 endif |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2582 jidx = varargin{end-1}; |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2583 svec = [varargin{1:end-1}]; |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2584 varargin(end-1) = []; |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2585 else |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2586 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2587 "for uniformdata matrix"]); |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2588 endif |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2589 |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2590 if (! (isnumeric (jidx) && isscalar (jidx) |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2591 && jidx == fix (jidx) |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2592 && jidx >= 0 && jidx <= 0xFFFFFFFF)) |
16979
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2593 error (["gallery: J must be an integer in the range [0, 2^32-1] " ... |
9aa293e00475
gallery.m: Add 'normaldata' matrix to function.
Rik <rik@octave.org>
parents:
16978
diff
changeset
|
2594 "for uniformdata matrix"]); |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2595 endif |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2596 |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2597 ## Save and restore random state. Initialization done so that reproducible |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2598 ## data is available from gallery depending on the jidx and size vector. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2599 randstate = rand ("state"); |
16978
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2600 unwind_protect |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2601 rand ("state", svec); |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2602 A = rand (varargin{:}); |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2603 unwind_protect_cleanup |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2604 rand ("state", randstate); |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2605 end_unwind_protect |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2606 |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2607 endfunction |
00379f9f8773
gallery.m: Add 'uniformdata' matrix to function.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
2608 |
16634 | 2609 function A = wathen (nx, ny, k = 0) |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2610 ## WATHEN returns the Wathen matrix. |
16634 | 2611 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2612 ## Discussion: |
16634 | 2613 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2614 ## The Wathen matrix is a finite element matrix which is sparse. |
16634 | 2615 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2616 ## The entries of the matrix depend in part on a physical quantity |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2617 ## related to density. That density is here assigned random values between |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2618 ## 0 and 100. |
16634 | 2619 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2620 ## A = WATHEN ( NX, NY ) is a sparse random N-by-N finite element matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2621 ## where N = 3*NX*NY + 2*NX + 2*NY + 1. |
16634 | 2622 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2623 ## A is the consistent mass matrix for a regular NX-by-NY |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2624 ## grid of 8-node (serendipity) elements in 2 space dimensions. |
16634 | 2625 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2626 ## Here is an illustration for NX = 3, NX = 2: |
16634 | 2627 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2628 ## 23-24-25-26-27-28-29 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2629 ## | | | | |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2630 ## 19 20 21 22 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2631 ## | | | | |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2632 ## 12-13-14-15-16-17-18 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2633 ## | | | | |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2634 ## 8 9 10 11 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2635 ## | | | | |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2636 ## 1--2--3--4--5--6--7 |
16634 | 2637 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2638 ## For this example, the total number of nodes is, as expected, |
16634 | 2639 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2640 ## N = 3 * 3 * 2 + 2 * 2 + 2 * 3 + 1 = 29. |
16634 | 2641 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2642 ## A is symmetric positive definite for any (positive) values of |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2643 ## the density, RHO(NX,NY), which is chosen randomly in this routine. |
16634 | 2644 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2645 ## In particular, if D = DIAG(DIAG(A)), then |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2646 ## 0.25 <= EIG(INV(D)*A) <= 4.5 |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2647 ## for any positive integers NX and NY and any densities RHO(NX,NY). |
16634 | 2648 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2649 ## A = WATHEN ( NX, NY, 1 ) returns the diagonally scaled matrix. |
16634 | 2650 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2651 ## Modified: |
16634 | 2652 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2653 ## 17 September 2007 |
16634 | 2654 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2655 ## Author: |
16634 | 2656 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2657 ## Nicholas Higham |
16634 | 2658 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2659 ## Reference: |
16634 | 2660 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2661 ## Nicholas Higham, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2662 ## Algorithm 694: A Collection of Test Matrices in MATLAB, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2663 ## ACM Transactions on Mathematical Software, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2664 ## Volume 17, Number 3, September 1991, pages 289-305. |
16634 | 2665 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2666 ## Andrew Wathen, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2667 ## Realistic eigenvalue bounds for the Galerkin mass matrix, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2668 ## IMA Journal of Numerical Analysis, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2669 ## Volume 7, 1987, pages 449-457. |
16634 | 2670 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2671 ## Parameters: |
16634 | 2672 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2673 ## Input, integer NX, NY, the number of elements in the X and Y directions |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2674 ## of the finite element grid. NX and NY must each be at least 1. |
16634 | 2675 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2676 ## Optional input, integer K, is used to request that the diagonally scaled |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2677 ## version of the matrix be returned. This happens if K is specified with |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2678 ## the value 1. |
16634 | 2679 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2680 ## Output, sparse real A(N,N), the matrix. The dimension N is determined by |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2681 ## NX and NY, as described above. A is stored in the MATLAB sparse matrix |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2682 ## format. |
16634 | 2683 |
2684 if (nargin < 2 || nargin > 3) | |
2685 error ("gallery: 2 or 3 arguments are required for wathen matrix."); | |
2686 elseif (! isnumeric (nx) || ! isscalar (nx) || nx < 1) | |
2687 error ("gallery: NX must be a positive scalar for wathen matrix."); | |
2688 elseif (! isnumeric (ny) || ! isscalar (ny) || ny < 1) | |
2689 error ("gallery: NY must be a positive scalar for wathen matrix."); | |
2690 elseif (! isscalar (k)) | |
2691 error ("gallery: K must be a scalar for wathen matrix."); | |
2692 endif | |
2693 | |
2694 e1 = [ 6 -6 2 -8 | |
2695 -6 32 -6 20 | |
2696 2 -6 6 -6 | |
2697 -8 20 -6 32 ]; | |
2698 | |
2699 e2 = [ 3 -8 2 -6 | |
2700 -8 16 -8 20 | |
2701 2 -8 3 -8 | |
2702 -6 20 -8 16 ]; | |
2703 | |
2704 e = [ e1 e2 | |
2705 e2' e1] / 45; | |
2706 | |
2707 n = 3*nx*ny + 2*nx + 2*ny + 1; | |
2708 | |
2709 A = sparse (n, n); | |
2710 | |
2711 rho = 100 * rand (nx, ny); | |
2712 | |
2713 for j = 1:ny | |
2714 for i = 1:nx | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2715 ## |
16634 | 2716 ## For the element (I,J), determine the indices of the 8 nodes. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2717 ## |
16634 | 2718 nn(1) = 3*j*nx + 2*i + 2*j + 1; |
2719 nn(2) = nn(1) - 1; | |
2720 nn(3) = nn(2) - 1; | |
2721 nn(4) = (3*j - 1) * nx + 2*j + i - 1; | |
2722 nn(5) = 3 * (j-1) * nx + 2*i + 2*j - 3; | |
2723 nn(6) = nn(5) + 1; | |
2724 nn(7) = nn(6) + 1; | |
2725 nn(8) = nn(4) + 1; | |
2726 | |
2727 em = e * rho(i,j); | |
2728 | |
2729 for krow = 1:8 | |
2730 for kcol = 1:8 | |
2731 A(nn(krow),nn(kcol)) = A(nn(krow),nn(kcol)) + em(krow,kcol); | |
2732 endfor | |
2733 endfor | |
2734 | |
2735 endfor | |
2736 endfor | |
2737 | |
2738 ## If requested, return A with diagonal scaling. | |
2739 if (k) | |
2740 A = diag (diag (A)) \ A; | |
2741 endif | |
2742 endfunction | |
2743 | |
2744 function [A, b] = wilk (n) | |
2745 ## WILK Various specific matrices devised/discussed by Wilkinson. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2746 ## [A, b] = WILK(N) is the matrix or system of order N. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2747 ## N = 3: upper triangular system Ux=b illustrating inaccurate solution. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2748 ## N = 4: lower triangular system Lx=b, ill-conditioned. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2749 ## N = 5: HILB(6)(1:5,2:6)*1.8144. Symmetric positive definite. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2750 ## N = 21: W21+, tridiagonal. Eigenvalue problem. |
16634 | 2751 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2752 ## References: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2753 ## J.H. Wilkinson, Error analysis of direct methods of matrix inversion, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2754 ## J. Assoc. Comput. Mach., 8 (1961), pp. 281-330. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2755 ## J.H. Wilkinson, Rounding Errors in Algebraic Processes, Notes on Applied |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2756 ## Science No. 32, Her Majesty's Stationery Office, London, 1963. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2757 ## J.H. Wilkinson, The Algebraic Eigenvalue Problem, Oxford University |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2758 ## Press, 1965. |
16634 | 2759 |
2760 if (nargin != 1) | |
2761 error ("gallery: 1 argument is required for wilk matrix."); | |
2762 elseif (! isnumeric (n) || ! isscalar (n)) | |
2763 error ("gallery: N must be a numeric scalar for wilk matrix."); | |
2764 endif | |
2765 | |
2766 if (n == 3) | |
2767 ## Wilkinson (1961) p.323. | |
2768 A = [ 1e-10 0.9 -0.4 | |
2769 0 0.9 -0.4 | |
2770 0 0 1e-10 ]; | |
2771 | |
2772 b = [ 0 | |
2773 0 | |
2774 1]; | |
2775 | |
2776 elseif (n == 4) | |
2777 ## Wilkinson (1963) p.105. | |
2778 A = [0.9143e-4 0 0 0 | |
2779 0.8762 0.7156e-4 0 0 | |
2780 0.7943 0.8143 0.9504e-4 0 | |
2781 0.8017 0.6123 0.7165 0.7123e-4]; | |
2782 | |
2783 b = [0.6524 | |
2784 0.3127 | |
2785 0.4186 | |
2786 0.7853]; | |
2787 | |
2788 elseif (n == 5) | |
2789 ## Wilkinson (1965), p.234. | |
2790 A = hilb (6); | |
2791 A = A(1:5, 2:6) * 1.8144; | |
2792 | |
2793 elseif (n == 21) | |
2794 ## Wilkinson (1965), p.308. | |
2795 E = diag (ones (n-1, 1), 1); | |
2796 m = (n-1)/2; | |
2797 A = diag (abs (-m:m)) + E + E'; | |
2798 | |
2799 else | |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
16734
diff
changeset
|
2800 error ("gallery: unknown N '%d' for wilk matrix.", n); |
16634 | 2801 endif |
2802 endfunction | |
2803 | |
2804 ## NOTE: bandred is part of the Test Matrix Toolbox and is used by randsvd() | |
2805 function A = bandred (A, kl, ku) | |
2806 ## BANDRED Band reduction by two-sided unitary transformations. | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2807 ## B = BANDRED(A, KL, KU) is a matrix unitarily equivalent to A |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2808 ## with lower bandwidth KL and upper bandwidth KU |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2809 ## (i.e. B(i,j) = 0 if i > j+KL or j > i+KU). |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2810 ## The reduction is performed using Householder transformations. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2811 ## If KU is omitted it defaults to KL. |
16634 | 2812 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2813 ## Called by RANDSVD. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2814 ## This is a `standard' reduction. Cf. reduction to bidiagonal form |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2815 ## prior to computing the SVD. This code is a little wasteful in that |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2816 ## it computes certain elements which are immediately set to zero! |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
2817 ## |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2818 ## Reference: |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2819 ## G.H. Golub and C.F. Van Loan, Matrix Computations, second edition, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2820 ## Johns Hopkins University Press, Baltimore, Maryland, 1989. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2821 ## Section 5.4.3. |
16634 | 2822 |
2823 ## Check for special case where order of left/right transformations matters. | |
2824 ## Easiest approach is to work on the transpose, flipping back at the end. | |
2825 flip = false; | |
2826 if (ku == 0) | |
2827 flip = true; | |
2828 A = A'; | |
2829 [ku, kl] = deal (kl, ku); | |
2830 endif | |
2831 | |
2832 [m, n] = size (A); | |
2833 | |
2834 for j = 1:min (min (m, n), max (m-kl-1, n-ku-1)) | |
2835 if (j+kl+1 <= m) | |
2836 [v, beta] = house (A(j+kl:m,j)); | |
2837 temp = A(j+kl:m,j:n); | |
2838 A(j+kl:m,j:n) = temp - beta*v*(v'*temp); | |
2839 A(j+kl+1:m,j) = zeros (m-j-kl, 1); | |
2840 endif | |
2841 | |
2842 if (j+ku+1 <= n) | |
2843 [v, beta] = house (A(j,j+ku:n)'); | |
2844 temp = A(j:m,j+ku:n); | |
2845 A(j:m,j+ku:n) = temp - beta*(temp*v)*v'; | |
2846 A(j,j+ku+1:n) = zeros (1, n-j-ku); | |
2847 endif | |
2848 endfor | |
2849 | |
2850 if (flip) | |
2851 A = A'; | |
2852 endif | |
2853 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
2854 |
20296
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2855 ## NOTE: qmult is part of the Test Matrix Toolbox and is used by randsvd() |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2856 function B = qmult (A) |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2857 ## QMULT Pre-multiply by random orthogonal matrix. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2858 ## QMULT(A) is Q*A where Q is a random real orthogonal matrix from |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2859 ## the Haar distribution, of dimension the number of rows in A. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2860 ## Special case: if A is a scalar then QMULT(A) is the same as |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2861 ## QMULT(EYE(A)). |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2862 ## |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2863 ## Called by RANDSVD. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2864 ## |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2865 ## Reference: |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2866 ## G.W. Stewart, The efficient generation of random |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2867 ## orthogonal matrices with an application to condition estimators, |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2868 ## SIAM J. Numer. Anal., 17 (1980), 403-409. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2869 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2870 [n, m] = size (A); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2871 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2872 ## Handle scalar A |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2873 if (isscalar (A)) |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2874 n = A; |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2875 A = eye (n); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2876 endif |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2877 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2878 d = zeros (n); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2879 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2880 for k = n-1:-1:1 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2881 ## Generate random Householder transformation. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2882 x = randn (n-k+1, 1); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2883 s = norm (x); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2884 sgn = sign (x(1)) + (x(1) == 0); # Modification for sign(1)=1. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2885 s = sgn*s; |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2886 d(k) = -sgn; |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2887 x(1) = x(1) + s; |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2888 beta = s*x(1); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2889 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2890 ## Apply the transformation to A. |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2891 y = x'*A(k:n,:); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2892 A(k:n,:) = A(k:n,:) - x*(y/beta); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2893 endfor |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2894 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2895 ## Tidy up signs |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2896 for i = 1:n-1 |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2897 A(i,:) = d(i)*A(i,:); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2898 endfor |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2899 A(n,:) = A(n,:) * sign (randn); |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2900 B = A; |
0b9d23557506
gallery: fix randsvd by adding missing dependency qmult().
Carnë Draug <carandraug@octave.org>
parents:
20295
diff
changeset
|
2901 endfunction |
19226
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2902 |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2903 ## BIST testing for just a few functions to verify that the main gallery |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2904 ## dispatch function works. |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2905 %assert (gallery ("clement", 3), [0 1 0; 2 0 2; 0 1 0]) |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2906 %assert (gallery ("invhess", 2), [1 -1; 1 2]) |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2907 |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2908 ## Test input validation of main dispatch function only |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2909 %!error gallery () |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2910 %!error <NAME must be a string> gallery (123) |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2911 %!error <matrix binomial not implemented> gallery ("binomial") |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2912 %!error <unknown matrix with NAME foobar> gallery ("foobar") |
cdfc8bc9ab62
gallery.m: Add BIST tests for central dispatch function.
Rik <rik@octave.org>
parents:
19040
diff
changeset
|
2913 |
19944
941e782d0429
gallery: performance increase for minij matrix.
Carnë Draug <carandraug@octave.org>
parents:
19833
diff
changeset
|
2914 %!assert (gallery ("minij", 4), [1 1 1 1; 1 2 2 2; 1 2 3 3; 1 2 3 4]) |
941e782d0429
gallery: performance increase for minij matrix.
Carnë Draug <carandraug@octave.org>
parents:
19833
diff
changeset
|
2915 %!assert (gallery ("minij", 1), 1) |
941e782d0429
gallery: performance increase for minij matrix.
Carnë Draug <carandraug@octave.org>
parents:
19833
diff
changeset
|
2916 %!assert (gallery ("minij", 0), []) |
941e782d0429
gallery: performance increase for minij matrix.
Carnë Draug <carandraug@octave.org>
parents:
19833
diff
changeset
|
2917 %!assert (gallery ("minij", -1), []) |