# HG changeset patch # User Rik # Date 1275341670 25200 # Node ID 1cd7c39a96eec432eeab9a8054a791204542b6e0 # Parent f5f9bc8e83fc558caf1c67caf6c9ef3653de1a97 legendre.m: Orient row vector correctly (bug #29997). Add input validation for negative values and %tests to check validation routines. diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,4 +1,9 @@ -2010-05-26 Rik +2010-05-31 Rik + * specfun/legendre.m: Orient row vector correctly (bug #29997). + Add input validation for negative values and %tests to check + validation routines. + +2010-05-30 Rik * sparse/svds.m: Overhaul code. Return smallest singular values if sigma == 0 (Bug #29721). diff --git a/scripts/specfun/legendre.m b/scripts/specfun/legendre.m --- a/scripts/specfun/legendre.m +++ b/scripts/specfun/legendre.m @@ -117,20 +117,20 @@ print_usage (); endif + if (!isscalar (n) || n < 0 || n != fix (n)) + error ("legendre: N must be a non-negative scalar integer"); + endif + + if (!isvector (x) || !isreal (x) || any (x < -1 | x > 1)) + error ("legendre: X must be real-valued vector in the range -1 <= X <= 1"); + endif + if (nargin == 3) normalization = lower (normalization); else normalization = "unnorm"; endif - if (! isscalar (n) || n < 0 || n != fix (n)) - error ("legendre: n must be a non-negative scalar integer"); - endif - - if (! isvector (x) || any (x < -1 || x > 1)) - error ("legendre: x must be vector in range -1 <= x <= 1"); - endif - switch (normalization) case "norm" scale = sqrt (n+0.5); @@ -139,9 +139,12 @@ case "unnorm" scale = 1; otherwise - error ("legendre: expecting normalization option to be \"norm\", \"sch\", or \"unnorm\""); + error ('legendre: expecting normalization option to be "norm", "sch", or "unnorm"'); endswitch + if (rows (x) != 1) + x = x'; + endif scale = scale * ones (1, numel (x)); ## Based on the recurrence relation below @@ -235,3 +238,15 @@ %!test %! result = legendre (0, 0:0.1:1); %! assert (result, full(ones(1,11))) + +%% Check correct invocation +%!error legendre (); +%!error legendre (1); +%!error legendre (1,2,3,4); +%!error legendre ([1, 2], [-1, 0, 1]); +%!error legendre (-1, [-1, 0, 1]); +%!error legendre (1.1, [-1, 0, 1]); +%!error legendre (1, [-1+i, 0, 1]); +%!error legendre (1, [-2, 0, 1]); +%!error legendre (1, [-1, 0, 2]); +%!error legendre (1, [-1, 0, 1], "badnorm");