Mercurial > hg > octave-nkf
annotate scripts/general/logspace.m @ 20828:a3b9ee5c040a
Replace bsxfun with broadcasting for performance with complex inputs (bug #38628).
cumtrapz.m, quadgk.m, trapz.m, center.m, zscore.m: Replace bsxfun with
broadcasting for performance where inputs might be complex.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 12 Oct 2015 21:28:32 -0700 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1993-2015 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
245 | 18 |
3369 | 19 ## -*- texinfo -*- |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {} logspace (@var{a}, @var{b}) |
14504
ecd4b048f98c
doc: Fix typo in logspace.m docstring (bug #36024)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
21 ## @deftypefnx {Function File} {} logspace (@var{a}, @var{b}, @var{n}) |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## @deftypefnx {Function File} {} logspace (@var{a}, pi, @var{n}) |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## Return a row vector with @var{n} elements logarithmically spaced from |
3369 | 24 ## @tex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
25 ## $10^{a}$ to $10^{b}$. |
3369 | 26 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
27 ## @ifnottex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## 10^@var{a} to 10^@var{b}. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
29 ## @end ifnottex |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
30 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## If @var{n} is unspecified it defaults to 50. |
3426 | 32 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
33 ## If @var{b} is equal to |
3369 | 34 ## @tex |
35 ## $\pi$, | |
36 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
37 ## @ifnottex |
3369 | 38 ## pi, |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
39 ## @end ifnottex |
3369 | 40 ## the points are between |
41 ## @tex | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 ## $10^{a}$ and $\pi$, |
3369 | 43 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
44 ## @ifnottex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 ## 10^@var{a} and pi, |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
46 ## @end ifnottex |
3369 | 47 ## @emph{not} |
48 ## @tex | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 ## $10^{a}$ and $10^{\pi}$, |
3369 | 50 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
51 ## @ifnottex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 ## 10^@var{a} and 10^pi, |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
53 ## @end ifnottex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 ## in order to be compatible with the corresponding @sc{matlab} function. |
6630 | 55 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 ## Also for compatibility with @sc{matlab}, return the second argument @var{b} |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 ## if fewer than two values are requested. |
5642 | 58 ## @seealso{linspace} |
3369 | 59 ## @end deftypefn |
4 | 60 |
2314 | 61 ## Author: jwe |
62 | |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
63 function retval = logspace (base, limit, n = 50) |
4 | 64 |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
65 if (nargin != 2 && nargin != 3) |
6046 | 66 print_usage (); |
4 | 67 endif |
68 | |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
69 if (! (isscalar (base) && isscalar (limit) && isscalar (n))) |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
70 error ("logspace: arguments BASE, LIMIT, and N must be scalars"); |
4 | 71 endif |
72 | |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
73 npoints = fix (n); |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
74 |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
75 if (limit == pi) |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
76 limit = log10 (pi); |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
77 endif |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
78 |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
79 retval = 10 .^ (linspace (base, limit, npoints)); |
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
80 |
4 | 81 endfunction |
7411 | 82 |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
83 |
7411 | 84 %!test |
85 %! x1 = logspace (1, 2); | |
13288
497bb1cf7b15
logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
86 %! x2 = logspace (1, 2, 10.1); |
7411 | 87 %! x3 = logspace (1, -2, 10); |
88 %! x4 = logspace (1, pi, 10); | |
15795
9a10fa38ef01
logspace.m: fix tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14506
diff
changeset
|
89 %! assert (size (x1) == [1, 50] && abs (x1(1) - 10) < eps && abs (x1(50) - 100) < eps); |
9a10fa38ef01
logspace.m: fix tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14506
diff
changeset
|
90 %! assert (size (x2) == [1, 10] && abs (x2(1) - 10) < eps && abs (x2(10) - 100) < eps); |
9a10fa38ef01
logspace.m: fix tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14506
diff
changeset
|
91 %! assert (size (x3) == [1, 10] && abs (x3(1) - 10) < eps && abs (x3(10) - 0.01) < eps); |
9a10fa38ef01
logspace.m: fix tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14506
diff
changeset
|
92 %! assert (size (x4) == [1, 10] && abs (x4(1) - 10) < eps && abs (x4(10) - pi) < sqrt (eps)); |
7411 | 93 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
94 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %!error logspace () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %!error logspace (1, 2, 3, 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!error logspace ([1, 2; 3, 4], 5, 6) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!error logspace (1, [1, 2; 3, 4], 6) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %!error logspace (1, 2, [1, 2; 3, 4]) |
7411 | 100 |