Mercurial > hg > octave-nkf
annotate scripts/general/gradient.m @ 14363:f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
* wavread.m, acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m,
asec.m, asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m,
csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, accumarray.m,
accumdim.m, bitcmp.m, bitget.m, bitset.m, blkdiag.m, cart2pol.m, cart2sph.m,
celldisp.m, chop.m, circshift.m, colon.m, common_size.m, cplxpair.m,
cumtrapz.m, curl.m, dblquad.m, deal.m, divergence.m, flipdim.m, fliplr.m,
flipud.m, genvarname.m, gradient.m, idivide.m, int2str.m, interp1.m,
interp1q.m, interp2.m, interp3.m, interpft.m, interpn.m, isa.m, isdir.m,
isequal.m, isequalwithequalnans.m, issquare.m, logspace.m, nargchk.m,
narginchk.m, nargoutchk.m, nextpow2.m, nthargout.m, num2str.m, pol2cart.m,
polyarea.m, postpad.m, prepad.m, profile.m, profshow.m, quadgk.m, quadv.m,
randi.m, rat.m, repmat.m, rot90.m, rotdim.m, shift.m, shiftdim.m, sph2cart.m,
structfun.m, trapz.m, triplequad.m, convhull.m, dsearch.m, dsearchn.m,
griddata3.m, griddatan.m, rectint.m, tsearchn.m, __makeinfo__.m, doc.m,
get_first_help_sentence.m, help.m, type.m, unimplemented.m, which.m, imread.m,
imwrite.m, dlmwrite.m, fileread.m, is_valid_file_id.m, strread.m, textread.m,
textscan.m, commutation_matrix.m, cond.m, condest.m, cross.m,
duplication_matrix.m, expm.m, housh.m, isdefinite.m, ishermitian.m,
issymmetric.m, logm.m, normest.m, null.m, onenormest.m, orth.m, planerot.m,
qzhess.m, rank.m, rref.m, trace.m, vech.m, ans.m, bincoeff.m, bug_report.m,
bzip2.m, comma.m, compare_versions.m, computer.m, edit.m, fileparts.m,
fullfile.m, getfield.m, gzip.m, info.m, inputname.m, isappdata.m, isdeployed.m,
ismac.m, ispc.m, isunix.m, list_primes.m, ls.m, mexext.m, namelengthmax.m,
news.m, orderfields.m, paren.m, recycle.m, rmappdata.m, semicolon.m,
setappdata.m, setfield.m, substruct.m, symvar.m, ver.m, version.m,
warning_ids.m, xor.m, fminbnd.m, fsolve.m, fzero.m, lsqnonneg.m, optimset.m,
pqpnonneg.m, sqp.m, matlabroot.m, __gnuplot_drawnow__.m,
__plt_get_axis_arg__.m, ancestor.m, cla.m, clf.m, close.m, colorbar.m,
colstyle.m, comet3.m, contourc.m, figure.m, gca.m, gcbf.m, gcbo.m, gcf.m,
ginput.m, graphics_toolkit.m, gtext.m, hggroup.m, hist.m, hold.m, isfigure.m,
ishghandle.m, ishold.m, isocolors.m, isonormals.m, isosurface.m, isprop.m,
legend.m, line.m, loglog.m, loglogerr.m, meshgrid.m, ndgrid.m, newplot.m,
orient.m, patch.m, plot3.m, plotyy.m, __print_parse_opts__.m, quiver3.m,
refreshdata.m, ribbon.m, semilogx.m, semilogxerr.m, semilogy.m, stem.m,
stem3.m, subplot.m, title.m, uigetfile.m, view.m, whitebg.m, compan.m, conv.m,
deconv.m, mkpp.m, mpoles.m, pchip.m, poly.m, polyaffine.m, polyder.m,
polyfit.m, polygcd.m, polyint.m, polyout.m, polyval.m, polyvalm.m, ppder.m,
ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, intersect.m,
ismember.m, powerset.m, setdiff.m, setxor.m, union.m, unique.m,
autoreg_matrix.m, bartlett.m, blackman.m, detrend.m, fftconv.m, fftfilt.m,
fftshift.m, freqz.m, hamming.m, hanning.m, ifftshift.m, sinc.m, sinetone.m,
sinewave.m, unwrap.m, bicg.m, bicgstab.m, gmres.m, gplot.m, nonzeros.m, pcg.m,
pcr.m, spaugment.m, spconvert.m, spdiags.m, speye.m, spfun.m, spones.m,
sprand.m, sprandsym.m, spstats.m, spy.m, svds.m, treelayout.m, bessel.m,
beta.m, betaln.m, factor.m, factorial.m, isprime.m, lcm.m, legendre.m,
nchoosek.m, nthroot.m, perms.m, pow2.m, primes.m, reallog.m, realpow.m,
realsqrt.m, hadamard.m, hankel.m, hilb.m, invhilb.m, magic.m, rosser.m,
vander.m, __finish__.m, center.m, cloglog.m, corr.m, cov.m, gls.m, histc.m,
iqr.m, kendall.m, kurtosis.m, logit.m, mahalanobis.m, mean.m, meansq.m,
median.m, mode.m, moment.m, ols.m, ppplot.m, prctile.m, probit.m, quantile.m,
range.m, ranks.m, run_count.m, runlength.m, skewness.m, spearman.m,
statistics.m, std.m, table.m, var.m, zscore.m, betacdf.m, betainv.m, betapdf.m,
betarnd.m, binocdf.m, binoinv.m, binopdf.m, binornd.m, cauchy_cdf.m,
cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m, chi2inv.m, chi2pdf.m,
chi2rnd.m, discrete_cdf.m, discrete_inv.m, discrete_pdf.m, discrete_rnd.m,
empirical_cdf.m, empirical_inv.m, empirical_pdf.m, empirical_rnd.m, expcdf.m,
expinv.m, exppdf.m, exprnd.m, fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m,
gaminv.m, gampdf.m, gamrnd.m, geocdf.m, geoinv.m, geopdf.m, geornd.m,
hygecdf.m, hygeinv.m, hygepdf.m, hygernd.m, kolmogorov_smirnov_cdf.m,
laplace_cdf.m, laplace_inv.m, laplace_pdf.m, laplace_rnd.m, logistic_cdf.m,
logistic_inv.m, logistic_pdf.m, logistic_rnd.m, logncdf.m, logninv.m,
lognpdf.m, lognrnd.m, nbincdf.m, nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m,
norminv.m, normpdf.m, normrnd.m, poisscdf.m, poissinv.m, poisspdf.m,
poissrnd.m, stdnormal_cdf.m, stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m,
tcdf.m, tinv.m, tpdf.m, trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m,
unifcdf.m, unifinv.m, unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m,
wblrnd.m, kolmogorov_smirnov_test.m, kruskal_wallis_test.m, base2dec.m,
bin2dec.m, blanks.m, cstrcat.m, deblank.m, dec2base.m, dec2bin.m, dec2hex.m,
findstr.m, hex2dec.m, index.m, isletter.m, mat2str.m, rindex.m, str2num.m,
strcat.m, strjust.m, strmatch.m, strsplit.m, strtok.m, strtrim.m, strtrunc.m,
substr.m, validatestring.m, demo.m, example.m, fail.m, speed.m, addtodate.m,
asctime.m, clock.m, ctime.m, date.m, datenum.m, datetick.m, datevec.m,
eomday.m, etime.m, is_leap_year.m, now.m:
Use Octave coding conventions in all m-file %!test blocks
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 13 Feb 2012 07:29:44 -0800 |
parents | 72c96de7a403 |
children | 5d3a684236b0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13146
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
20 ## @deftypefn {Function File} {@var{dx} =} gradient (@var{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
|
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 ## |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
28 ## Calculate the gradient of sampled data or a function. If @var{m} |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
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 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
34 ## matrix. Additional return arguments can be use for multi-dimensional |
5837 | 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 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
38 ## @var{s} parameter. If @var{s} is a scalar, it is assumed to be the spacing |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
39 ## for all dimensions. |
9015
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 |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
41 ## the @var{x}, @dots{} arguments. Scalar values specify an equidistant |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
42 ## spacing. |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
43 ## Vector values for the @var{x}, @dots{} arguments specify the coordinate for |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
44 ## that |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
45 ## dimension. The length must match their respective dimension of @var{m}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
47 ## At boundary points a linear extrapolation is applied. Interior points |
5837 | 48 ## are calculated with the first approximation of the numerical gradient |
49 ## | |
50 ## @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
|
51 ## y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)). |
5837 | 52 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
53 ## |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
54 ## 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
|
55 ## 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
|
56 ## difference. For example, @code{gradient (@@cos, 0)} approximates the |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
57 ## gradient of the cosine function in the point @math{x0 = 0}. As with |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
58 ## 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
|
59 ## gradient is estimated can be set via the @var{s} or @var{dx}, |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9015
diff
changeset
|
60 ## @var{dy}, @dots{} arguments. By default 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
|
61 ## @seealso{diff, del2} |
5837 | 62 ## @end deftypefn |
63 | |
64 ## Author: Kai Habel <kai.habel@gmx.de> | |
65 ## Modified: David Bateman <dbateman@free.fr> Added NDArray support | |
66 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
67 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
|
68 |
5838 | 69 if (nargin < 1) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
70 print_usage (); |
5837 | 71 endif |
72 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
73 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
|
74 if (ismatrix (m)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
75 [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
|
76 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
|
77 [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
|
78 elseif (ischar(m)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
79 [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
|
80 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
81 error ("gradient: first input must be an array or a function"); |
5837 | 82 endif |
83 | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
84 endfunction |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
85 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
86 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
|
87 transposed = false; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
88 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
|
89 ## 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
|
90 transposed = (size (m, 2) == 1); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
91 m = m(:).'; |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
92 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
93 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
94 nd = ndims (m); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 endif |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
99 |
5837 | 100 if (nargin > 2 && nargin != nd + 1) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
101 print_usage (); |
5837 | 102 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
103 |
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 ## cell d stores a spacing vector for each dimension |
5838 | 105 d = cell (1, nd); |
5837 | 106 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
|
107 ## no spacing given - assume 1.0 for all dimensions |
8507 | 108 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
|
109 d{i} = ones (sz(i) - 1, 1); |
5837 | 110 endfor |
111 elseif (nargin == 2) | |
112 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
|
113 ## single scalar value for all dimensions |
5838 | 114 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
|
115 d{i} = varargin{1} * ones (sz(i) - 1, 1); |
5837 | 116 endfor |
117 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
|
118 ## 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
|
119 d{1} = diff (varargin{1}(:)); |
5837 | 120 endif |
121 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
|
122 ## 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
|
123 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
|
124 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
|
125 endif |
8507 | 126 for i = 1:nd |
7120 | 127 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
|
128 d{i} = varargin{i} * ones (sz(i) - 1, 1); |
5837 | 129 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
|
130 d{i} = diff (varargin{i}(:)); |
5837 | 131 endif |
132 endfor | |
133 endif | |
134 | |
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 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 |
5837 | 141 if (mr > 1) |
8507 | 142 ## 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
|
143 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
|
144 Y(mr,:) = diff (m(mr-1:mr, :) / d{i}(mr - 1)); |
5837 | 145 endif |
146 | |
147 if (mr > 2) | |
8507 | 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,:)) |
9015
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
150 ./ kron (d{i}(1:mr-2) + d{i}(2:mr-1), ones (1, mc))); |
5837 | 151 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
|
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 ## 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
|
154 ## 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
|
155 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 else |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
163 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
|
164 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
|
165 endif |
5837 | 166 endfor |
167 | |
168 if (transposed) | |
169 varargout{1} = varargout{1}.'; | |
170 endif | |
171 endfunction | |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
172 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
173 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
|
174 ## Input checking |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
175 p0_size = size (p0); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
176 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
177 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
|
178 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
|
179 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
180 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
181 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
|
182 p0 = p0 (:); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
183 dim = 1; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
184 num_points = numel (p0); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
185 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
186 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
|
187 dim = p0_size (2); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
188 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
189 |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
190 if (length (varargin) == 0) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
191 delta = 1; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
192 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
|
193 try |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
194 delta = [varargin{:}]; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
195 catch |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
196 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
|
197 end_try_catch |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
198 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
199 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
|
200 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
201 |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
202 if (isscalar (delta)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
203 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
|
204 elseif (!isvector (delta)) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
205 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
|
206 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
207 |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
208 ## Calculate the gradient |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
209 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
|
210 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
|
211 for d = 1:dim |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
212 s = delta (d); |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
213 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
|
214 - 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
|
215 if (dim == 1) |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
216 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
|
217 else |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
218 varargout{d} = df_dx; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
219 endif |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
220 endfor |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
221 endfunction |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
222 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
223 |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
224 %!test |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
225 %! 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
|
226 %! 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
|
227 %! 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
|
228 %! 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
|
229 %! 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
|
230 %! 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
|
231 %! 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
|
232 %! 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
|
233 |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
234 %!test |
06cebb6c5dde
Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents:
8920
diff
changeset
|
235 %! [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
|
236 %! [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
|
237 %! assert (all (dX(:) == 1)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
238 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
239 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
240 %! 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
|
241 %! [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
|
242 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
243 %! assert (all (dY(:) == 2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
244 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
245 %! 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
|
246 %! [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
|
247 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
248 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
249 %! assert (all (dZ(:) == 4)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
250 %! 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
|
251 %! [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
|
252 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
253 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
254 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
255 %! assert (all (dU(:) == 5)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
256 %! 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
|
257 %! [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
|
258 %! 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
|
259 %! [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
|
260 %! 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
|
261 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
262 %!test |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
263 %! [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
|
264 %! [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
|
265 %! 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
|
266 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
267 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
268 %! assert (all (dU(:) == 0)); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
269 %! [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
|
270 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
271 %! 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
|
272 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
273 %! assert (all (dU(:) == 0)); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
274 %! [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
|
275 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
276 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
277 %! assert (all (dZ(:) == 4)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
278 %! assert (all (dU(:) == 0)); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
279 %! [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
|
280 %! assert (all (dX(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
281 %! assert (all (dY(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
282 %! assert (all (dZ(:) == 0)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
283 %! assert (all (dU(:) == 5)); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
284 %! assert (size_equal(dX, dY, dZ, dU, X, Y, Z, U)); |
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
285 %! [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
|
286 %! assert (all (dU(:) == 1)); |
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, 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
|
288 %! assert (all (dU(:) == 2)); |
13146
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
289 |
1ce5cd703af0
Fix bug for complex input for gradient (#34292)
Kai Habel <kai.habel@gmx.de>
parents:
11589
diff
changeset
|
290 %!test |
8601
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
291 %! x = 0:10; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
292 %! f = @cos; |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
293 %! 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
|
294 %! 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
|
295 %! 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
|
296 |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
297 %!test |
b297b86f4ad9
gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents:
8507
diff
changeset
|
298 %! 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
|
299 %! 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
|
300 %! 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
|
301 %! 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
|
302 %! [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
|
303 %! 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
|
304 %! 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
|
305 |