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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6630
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6630
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6630
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6630
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
18
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3407
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
34 ## @tex
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
35 ## $\pi$,
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
40 ## the points are between
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
47 ## @emph{not}
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
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
0fcc8d65b571 [project @ 2007-05-16 15:32:09 by jwe]
jwe
parents: 6629
diff changeset
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
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
58 ## @seealso{linspace}
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2847
diff changeset
59 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
60
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
61 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
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
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
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
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
66 print_usage ();
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
67 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
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
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
71 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
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
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
81 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
82
13288
497bb1cf7b15 logspace.m: Simplify function by using default values for arguments
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
83
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
84 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
87 %! x3 = logspace (1, -2, 10);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
100