Mercurial > hg > octave-nkf
diff scripts/special-matrix/hilb.m @ 13872:779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
* hilb.m: 10% speedup by using in-place accumulation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 16 Nov 2011 20:49:04 -0800 |
parents | 4d777e05d47c |
children | 72c96de7a403 |
line wrap: on
line diff
--- a/scripts/special-matrix/hilb.m +++ b/scripts/special-matrix/hilb.m @@ -54,28 +54,26 @@ function retval = hilb (n) - if (nargin != 1) print_usage (); + elseif (! isscalar (n)) + error ("hilb: N must be a scalar integer"); endif - nmax = length (n); - if (nmax == 1) - retval = zeros (n); - tmp = 1:n; - for i = 1:n - retval (i, :) = 1.0 ./ (tmp + (i - 1)); - endfor - else - error ("hilb: expecting scalar argument, found something else"); - endif + retval = zeros (n); + tmp = 1:n; + for i = 1:n + retval(i, :) = 1.0 ./ tmp; + tmp++; + endfor endfunction -%!assert((hilb (2) == [1, 1/2; 1/2, 1/3] -%! && hilb (3) == [1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5])); + +%!assert (hilb (2), [1, 1/2; 1/2, 1/3]) +%!assert (hilb (3), [1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5]) -%!error hilb (); +%!error hilb () +%!error hilb (1, 2) +%!error <N must be a scalar integer> hilb (ones(2)) -%!error hilb (1, 2); -