Mercurial > hg > octave-lyh
annotate scripts/general/gradient.m @ 9015:06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
coordinate now. Tests added.
author | Kai Habel <kai.habel@gmx.de> |
---|---|
date | Wed, 25 Mar 2009 14:33:44 -0400 |
parents | eb63fbe60fab |
children | 1bf0ce0930be |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2008, 2009 Kai Habel |
5837 | 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. | |
5837 | 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/>. | |
5837 | 18 |
19 ## -*- texinfo -*- | |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
20 ## @deftypefn {Function File} {@var{dx} =} gradient (@var{m}) |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{dx}, @var{dy}, @var{dz}, @dots{}] =} gradient (@var{m}) |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
22 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{m}, @var{s}) |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
23 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{m}, @var{x}, @var{y}, @var{z}, @dots{}) |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
24 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
25 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{s}) |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
26 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{x}, @var{y}, @dots{}) |
5837 | 27 ## |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
28 ## Calculate the gradient of sampled data, or of a function. If @var{m} |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
29 ## is a vector, calculate the one dimensional gradient of @var{m}. If |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
30 ## @var{m} is a matrix the gradient is calculated for each dimension. |
5837 | 31 ## |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
32 ## @code{[@var{dx}, @var{dy}] = gradient (@var{m})} calculates the one |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
33 ## dimensional gradient for @var{x} and @var{y} direction if @var{m} is a |
5837 | 34 ## matrix. Additional return arguments can be use for multi-dimensional |
35 ## matrices. | |
36 ## | |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
37 ## A constant spacing between two points can be provided by the |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
38 ## @var{s} parameter. If @var{s} is a scalar, it is assumed to be the spacing |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
39 ## for all dimensions. |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
40 ## Otherwise, separate values of the spacing can be supplied by |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
41 ## the @var{x}, @dots{} arguments. Scalar values specify an equidistant spacing. |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
42 ## Vector values for the @var{x}, @dots{} arguments specify the coordinate for that |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
43 ## dimension. The length must match their respective dimension of @var{m}. |
5837 | 44 ## |
45 ## At boundary points a linear extrapolation is applied. Interior points | |
46 ## are calculated with the first approximation of the numerical gradient | |
47 ## | |
48 ## @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
|
49 ## y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)). |
5837 | 50 ## @end example |
51 ## | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
52 ## If the first argument @var{f} is a function handle, the gradient of the |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
53 ## function at the points in @var{x0} is approximated using central |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
54 ## difference. For example, @code{gradient (@@cos, 0)} approximates the |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
55 ## gradient of the cosine function in the point @math{x0 = 0}. As with |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
56 ## sampled data, the spacing values between the points from which the |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
57 ## gradient is estimated can be set via the @var{s} or @var{dx}, |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
58 ## @var{dy}, @dots{} arguments. By default a spacing of 1 is used. |
5837 | 59 ## @end deftypefn |
60 | |
61 ## Author: Kai Habel <kai.habel@gmx.de> | |
62 ## Modified: David Bateman <dbateman@free.fr> Added NDArray support | |
63 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
64 function varargout = gradient (m, varargin) |
5837 | 65 |
5838 | 66 if (nargin < 1) |
5837 | 67 print_usage () |
68 endif | |
69 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
70 nargout_with_ans = max(1,nargout); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
71 if (ismatrix (m)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
72 [varargout{1:nargout_with_ans}] = matrix_gradient (m, varargin{:}); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
73 elseif (isa (m, "function_handle")) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
74 [varargout{1:nargout_with_ans}] = handle_gradient (m, varargin{:}); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
75 elseif (ischar(m)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
76 [varargout{1:nargout_with_ans}] = handle_gradient (str2func (m), varargin{:}); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
77 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
78 error ("gradient: first input must be an array or a function"); |
5837 | 79 endif |
80 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
81 endfunction |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
82 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
83 function varargout = matrix_gradient (m, varargin) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
84 transposed = false; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
85 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
|
86 ## make a row vector. |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
87 transposed = (size (m, 2) == 1); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
88 m = m(:)'; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
89 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
90 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
91 nd = ndims (m); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
92 sz = size (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
|
93 if (length(sz) > 1) |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
94 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
|
95 endif |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
96 |
5837 | 97 if (nargin > 2 && nargin != nd + 1) |
98 print_usage () | |
99 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
|
100 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
101 ## cell d stores a spacing vector for each dimension |
5838 | 102 d = cell (1, nd); |
5837 | 103 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
|
104 ## no spacing given - assume 1.0 for all dimensions |
8507 | 105 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
|
106 d{i} = ones (sz(i) - 1, 1); |
5837 | 107 endfor |
108 elseif (nargin == 2) | |
109 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
|
110 ## single scalar value for all dimensions |
5838 | 111 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
|
112 d{i} = varargin{1} * ones (sz(i) - 1, 1); |
5837 | 113 endfor |
114 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
|
115 ## 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
|
116 d{1} = diff (varargin{1}(:)); |
5837 | 117 endif |
118 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
|
119 ## have spacing value for each dimension |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
120 if (length(varargin) != nd) |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
121 error ("dimensions and number of spacing values do not match."); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
122 end |
8507 | 123 for i = 1:nd |
7120 | 124 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
|
125 d{i} = varargin{i} * ones (sz(i) - 1, 1); |
5837 | 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 d{i} = diff (varargin{i}(:)); |
5837 | 128 endif |
129 endfor | |
130 endif | |
131 | |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 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
|
136 Y = zeros (size (m), class (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
|
137 |
5837 | 138 if (mr > 1) |
8507 | 139 ## 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
|
140 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
|
141 Y(mr,:) = diff (m(mr-1:mr, :) / d{i}(mr - 1)); |
5837 | 142 endif |
143 | |
144 if (mr > 2) | |
8507 | 145 ## Interior points. |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
146 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
|
147 ./ kron (d{i}(1:mr-2) + d{i}(2:mr-1), ones (1, mc))); |
5837 | 148 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
|
149 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
150 ## 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
|
151 ## 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
|
152 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 else |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
160 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
|
161 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
|
162 endif |
5837 | 163 endfor |
164 | |
165 if (transposed) | |
166 varargout{1} = varargout{1}.'; | |
167 endif | |
168 endfunction | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
169 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
170 function varargout = handle_gradient (f, p0, varargin) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
171 ## Input checking |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
172 p0_size = size (p0); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
173 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
174 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
|
175 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
|
176 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
177 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
178 if (any (p0_size == 1)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
179 p0 = p0 (:); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
180 dim = 1; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
181 num_points = numel (p0); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
182 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
183 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
|
184 dim = p0_size (2); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
185 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
186 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
187 if (length (varargin) == 0) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
188 delta = 1; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
189 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
|
190 try |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
191 delta = [varargin{:}]; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
192 catch |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
193 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
|
194 end_try_catch |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
195 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
196 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
|
197 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
198 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
199 if (isscalar (delta)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
200 delta = repmat (delta, 1, dim); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
201 elseif (!isvector (delta)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
202 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
|
203 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
204 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
205 ## Calculate the gradient |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
206 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
|
207 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
|
208 for d = 1:dim |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
209 s = delta (d); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
210 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
|
211 - 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
|
212 if (dim == 1) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
213 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
|
214 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
215 varargout{d} = df_dx; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
216 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
217 endfor |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
218 endfunction |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
219 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
220 %!test |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
221 %! 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
|
222 %! 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
|
223 %! 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
|
224 %! 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
|
225 %! 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
|
226 %! 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
|
227 %! assert (dx3, [4, 4, 0, -1]); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
228 %! assert (size_equal(data, dx)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
229 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
230 %!test |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
231 %! [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
|
232 %! [dX,dY,dZ,dU] = gradient (X); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
233 %! assert (all(dX(:)==1)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
234 %! assert (all(dY(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
235 %! assert (all(dZ(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
236 %! assert (all(dU(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
237 %! [dX,dY,dZ,dU] = gradient (Y); |
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 (all(dX(:)==0)); |
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 (all(dY(:)==2)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
240 %! assert (all(dZ(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
241 %! assert (all(dU(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
242 %! [dX,dY,dZ,dU] = gradient (Z); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
243 %! assert (all(dX(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
244 %! assert (all(dY(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
245 %! assert (all(dZ(:)==4)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
246 %! assert (all(dU(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
247 %! [dX,dY,dZ,dU] = gradient (U); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
248 %! assert (all(dX(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
249 %! assert (all(dY(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
250 %! assert (all(dZ(:)==0)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
251 %! assert (all(dU(:)==5)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
252 %! assert (size_equal(dX, dY, dZ, dU, X, Y, Z, U)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
253 %! [dX,dY,dZ,dU] = gradient (U, 5.0); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
254 %! assert (all(dU(:)==1)); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
255 %! [dX,dY,dZ,dU] = gradient (U, 1.0, 2.0, 3.0, 2.5); |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
256 %! 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
|
257 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
258 %!test |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
259 %! x = 0:10; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
260 %! f = @cos; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
261 %! 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
|
262 %! 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
|
263 %! 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
|
264 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
265 %!test |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
266 %! 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
|
267 %! 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
|
268 %! 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
|
269 %! 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
|
270 %! [dx, dy] = gradient (f, xy); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
271 %! assert (dx, df_dx (xy (:, 1), xy (:, 2)), 0.1) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
272 %! assert (dy, df_dy (xy (:, 1), xy (:, 2)), 0.1) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
273 |