annotate scripts/general/gradient.m @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents b297b86f4ad9
children 06cebb6c5dde
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8601
diff changeset
1 ## Copyright (C) 2000, 2006, 2007, 2008, 2009 Kai Habel
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
2 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
4 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## your option) any later version.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
9 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
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 ## 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
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
18
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
20 ## @deftypefn {Function File} {@var{x} =} gradient (@var{m})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
21 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @dots{}] =} gradient (@var{m})
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})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
23 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{m}, @var{dx}, @var{dy}, @dots{})
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})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
26 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{dx}, @var{dy}, @dots{})
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
30 ## @var{m} is a matrix the gradient is calculated for each row.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
31 ##
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
32 ## @code{[@var{x}, @var{y}] = gradient (@var{m})} calculates the one
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
33 ## dimensional gradient for each direction if @var{m} if @var{m} is a
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
34 ## matrix. Additional return arguments can be use for multi-dimensional
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
35 ## matrices.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
36 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
37 ## Spacing values between two points can be provided by the
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
38 ## @var{dx}, @var{dy} or @var{h} parameters. If @var{h} is supplied it
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 5838
diff changeset
39 ## is assumed to be the spacing in all directions. Otherwise, separate
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
40 ## values of the spacing can be supplied by the @var{dx}, etc variables.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
41 ## A scalar value specifies an equidistant spacing, while a vector value
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
42 ## can be used to specify a variable spacing. The length must match
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
43 ## their respective dimension of @var{m}.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
44 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
45 ## At boundary points a linear extrapolation is applied. Interior points
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
46 ## are calculated with the first approximation of the numerical gradient
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
47 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
48 ## @example
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
49 ## y'(i) = 1/(x(i+1)-x(i-1)) *(y(i-1)-y(i+1)).
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
50 ## @end example
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
59 ## @end deftypefn
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
60
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
61 ## Author: Kai Habel <kai.habel@gmx.de>
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
62 ## Modified: David Bateman <dbateman@free.fr> Added NDArray support
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
65
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
66 if (nargin < 1)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
67 print_usage ()
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
68 endif
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 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
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
79 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
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))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
86 ## make a column vector.
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);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
93 if (nargin > 2 && nargin != nd + 1)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
94 print_usage ()
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
95 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
96
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
97 d = cell (1, nd);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
98 if (nargin == 1)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
99 for i = 1:nd
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
100 d{i} = ones (sz(i), 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
101 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
102 elseif (nargin == 2)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
103 if (isscalar (varargin{1}))
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
104 for i = 1:nd
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
105 d{i} = varargin{1} * ones (sz(i), 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
106 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
107 else
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
108 for i = 1:nd
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
109 d{i} = varargin{1};
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
110 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
111 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
112 else
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
113 for i = 1:nd
7120
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
114 if (isscalar (varargin{i}))
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
115 ## Why the hell did Matlab decide to swap these two values?
7120
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
116 if (i == 1)
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
117 d{2} = varargin{1} * ones (sz(2), 1);
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
118 elseif (i == 2)
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
119 d{1} = varargin{2} * ones (sz(1), 1);
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
120 else
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
121 d{i} = varargin{i} * ones (sz(i), 1);
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
122 endif
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
123 else
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
124 ## Why the hell did Matlab decide to swap these two values?
7120
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
125 if (i == 1)
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
126 d{2} = varargin{1};
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
127 elseif (i == 2)
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
128 d{1} = varargin{2};
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
129 else
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
130 d{i} = varargin{i};
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
131 endif
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
132 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
133 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
134 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
135
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
136 for i = 1:max (2, min (nd, nargout))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
137 mr = sz(i);
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
138 mc = prod ([sz(1:i-1), sz(i+1:nd)]);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
139 Y = zeros (size (m), class (m));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
140
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
141 if (mr > 1)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
142 ## Top and bottom boundary.
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
143 Y(1,:) = diff (m(1:2,:)) / d{i}(1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
144 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
145 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
146
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
147 if (mr > 2)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
148 ## Interior points.
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
149 Y(2:mr-1,:) = ((m(3:mr,:) - m(1:mr-2,:))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
150 ./ 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
151 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
152 varargout{i} = ipermute (Y, [i:nd,1:i-1]);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
153 m = permute (m, [2:nd,1]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
154 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
155
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
156 ## Why the hell did Matlab decide to swap these two values?
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
157 tmp = varargout{1};
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
158 varargout{1} = varargout{2};
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
159 varargout{2} = tmp;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
160
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
161 if (transposed)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
162 varargout{1} = varargout{1}.';
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
163 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
164 endfunction
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
165
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
166 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
167 ## Input checking
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
168 p0_size = size (p0);
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 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
171 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
172 endif
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 (any (p0_size == 1))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
175 p0 = p0 (:);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
176 dim = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
177 num_points = numel (p0);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
178 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
179 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
180 dim = p0_size (2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
181 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
182
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
183 if (length (varargin) == 0)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
184 delta = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
185 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
186 try
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
187 delta = [varargin{:}];
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
188 catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
189 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
190 end_try_catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
191 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
192 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
193 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
194
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
195 if (isscalar (delta))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
196 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
197 elseif (!isvector (delta))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
198 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
199 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
200
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
201 ## Calculate the gradient
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
202 p0 = mat2cell (p0, num_points, ones (1, dim));
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
203 varargout = cell (1,dim);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
204 for d = 1:dim
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
205 s = delta (d);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
206 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
207 - 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
208 if (dim == 1)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
209 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
210 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
211 varargout{d} = df_dx;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
212 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
213 endfor
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
214 endfunction
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
215
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
216 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
217 %! 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
218 %! dx = gradient (data);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
219 %! assert (dx, [1, 3/2, 0, -2]);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
220
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
221 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
222 %! x = 0:10;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
223 %! f = @cos;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
224 %! 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
225 %! 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
226 %! 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
227
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
228 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
229 %! 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
230 %! 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
231 %! 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
232 %! 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
233 %! [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
234 %! 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
235 %! 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
236