comparison scripts/general/gradient.m @ 19833:9fc020886ae9

maint: Clean up m-files to follow Octave coding conventions. Try to trim long lines to < 80 chars. Use '##' for single line comments. Use '(...)' around tests for if/elseif/switch/while. Abut cell indexing operator '{' next to variable. Abut array indexing operator '(' next to variable. Use space between negation operator '!' and following expression. Use two newlines between endfunction and start of %!test or %!demo code. Remove unnecessary parens grouping between short-circuit operators. Remove stray extra spaces (typos) between variables and assignment operators. Remove stray extra spaces from ends of lines.
author Rik <rik@octave.org>
date Mon, 23 Feb 2015 14:54:39 -0800
parents 00e31f316a3a
children 7503499a252b
comparison
equal deleted inserted replaced
19832:a1acca0c2216 19833:9fc020886ae9
74 if (isnumeric (m)) 74 if (isnumeric (m))
75 [varargout{1:nargout_with_ans}] = matrix_gradient (m, varargin{:}); 75 [varargout{1:nargout_with_ans}] = matrix_gradient (m, varargin{:});
76 elseif (isa (m, "function_handle")) 76 elseif (isa (m, "function_handle"))
77 [varargout{1:nargout_with_ans}] = handle_gradient (m, varargin{:}); 77 [varargout{1:nargout_with_ans}] = handle_gradient (m, varargin{:});
78 elseif (ischar (m)) 78 elseif (ischar (m))
79 [varargout{1:nargout_with_ans}] = handle_gradient (str2func (m), varargin{:}); 79 [varargout{1:nargout_with_ans}] = handle_gradient (str2func (m), ...
80 varargin{:});
80 else 81 else
81 error ("gradient: first input must be an array or a function"); 82 error ("gradient: first input must be an array or a function");
82 endif 83 endif
83 84
84 endfunction 85 endfunction
177 if (numel (p0_size) != 2) 178 if (numel (p0_size) != 2)
178 error ("gradient: the second input argument should either be a vector or a matrix"); 179 error ("gradient: the second input argument should either be a vector or a matrix");
179 endif 180 endif
180 181
181 if (any (p0_size == 1)) 182 if (any (p0_size == 1))
182 p0 = p0 (:); 183 p0 = p0(:);
183 dim = 1; 184 dim = 1;
184 num_points = numel (p0); 185 num_points = numel (p0);
185 else 186 else
186 num_points = p0_size (1); 187 num_points = p0_size (1);
187 dim = p0_size (2); 188 dim = p0_size (2);
199 error ("gradient: incorrect number of spacing parameters"); 200 error ("gradient: incorrect number of spacing parameters");
200 endif 201 endif
201 202
202 if (isscalar (delta)) 203 if (isscalar (delta))
203 delta = repmat (delta, 1, dim); 204 delta = repmat (delta, 1, dim);
204 elseif (!isvector (delta)) 205 elseif (! isvector (delta))
205 error ("gradient: spacing values must be scalars or a vector"); 206 error ("gradient: spacing values must be scalars or a vector");
206 endif 207 endif
207 208
208 ## Calculate the gradient 209 ## Calculate the gradient
209 p0 = mat2cell (p0, num_points, ones (1, dim)); 210 p0 = mat2cell (p0, num_points, ones (1, dim));
210 varargout = cell (1, dim); 211 varargout = cell (1, dim);
211 for d = 1:dim 212 for d = 1:dim
212 s = delta (d); 213 s = delta(d);
213 df_dx = (f (p0{1:d-1}, p0{d}+s, p0{d+1:end}) 214 df_dx = (f (p0{1:d-1}, p0{d}+s, p0{d+1:end})
214 - f (p0{1:d-1}, p0{d}-s, p0{d+1:end})) ./ (2*s); 215 - f (p0{1:d-1}, p0{d}-s, p0{d+1:end})) ./ (2*s);
215 if (dim == 1) 216 if (dim == 1)
216 varargout{d} = reshape (df_dx, p0_size); 217 varargout{d} = reshape (df_dx, p0_size);
217 else 218 else