7017
|
1 ## Copyright (C) 2000, 2006, 2007 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 -*- |
|
20 ## @deftypefn {Function File} {@var{x} = } gradient (@var{M}) |
|
21 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @dots{}] = } gradient (@var{M}) |
|
22 ## @deftypefnx {Function File} {[@dots{}] = } gradient (@var{M}, @var{s}) |
|
23 ## @deftypefnx {Function File} {[@dots{}] = } gradient (@var{M}, @var{dx}, @var{dy}, @dots{}) |
|
24 ## |
|
25 ## Calculates the gradient. @code{@var{x} = gradient (@var{M})} |
|
26 ## calculates the one dimensional gradient if @var{M} is a vector. If |
|
27 ## @var{M} is a matrix the gradient is calculated for each row. |
|
28 ## |
|
29 ## @code{[@var{x}, @var{y}] = gradient (@var{M})} calculates the one |
|
30 ## dimensional gradient for each direction if @var{M} if @var{M} is a |
|
31 ## matrix. Additional return arguments can be use for multi-dimensional |
|
32 ## matrices. |
|
33 ## |
|
34 ## Spacing values between two points can be provided by the |
|
35 ## @var{dx}, @var{dy} or @var{h} parameters. If @var{h} is supplied it |
7001
|
36 ## is assumed to be the spacing in all directions. Otherwise, separate |
5837
|
37 ## values of the spacing can be supplied by the @var{dx}, etc variables. |
|
38 ## A scalar value specifies an equidistant spacing, while a vector value |
|
39 ## can be used to specify a variable spacing. The length must match |
|
40 ## their respective dimension of @var{M}. |
|
41 ## |
|
42 ## At boundary points a linear extrapolation is applied. Interior points |
|
43 ## are calculated with the first approximation of the numerical gradient |
|
44 ## |
|
45 ## @example |
|
46 ## y'(i) = 1/(x(i+1)-x(i-1)) *(y(i-1)-y(i+1)). |
|
47 ## @end example |
|
48 ## |
|
49 ## @end deftypefn |
|
50 |
|
51 ## Author: Kai Habel <kai.habel@gmx.de> |
|
52 ## Modified: David Bateman <dbateman@free.fr> Added NDArray support |
|
53 |
|
54 function [varargout] = gradient (M, varargin) |
|
55 |
5838
|
56 if (nargin < 1) |
5837
|
57 print_usage () |
|
58 endif |
|
59 |
|
60 transposed = false; |
|
61 if (isvector (M)) |
|
62 ## make a column vector |
5838
|
63 transposed = (size (M, 2) == 1); |
5837
|
64 M = M(:)'; |
|
65 endif |
|
66 |
|
67 nd = ndims (M); |
|
68 sz = size (M); |
|
69 if (nargin > 2 && nargin != nd + 1) |
|
70 print_usage () |
|
71 endif |
|
72 |
5838
|
73 d = cell (1, nd); |
5837
|
74 if (nargin == 1) |
|
75 for i=1:nd |
5838
|
76 d{i} = ones (sz(i), 1); |
5837
|
77 endfor |
|
78 elseif (nargin == 2) |
|
79 if (isscalar (varargin{1})) |
5838
|
80 for i = 1:nd |
|
81 d{i} = varargin{1} * ones (sz(i), 1); |
5837
|
82 endfor |
|
83 else |
5838
|
84 for i = 1:nd |
5837
|
85 d{i} = varargin{1}; |
|
86 endfor |
|
87 endif |
|
88 else |
|
89 for i=1:nd |
|
90 if (isscalar (varargin{1})) |
5838
|
91 d{i} = varargin{i} * ones (sz(i), 1); |
5837
|
92 else |
|
93 d{i} = varargin{i}; |
|
94 endif |
|
95 endfor |
|
96 |
|
97 ## Why the hell did matlab decide to swap these two values? |
|
98 tmp = d{1}; |
|
99 d{1} = d{2}; |
|
100 d{2} = tmp; |
|
101 endif |
|
102 |
5838
|
103 for i = 1:max (2, min (nd, nargout)) |
5837
|
104 mr = sz(i); |
5838
|
105 mc = prod ([sz(1:i-1), sz(i+1:nd)]); |
|
106 Y = zeros (size (M), class (M)); |
5837
|
107 |
|
108 if (mr > 1) |
|
109 ## top and bottom boundary |
5838
|
110 Y(1,:) = diff (M(1:2,:)) / d{i}(1); |
|
111 Y(mr,:) = diff (M(mr-1:mr,:)) / d{i}(mr-1); |
5837
|
112 endif |
|
113 |
|
114 if (mr > 2) |
|
115 ## interior points |
5838
|
116 Y(2:mr-1,:) = (M(3:mr,:) .- M(1:mr-2,:)) ... |
|
117 ./ kron (d{i}(1:mr-2) .+ d{i}(2:mr-1), ones (1, mc)); |
5837
|
118 endif |
|
119 varargout{i} = ipermute (Y, [i:nd,1:i-1]); |
|
120 M = permute (M, [2:nd,1]); |
|
121 endfor |
|
122 |
|
123 ## Why the hell did matlab decide to swap these two values? |
|
124 tmp = varargout{1}; |
|
125 varargout{1} = varargout{2}; |
|
126 varargout{2} = tmp; |
|
127 |
|
128 if (transposed) |
|
129 varargout{1} = varargout{1}.'; |
|
130 endif |
|
131 endfunction |