Mercurial > hg > octave-terminal
changeset 13288:497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
* logspace.m: Simplify function by using default values for arguments.
Use isscalar instead of 'length (x) == 1' to be clear about what is being tested.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 07 Oct 2011 08:53:04 -0700 |
parents | 5c22edebf2e8 |
children | 1a6537dbce7b |
files | scripts/general/logspace.m |
diffstat | 1 files changed, 23 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/logspace.m +++ b/scripts/general/logspace.m @@ -59,44 +59,41 @@ ## Author: jwe -function retval = logspace (base, limit, n) +function retval = logspace (base, limit, n = 50) - if (nargin == 2) - npoints = 50; - elseif (nargin == 3) - if (length (n) == 1) - npoints = fix (n); - else - error ("logspace: arguments must be scalars"); - endif - else + if (nargin != 2 && nargin != 3) print_usage (); endif - if (length (base) == 1 && length (limit) == 1) - if (limit == pi) - limit = log10 (pi); - endif - retval = 10 .^ (linspace (base, limit, npoints)); - else - error ("logspace: arguments must be scalars"); + if (! (isscalar (base) && isscalar (limit) && isscalar (n))) + error ("logspace: arguments BASE, LIMIT, and N must be scalars"); endif + npoints = fix (n); + + if (limit == pi) + limit = log10 (pi); + endif + + retval = 10 .^ (linspace (base, limit, npoints)); + endfunction + %!test %! x1 = logspace (1, 2); -%! x2 = logspace (1, 2, 10); +%! x2 = logspace (1, 2, 10.1); %! x3 = logspace (1, -2, 10); %! x4 = logspace (1, pi, 10); -%! assert((size (x1) == [1, 50] && x1(1) == 10 && x1(50) == 100 -%! && size (x2) == [1, 10] && x2(1) == 10 && x2(10) == 100 -%! && size (x3) == [1, 10] && x3(1) == 10 && x3(10) == 0.01 -%! && size (x4) == [1, 10] && x4(1) == 10 && abs (x4(10) - pi) < sqrt (eps))); +%! assert (size (x1) == [1, 50] && x1(1) == 10 && x1(50) == 100); +%! assert (size (x2) == [1, 10] && x2(1) == 10 && x2(10) == 100); +%! assert (size (x3) == [1, 10] && x3(1) == 10 && x3(10) == 0.01); +%! assert (size (x4) == [1, 10] && x4(1) == 10 && abs (x4(10) - pi) < sqrt (eps)); +%% Test input validation +%!error logspace (); +%!error logspace (1, 2, 3, 4); %!error logspace ([1, 2; 3, 4], 5, 6); - -%!error logspace (); +%!error logspace (1, [1, 2; 3, 4], 6); +%!error logspace (1, 2, [1, 2; 3, 4]); -%!error logspace (1, 2, 3, 4); -