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