annotate scripts/general/gradient.m @ 31202:434b5a1b9498

isequal.m: Add ability to compare Java objects (bug #62930) * isequal.m: Add elseif clause to detect Java objects and use built-in Java "equals" method for comparison. Add BIST test for Java object comparison.
author Rik <rik@octave.org>
date Thu, 25 Aug 2022 15:35:25 -0700
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2000-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26691
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
7 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
14 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
19 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
20 ## 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: 7001
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
25
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @deftypefn {} {@var{dx} =} gradient (@var{m})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
28 ## @deftypefnx {} {[@var{dx}, @var{dy}, @var{dz}, @dots{}] =} gradient (@var{m})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
29 ## @deftypefnx {} {[@dots{}] =} gradient (@var{m}, @var{s})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
30 ## @deftypefnx {} {[@dots{}] =} gradient (@var{m}, @var{x}, @var{y}, @var{z}, @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
31 ## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
32 ## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{s})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
33 ## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{x}, @var{y}, @dots{})
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
34 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## Calculate the gradient of sampled data or a function.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
36 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## If @var{m} is a vector, calculate the one-dimensional gradient of @var{m}.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
38 ## If @var{m} is a matrix the gradient is calculated for each dimension.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ## @code{[@var{dx}, @var{dy}] = gradient (@var{m})} calculates the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
41 ## one-dimensional gradient for @var{x} and @var{y} direction if @var{m} is a
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
42 ## matrix. Additional return arguments can be use for multi-dimensional
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
43 ## matrices.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
44 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## A constant spacing between two points can be provided by the @var{s}
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## parameter. If @var{s} is a scalar, it is assumed to be the spacing for all
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## dimensions. Otherwise, separate values of the spacing can be supplied by
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
48 ## the @var{x}, @dots{} arguments. Scalar values specify an equidistant
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
49 ## spacing. Vector values for the @var{x}, @dots{} arguments specify the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
50 ## coordinate for that dimension. The length must match their respective
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
51 ## dimension of @var{m}.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
52 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
53 ## At boundary points a linear extrapolation is applied. Interior points
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
54 ## are calculated with the first approximation of the numerical gradient
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
55 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
56 ## @example
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
57 ## y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)).
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
58 ## @end example
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
59 ##
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
60 ## If the first argument @var{f} is a function handle, the gradient of the
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
61 ## function at the points in @var{x0} is approximated using central difference.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
62 ## For example, @code{gradient (@@cos, 0)} approximates the gradient of the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
63 ## cosine function in the point @math{x0 = 0}. As with sampled data, the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
64 ## spacing values between the points from which the gradient is estimated can
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
65 ## be set via the @var{s} or @var{dx}, @var{dy}, @dots{} arguments. By default
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
66 ## a spacing of 1 is used.
9141
c1fff751b5a8 Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
67 ## @seealso{diff, del2}
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
68 ## @end deftypefn
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
69
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
70 function varargout = gradient (m, varargin)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
71
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
72 if (nargin < 1)
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11588
diff changeset
73 print_usage ();
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
74 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
75
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
76 nargout_with_ans = max (1,nargout);
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
77 if (isnumeric (m))
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
78 [varargout{1:nargout_with_ans}] = matrix_gradient (m, varargin{:});
25803
23483673ba43 Use is_function_handle instead of isa (x, "function_handle").
Rik <rik@octave.org>
parents: 25054
diff changeset
79 elseif (is_function_handle (m))
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
80 [varargout{1:nargout_with_ans}] = handle_gradient (m, varargin{:});
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
81 elseif (ischar (m))
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
82 [varargout{1:nargout_with_ans}] = handle_gradient (str2func (m), ...
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
83 varargin{:});
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
84 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
85 error ("gradient: first input must be an array or a function");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
86 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
87
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
88 endfunction
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
89
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
90 function varargout = matrix_gradient (m, varargin)
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21580
diff changeset
91
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
92 transposed = false;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
93 if (isvector (m))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
94 ## make a row vector.
14872
c2dbdeaa25df maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents: 14868
diff changeset
95 transposed = (columns (m) == 1);
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
96 m = m(:).';
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
97 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
98
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
99 nd = ndims (m);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
100 sz = size (m);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
101 if (length (sz) > 1)
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
102 tmp = sz(1); sz(1) = sz(2); sz(2) = tmp;
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
103 endif
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
104
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
105 if (nargin > 2 && nargin != nd + 1)
26691
f2bb4f2093b9 Fix print_usage() messages in subfunctions (bug #55599).
Rik <rik@octave.org>
parents: 26376
diff changeset
106 print_usage ("gradient");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
107 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
108
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
109 ## cell d stores a spacing vector for each dimension
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
110 d = cell (1, nd);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
111 if (nargin == 1)
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
112 ## no spacing given - assume 1.0 for all dimensions
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
113 for i = 1:nd
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
114 d{i} = ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
115 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
116 elseif (nargin == 2)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
117 if (isscalar (varargin{1}))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
118 ## single scalar value for all dimensions
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
119 for i = 1:nd
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
120 d{i} = varargin{1} * ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
121 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
122 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
123 ## vector for one-dimensional derivative
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
124 d{1} = diff (varargin{1}(:));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
125 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
126 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
127 ## have spacing value for each dimension
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 27985
diff changeset
128 if (length (varargin) != nd)
11588
d5bd2766c640 style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
129 error ("gradient: dimensions and number of spacing values do not match");
11113
a8ac114ec9ab Stylefixes, replace end by endif.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10793
diff changeset
130 endif
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
131 for i = 1:nd
7120
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
132 if (isscalar (varargin{i}))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
133 d{i} = varargin{i} * ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
134 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
135 d{i} = diff (varargin{i}(:));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
136 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
137 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
138 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
139
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
140 m = shiftdim (m, 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
141 for i = 1:min (nd, nargout)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
142 mr = rows (m);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
143 mc = numel (m) / mr;
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
144 Y = zeros (size (m), class (m));
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
145
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
146 if (mr > 1)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
147 ## Top and bottom boundary.
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
148 Y(1,:) = diff (m(1:2, :)) / d{i}(1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
149 Y(mr,:) = diff (m(mr-1:mr, :) / d{i}(mr - 1));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
150 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
151
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
152 if (mr > 2)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
153 ## Interior points.
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
154 Y(2:mr-1,:) = ((m(3:mr,:) - m(1:mr-2,:))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
155 ./ kron (d{i}(1:mr-2) + d{i}(2:mr-1), ones (1, mc)));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
156 endif
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
157
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
158 ## turn multi-dimensional matrix in a way, that gradient
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
159 ## along x-direction is calculated first then y, z, ...
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
160
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
161 if (i == 1)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
162 varargout{i} = shiftdim (Y, nd - 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
163 m = shiftdim (m, nd - 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
164 elseif (i == 2)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
165 varargout{i} = Y;
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
166 m = shiftdim (m, 2);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
167 else
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
168 varargout{i} = shiftdim (Y, nd - i + 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
169 m = shiftdim (m, 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
170 endif
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
171 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
172
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
173 if (transposed)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
174 varargout{1} = varargout{1}.';
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
175 endif
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21580
diff changeset
176
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
177 endfunction
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
178
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
179 function varargout = handle_gradient (f, p0, varargin)
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21580
diff changeset
180
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
181 ## Input checking
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
182 p0_size = size (p0);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
183
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
184 if (numel (p0_size) != 2)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
185 error ("gradient: the second input argument should either be a vector or a matrix");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
186 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
187
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
188 if (any (p0_size == 1))
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
189 p0 = p0(:);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
190 dim = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
191 num_points = numel (p0);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
192 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
193 num_points = p0_size (1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
194 dim = p0_size (2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
195 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
196
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
197 if (length (varargin) == 0)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
198 delta = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
199 elseif (length (varargin) == 1 || length (varargin) == dim)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
200 try
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
201 delta = [varargin{:}];
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
202 catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
203 error ("gradient: spacing parameters must be scalars or a vector");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
204 end_try_catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
205 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
206 error ("gradient: incorrect number of spacing parameters");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
207 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
208
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
209 if (isscalar (delta))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
210 delta = repmat (delta, 1, dim);
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
211 elseif (! isvector (delta))
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
212 error ("gradient: spacing values must be scalars or a vector");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
213 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
214
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
215 ## Calculate the gradient
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
216 p0 = mat2cell (p0, num_points, ones (1, dim));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
217 varargout = cell (1, dim);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
218 for d = 1:dim
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
219 s = delta(d);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
220 df_dx = (f (p0{1:d-1}, p0{d}+s, p0{d+1:end})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
221 - f (p0{1:d-1}, p0{d}-s, p0{d+1:end})) ./ (2*s);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
222 if (dim == 1)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
223 varargout{d} = reshape (df_dx, p0_size);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
224 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
225 varargout{d} = df_dx;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
226 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
227 endfor
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21580
diff changeset
228
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
229 endfunction
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
230
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
231
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
232 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
233 %! data = [1, 2, 4, 2];
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
234 %! dx = gradient (data);
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
235 %! dx2 = gradient (data, 0.25);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
236 %! dx3 = gradient (data, [0.25, 0.5, 1, 3]);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
237 %! assert (dx, [1, 3/2, 0, -2]);
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
238 %! assert (dx2, [4, 6, 0, -8]);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
239 %! assert (dx3, [4, 4, 0, -1]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
240 %! assert (size_equal (data, dx));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
241
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
242 %!test
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
243 %! [Y,X,Z,U] = ndgrid (2:2:8,1:5,4:4:12,3:5:30);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
244 %! [dX,dY,dZ,dU] = gradient (X);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
245 %! assert (all (dX(:) == 1));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
246 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
247 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
248 %! assert (all (dU(:) == 0));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
249 %! [dX,dY,dZ,dU] = gradient (Y);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
250 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
251 %! assert (all (dY(:) == 2));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
252 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
253 %! assert (all (dU(:) == 0));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
254 %! [dX,dY,dZ,dU] = gradient (Z);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
255 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
256 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
257 %! assert (all (dZ(:) == 4));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
258 %! assert (all (dU(:) == 0));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
259 %! [dX,dY,dZ,dU] = gradient (U);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
260 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
261 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
262 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
263 %! assert (all (dU(:) == 5));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
264 %! assert (size_equal (dX, dY, dZ, dU, X, Y, Z, U));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
265 %! [dX,dY,dZ,dU] = gradient (U, 5.0);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
266 %! assert (all (dU(:) == 1));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
267 %! [dX,dY,dZ,dU] = gradient (U, 1.0, 2.0, 3.0, 2.5);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
268 %! assert (all (dU(:) == 2));
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
269
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
270 %!test
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
271 %! [Y,X,Z,U] = ndgrid (2:2:8,1:5,4:4:12,3:5:30);
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
272 %! [dX,dY,dZ,dU] = gradient (X+j*X);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
273 %! assert (all (dX(:) == 1+1j));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
274 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
275 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
276 %! assert (all (dU(:) == 0));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
277 %! [dX,dY,dZ,dU] = gradient (Y-j*Y);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
278 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
279 %! assert (all (dY(:) == 2-j*2));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
280 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
281 %! assert (all (dU(:) == 0));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
282 %! [dX,dY,dZ,dU] = gradient (Z+j*1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
283 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
284 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
285 %! assert (all (dZ(:) == 4));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
286 %! assert (all (dU(:) == 0));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
287 %! [dX,dY,dZ,dU] = gradient (U-j*1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
288 %! assert (all (dX(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
289 %! assert (all (dY(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
290 %! assert (all (dZ(:) == 0));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
291 %! assert (all (dU(:) == 5));
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
292 %! assert (size_equal (dX, dY, dZ, dU, X, Y, Z, U));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
293 %! [dX,dY,dZ,dU] = gradient (U, 5.0);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
294 %! assert (all (dU(:) == 1));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
295 %! [dX,dY,dZ,dU] = gradient (U, 1.0, 2.0, 3.0, 2.5);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
296 %! assert (all (dU(:) == 2));
13146
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
297
1ce5cd703af0 Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents: 11589
diff changeset
298 %!test
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
299 %! x = 0:10;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
300 %! f = @cos;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
301 %! df_dx = @(x) -sin (x);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
302 %! assert (gradient (f, x), df_dx (x), 0.2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
303 %! assert (gradient (f, x, 0.5), df_dx (x), 0.1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
304
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
305 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
306 %! xy = reshape (1:10, 5, 2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
307 %! f = @(x,y) sin (x) .* cos (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
308 %! df_dx = @(x, y) cos (x) .* cos (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
309 %! df_dy = @(x, y) -sin (x) .* sin (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
310 %! [dx, dy] = gradient (f, xy);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 20852
diff changeset
311 %! assert (dx, df_dx (xy (:, 1), xy (:, 2)), 0.1);
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 20852
diff changeset
312 %! assert (dy, df_dy (xy (:, 1), xy (:, 2)), 0.1);