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