]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/logspace.m
update packages
[CreaPhase.git] / octave_packages / m / general / logspace.m
1 ## Copyright (C) 1993-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} logspace (@var{a}, @var{b})
21 ## @deftypefnx {Function File} {} logspace (@var{a}, @var{b}, @var{n})
22 ## @deftypefnx {Function File} {} logspace (@var{a}, pi, @var{n})
23 ## Return a row vector with @var{n} elements logarithmically spaced from
24 ## @tex
25 ## $10^{a}$ to $10^{b}$.
26 ## @end tex
27 ## @ifnottex
28 ## 10^@var{a} to 10^@var{b}.
29 ## @end ifnottex
30 ## If @var{n} is unspecified it defaults to 50.
31 ##
32 ## If @var{b} is equal to
33 ## @tex
34 ## $\pi$,
35 ## @end tex
36 ## @ifnottex
37 ## pi,
38 ## @end ifnottex
39 ## the points are between
40 ## @tex
41 ## $10^{a}$ and $\pi$,
42 ## @end tex
43 ## @ifnottex
44 ## 10^@var{a} and pi,
45 ## @end ifnottex
46 ## @emph{not}
47 ## @tex
48 ## $10^{a}$ and $10^{\pi}$,
49 ## @end tex
50 ## @ifnottex
51 ## 10^@var{a} and 10^pi,
52 ## @end ifnottex
53 ## in order to be compatible with the corresponding @sc{matlab} function.
54 ##
55 ## Also for compatibility with @sc{matlab}, return the second argument @var{b}
56 ## if fewer than two values are requested.
57 ## @seealso{linspace}
58 ## @end deftypefn
59
60 ## Author: jwe
61
62 function retval = logspace (base, limit, n = 50)
63
64   if (nargin != 2 && nargin != 3)
65     print_usage ();
66   endif
67
68   if (! (isscalar (base) && isscalar (limit) && isscalar (n)))
69     error ("logspace: arguments BASE, LIMIT, and N must be scalars");
70   endif
71
72   npoints = fix (n);
73
74   if (limit == pi)
75     limit = log10 (pi);
76   endif
77
78   retval = 10 .^ (linspace (base, limit, npoints));
79
80 endfunction
81
82
83 %!test
84 %! x1 = logspace (1, 2);
85 %! x2 = logspace (1, 2, 10.1);
86 %! x3 = logspace (1, -2, 10);
87 %! x4 = logspace (1, pi, 10);
88 %! assert (size (x1) == [1, 50] && x1(1) == 10 && x1(50) == 100);
89 %! assert (size (x2) == [1, 10] && x2(1) == 10 && x2(10) == 100);
90 %! assert (size (x3) == [1, 10] && x3(1) == 10 && x3(10) == 0.01);
91 %! assert (size (x4) == [1, 10] && x4(1) == 10 && abs (x4(10) - pi) < sqrt (eps));
92
93 %% Test input validation
94 %!error logspace ();
95 %!error logspace (1, 2, 3, 4);
96 %!error logspace ([1, 2; 3, 4], 5, 6);
97 %!error logspace (1, [1, 2; 3, 4], 6);
98 %!error logspace (1, 2, [1, 2; 3, 4]);
99