]> Creatis software - CreaPhase.git/blob - octave_packages/m/special-matrix/wilkinson.m
update packages
[CreaPhase.git] / octave_packages / m / special-matrix / wilkinson.m
1 ## Copyright (C) 1999-2012 Peter Ekberg
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} wilkinson (@var{n})
21 ## Return the Wilkinson matrix of order @var{n}.  Wilkinson matrices are
22 ## symmetric and tridiagonal with pairs of nearly, but not exactly, equal
23 ## eigenvalues.  They are useful in testing the behavior and performance
24 ## of eigenvalue solvers.
25 ##
26 ## @seealso{rosser, eig}
27 ## @end deftypefn
28
29 ## Author: Peter Ekberg
30 ##         (peda)
31
32 function retval = wilkinson (n)
33
34   if (nargin != 1)
35     print_usage ();
36   endif
37
38   if (! (isscalar (n) && n >= 0 && (n == fix (n))))
39     error ("wilkinson: N must be a non-negative integer");
40   endif
41
42   side = ones (n-1, 1);
43   center = abs (-(n-1)/2:(n-1)/2);
44   retval = diag (side, -1) + diag (center) + diag (side, 1);
45
46 endfunction
47
48
49 %!assert (wilkinson (0), [])
50 %!assert (wilkinson (1), 0)
51 %!assert (wilkinson (2), [0.5,1;1,0.5])
52 %!assert (wilkinson (3), [1,1,0;1,0,1;0,1,1])
53 %!assert (wilkinson (4), [1.5,1,0,0;1,0.5,1,0;0,1,0.5,1;0,0,1,1.5])
54
55 %% Test input validation
56 %!error wilkinson ()
57 %!error wilkinson (1,2)
58 %!error <N must be a non-negative integer> wilkinson (ones (2))
59 %!error <N must be a non-negative integer> wilkinson (-1)
60 %!error <N must be a non-negative integer> wilkinson (1.5)
61