annotate scripts/legacy/vectorize.m @ 28931:c5b1bbb95a66

maint: start %!demo or %!test blocks 2 newlines after endfunction. * vectorize.m, ismethod.m, optimset.m, ostreamtube.m, streamribbon.m, streamtube.m, graphics_toolkit.m, deconv.m, gmres.m, tfqmr.m: Start %!demo or %!test blocks 2 newlines after endfunction.
author Rik <rik@octave.org>
date Wed, 14 Oct 2020 14:38:00 -0700
parents de5f2f9a64ff
children 6e460773bdda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28456
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 ########################################################################
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## Copyright (C) 2020 The Octave Project Developers
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## This file is part of Octave.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## (at your option) any later version.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 ## GNU General Public License for more details.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 ########################################################################
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 ## -*- texinfo -*-
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 ## @deftypefn {} {} vectorize (@var{fun})
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 ## Create a vectorized version of the anonymous function or expression
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 ## @var{fun} by replacing all occurrences of @code{*}, @code{/}, etc.,
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 ## with @code{.*}, @code{./}, etc.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 ##
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 ## Note that the transformation is extremely simplistic. Use of this
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 ## function is strongly discouraged. It may be removed from a future
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 ## version of Octave.
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 ## @end deftypefn
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 ## The following function was translated directly from the original C++
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 ## version. Yes, it will be slow, but its use is strongly discouraged
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 ## anyway, and most expressions will probably be short. It may also be
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 ## buggy. Well, don't use this function!
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 function retval = vectorize (fun)
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43
28458
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
44 persistent warned = false;
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
45 if (! warned)
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
46 warned = true;
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
47 warning ("Octave:legacy-function",
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
48 "vectorize is unreliable; its use is strongly discouraged\n");
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
49 endif
ef8cf8dda0ba warn if legacy inline, genvarname or vectorize functions are used
John W. Eaton <jwe@octave.org>
parents: 28456
diff changeset
50
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
51 if (nargin < 1)
28456
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 print_usage ();
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 endif
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 if (isa (fun, "function_handle"))
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 finfo = functions (fun);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 if (! strcmp (finfo.type, "anonymous"))
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 error ("vectorize: FUN must be a string or anonymous function handle");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 endif
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 expr = finfo.function;
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 idx = index (expr, ")");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 args = expr(1:idx);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 expr = expr(idx+1:end);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 new_expr = __vectorize__ (expr);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 retval = str2func ([args, new_expr]);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 elseif (ischar (fun))
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 retval = __vectorize__ (fun);
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 else
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 error ("vectorize: FUN must be a string or anonymous function handle");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 endif
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 endfunction
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
28931
c5b1bbb95a66 maint: start %!demo or %!test blocks 2 newlines after endfunction.
Rik <rik@octave.org>
parents: 28891
diff changeset
73
28456
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 %!assert (vectorize ("x.^2 + 1"), "x.^2 + 1")
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 %!test
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 %! fh = @(x) x.^2 + 1;
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 %! finfo = functions (vectorize (fh));
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 %! assert (finfo.function, "@(x) x .^ 2 + 1");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 %!assert (vectorize ("1e-3*y + 2e4*z"), "1e-3.*y + 2e4.*z")
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 %!test
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 %! fh = @(x, y, z) 1e-3*y + 2e4*z;
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 %! finfo = functions (vectorize (fh));
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 %! assert (finfo.function, "@(x, y, z) 1e-3 .* y + 2e4 .* z");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 %!assert (vectorize ("2**x^5"), "2.**x.^5")
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 ## Note that ** is transformed to ^ by the code that prints the parse
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 ## tree. I don't care too much about that...
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 %!test
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 %! fh = @(x) 2**x^5;
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 %! finfo = functions (vectorize (fh));
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 %! assert (finfo.function, "@(x) 2 .^ x .^ 5");
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28458
diff changeset
95 %!error <Invalid call> vectorize ()
28456
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 %!error <FUN must be a string or anonymous function handle> vectorize (1)
53d8e7ca99c5 revive legacy vectorize function for strings and function handles
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97