comparison scripts/general/gradient.m @ 5837:55404f3b0da1

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