Mercurial > hg > octave-terminal
changeset 13891:5180791b8d9e
magic.m: Use Octave spacing convention. Add more input validation tests.
* magic.m: Use Octave spacing convention. Add more input validation tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 19 Nov 2011 07:05:28 -0800 |
parents | 3a2f28c08fbd |
children | dd01f0bfd78d |
files | scripts/special-matrix/magic.m |
diffstat | 1 files changed, 18 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/special-matrix/magic.m +++ b/scripts/special-matrix/magic.m @@ -32,8 +32,8 @@ print_usage (); endif - if (n != floor (n) || n < 0 || n == 2) - error ("magic: N must be an positive integer not equal to 2"); + if (n != fix (n) || n < 0 || n == 2) + error ("magic: N must be a positive integer not equal to 2"); endif if (n == 0) @@ -45,7 +45,7 @@ shift = floor ((0:n*n-1)/n); c = mod ([1:n*n] - shift + (n-3)/2, n); r = mod ([n*n:-1:1] + 2*shift, n); - A (c*n+r+1) = 1:n*n; + A(c*n+r+1) = 1:n*n; A = reshape (A, n, n); elseif (mod (n, 4) == 0) @@ -64,7 +64,7 @@ A = magic (m); A = [A, A+2*m*m; A+3*m*m, A+m*m]; k = (m-1)/2; - if (k>1) + if (k > 1) I = 1:m; J = [2:k, n-k+2:n]; A([I,I+m],J) = A([I+m,I],J); @@ -78,11 +78,20 @@ endfunction + %!test %! for i=3:30 -%! A=magic(i); -%! assert(norm(diff([sum(diag(A)),sum(diag(flipud(A))),sum(A),sum(A')])),0) +%! A = magic (i); +%! assert (norm(diff([sum(diag(A)),sum(diag(flipud(A))),sum(A),sum(A')])),0); %! endfor -%!assert(isempty(magic(0))); -%!assert(magic(1),1); -%!error magic(2) + +%!assert (isempty (magic (0))) +%!assert (magic(1), 1) + +%% Test input validation +%!error magic () +%!error magic (1, 2) +%!error <N must be a positive integer not equal to 2> magic (1.5) +%!error <N must be a positive integer not equal to 2> magic (-1) +%!error <N must be a positive integer not equal to 2> magic (2) +