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