File: del2.m

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (341 lines) | stat: -rw-r--r-- 9,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
########################################################################
##
## Copyright (C) 2000-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{L} =} del2 (@var{M})
## @deftypefnx {} {@var{L} =} del2 (@var{M}, @var{h})
## @deftypefnx {} {@var{L} =} del2 (@var{M}, @var{dx}, @var{dy}, @dots{})
##
## Calculate the discrete Laplace
## @tex
## operator $( \nabla^2 )$.
## @end tex
## @ifnottex
## operator.
## @end ifnottex
##
## For a 2-dimensional matrix @var{M} this is defined as
## @tex
## $$L = {1 \over 4} \left( {d^2 \over dx^2} M(x,y) + {d^2 \over dy^2} M(x,y) \right)$$
## @end tex
## @ifnottex
##
## @example
## @group
##       1    / d^2            d^2         \
## L  = --- * | ---  M(x,y) +  ---  M(x,y) |
##       4    \ dx^2           dy^2        /
## @end group
## @end example
##
## @end ifnottex
## For N-dimensional arrays the sum in parentheses is expanded to include
## second derivatives over the additional higher dimensions.
##
## The spacing between evaluation points may be defined by @var{h}, which is a
## scalar defining the equidistant spacing in all dimensions.  Alternatively,
## the spacing in each dimension may be defined separately by @var{dx},
## @var{dy}, etc.  A scalar spacing argument defines equidistant spacing,
## whereas a vector argument can be used to specify variable spacing.  The
## length of the spacing vectors must match the respective dimension of
## @var{M}.  The default spacing value is 1.
##
## Dimensions with fewer than 3 data points are skipped.  Boundary points are
## calculated from the linear extrapolation of interior points.
##
## Example: Second derivative of 2*x^3
##
## @example
## @group
## f = @@(x) 2*x.^3;
## dd = @@(x) 12*x;
## x = 1:6;
## L = 4*del2 (f(x));
## assert (L, dd (x));
## @end group
## @end example
##
## @seealso{gradient, diff}
## @end deftypefn

function L = del2 (M, varargin)

  if (nargin < 1)
    print_usage ();
  endif

  nd = ndims (M);
  sz = size (M);
  dx = cell (1, nd);
  if (nargin == 1)
    for i = 1 : nd
      dx(i) = ones (sz(i), 1);
    endfor
  elseif (nargin == 2 && isscalar (varargin{1}))
    h = varargin{1};
    for i = 1 : nd
      dx(i) = h * ones (sz(i), 1);
    endfor
  elseif (numel (varargin) <= nd)
    ndx = numel (varargin);
    varargin(ndx+1:nd) = 1;   # Fill missing dims with 1.
    ## Reverse dx{1} and dx{2} as the X-dim is the 2nd dim of a meshgrid array
    varargin([1, 2]) = varargin([2, 1]);
    for i = 1 : nd
      arg = varargin{i};
      if (isscalar (arg))
        dx(i) = arg * ones (sz(i), 1);
      elseif (isvector (arg))
        if (length (arg) != sz(i))
          error ("del2: number of elements in spacing vector %d does not match dimension %d of M", i, i);
        endif
        dx(i) = diff (varargin{i})(:);
      else
        error ("del2: spacing element %d must be a scalar or vector", i);
      endif
    endfor
  else
    print_usage ();
  endif

  idx = cell (1, nd);
  idx(:) = ":";

  L = zeros (sz);
  for i = 1 : nd
    if (sz(i) >= 3)
      DD = zeros (sz);
      idx1 = idx2 = idx3 = idx;

      ## interior points
      idx1{i} = 1 : sz(i) - 2;
      idx2{i} = 2 : sz(i) - 1;
      idx3{i} = 3 : sz(i);
      szi = sz;
      szi(i) = 1;

      h1 = repmat (shiftdim (dx{i}(1 : sz(i) - 2), 1 - i), szi);
      h2 = repmat (shiftdim (dx{i}(2 : sz(i) - 1), 1 - i), szi);
      DD(idx2{:}) = ((M(idx1{:}) - M(idx2{:})) ./ h1 + ...
                     (M(idx3{:}) - M(idx2{:})) ./ h2) ./ (h1 + h2);

      ## left and right boundary
      if (sz(i) == 3)
        DD(idx1{:}) = DD(idx3{:}) = DD(idx2{:});
      else
        idx1{i} = 1;
        idx2{i} = 2;
        idx3{i} = 3;
        DD(idx1{:}) = (dx{i}(1) + dx{i}(2)) / dx{i}(2) * DD(idx2{:}) - ...
            dx{i}(1) / dx{i}(2) * DD(idx3{:});

        idx1{i} = sz(i);
        idx2{i} = sz(i) - 1;
        idx3{i} = sz(i) - 2;
        DD(idx1{:}) = (dx{i}(sz(i) - 1) + dx{i}(sz(i) - 2)) / ...
            dx{i}(sz(i) - 2) * DD(idx2{:}) - ...
            dx{i}(sz(i) - 1) / dx{i}(sz(i) - 2) * DD(idx3{:});
      endif

      L += DD;
    endif
  endfor

  L ./= nd;

endfunction


## 3x3 constant test
%!test
%! a = ones (3,3);
%! b = del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00]);

## 3x3 planar test
%!test
%! a = [1,2,3;2,3,4;3,4,5];
%! b = del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00]);

## 3x3 corner test
%!test
%! a = zeros (3,3);
%! a(1,1) = 1.0;
%! b = 2*del2 (a);
%! assert (b(:,1), [1.00;0.50;0.50]);
%! assert (b(:,2), [0.50;0.00;0.00]);
%! assert (b(:,3), [0.50;0.00;0.00]);
%! assert (b, flipud (2*del2 (flipud (a))));
%! assert (b, fliplr (2*del2 (fliplr (a))));
%! assert (b, flipud (fliplr (2*del2 (fliplr (flipud (a))))));

## 3x3 boundary test
%!test
%! a = zeros (3,3);
%! a(2,1)=1.0;
%! b = 2*del2 (a);
%! assert (b(:,1), [-1.00;-0.50;-1.00]);
%! assert (b(:,2), [0.00;0.50;0.00]);
%! assert (b(:,3), [0.00;0.50;0.00]);
%! assert (b, flipud (2*del2 (flipud (a))));
%! assert (b, fliplr (2*del2 (fliplr (a))));
%! assert (b, flipud (fliplr (2*del2 (fliplr (flipud (a))))));

## 3x3 center test
%!test
%! a = zeros (3,3);
%! a(2,2) = 1.0;
%! b = del2 (a);
%! assert (b(:,1), [0.00;-0.50;0.00]);
%! assert (b(:,2), [-0.50;-1.00;-0.50]);
%! assert (b(:,3), [0.00;-0.50;0.00]);

## 4x4 constant test
%!test
%! a = ones (4,4);
%! b = del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00;0.00]);
%! assert (b(:,4), [0.00;0.00;0.00;0.00]);

## 4x4 planar test
%!test
%! a = [1,2,3,4;2,3,4,5;3,4,5,6;4,5,6,7];
%! b = del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00;0.00]);
%! assert (b(:,4), [0.00;0.00;0.00;0.00]);

## 4x4 corner test
%!test
%! a = zeros (4,4);
%! a(1,1) = 1.0;
%! b = 2*del2 (a);
%! assert (b(:,1), [2.00;0.50;0.00;-0.50]);
%! assert (b(:,2), [0.50;0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00;0.00]);
%! assert (b(:,4), [-0.50;0.00;0.00;0.00]);
%! assert (b, flipud (2*del2 (flipud (a))));
%! assert (b, fliplr (2*del2 (fliplr (a))));
%! assert (b, flipud (fliplr (2*del2 (fliplr (flipud (a))))));

## 9x9 center test
%!test
%! a = zeros (9,9);
%! a(5,5) = 1.0;
%! b = 2*del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00]);
%! assert (b(:,3), [0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00]);
%! assert (b(:,4), [0.00;0.00;0.00;0.00;0.50;0.00;0.00;0.00;0.00]);
%! assert (b(:,5), [0.00;0.00;0.00;0.50;-2.00;0.50;0.00;0.00;0.00]);
%! assert (b(:,6), b(:,4));
%! assert (b(:,7), b(:,3));
%! assert (b(:,8), b(:,2));
%! assert (b(:,9), b(:,1));

## 9x9 boundary test
%!test
%! a = zeros (9,9);
%! a(1,5) = 1.0;
%! b = 2*del2 (a);
%! assert (b(1,:), [0.00,0.00,0.00,0.50,0.00,0.50,0.00,0.00,0.00]);
%! assert (b(2,:), [0.00,0.00,0.00,0.00,0.50,0.00,0.00,0.00,0.00]);
%! assert (b(3:9,:), zeros (7,9));
%! a(1,5) = 0.0;
%! a(5,1) = 1.0;
%! b = 2*del2 (a);
%! assert (b(:,1), [0.00;0.00;0.00;0.50;0.00;0.50;0.00;0.00;0.00]);
%! assert (b(:,2), [0.00;0.00;0.00;0.00;0.50;0.00;0.00;0.00;0.00]);
%! assert (b(:,3:9), zeros (9,7));

## 9x9 dh center test
%!test
%! a = zeros (9,9);
%! a(5,5) = 1.0;
%! b = 8*del2 (a,2);
%! assert (b(:,1:3), zeros (9,3));
%! assert (b(:,4), [0.00;0.00;0.00;0.00;0.50;0.00;0.00;0.00;0.00]);
%! assert (b(:,5), [0.00;0.00;0.00;0.50;-2.00;0.50;0.00;0.00;0.00]);
%! assert (b(:,6), b(:,4));
%! assert (b(:,7:9), zeros (9,3));

## 9x9 dx test
%!test
%! a = zeros (9,9);
%! a(5,5) = 1.0;
%! b = 4*del2 (a,2,1);
%! assert (b(1:3,:), zeros (3,9));
%! assert (b(4,:), [0.00;0.00;0.00;0.00;1.00;0.00;0.00;0.00;0.00]');
%! assert (b(5,:), [0.00;0.00;0.00;0.25;-2.5;0.25;0.00;0.00;0.00]');
%! assert (b(6,:), b(4,:));
%! assert (b(7:9,:), zeros (3,9));

## 9x9 dy test
%!test
%! a = zeros (9,9);
%! a(5,5) = 1.0;
%! b = 4*del2 (a,1,2);
%! assert (b(:,1:3), zeros (9,3));
%! assert (b(:,4), [0.00;0.00;0.00;0.00;1.00;0.00;0.00;0.00;0.00]);
%! assert (b(:,5), [0.00;0.00;0.00;0.25;-2.5;0.25;0.00;0.00;0.00]);
%! assert (b(:,6), b(:,4));
%! assert (b(:,7:9), zeros (9,3));

## 3-D test
%!test
%! a = zeros (9,9,9);
%! a(5,5,5) = 1.0;
%! b = 8*3*del2 (a,2);
%! assert (b(:,:,1:3), zeros (9,9,3));
%! assert (b(:,1:3,:), zeros (9,3,9));
%! assert (b(1:3,:,:), zeros (3,9,9));
%! assert (b(4:5,4,4), [0.0,0.0]');
%! assert (b(5,5,4), 1.00);
%! assert (b(4,4,5), 0.00);
%! assert (b(5,4,5), 1.00);
%! assert (b(5,5,5),-6.00);
%! assert (b, flip (b,1));
%! assert (b, flip (b,2));
%! assert (b, flip (b,3));

%!test <*51728>
%! x = linspace (-2*pi, 2*pi);
%! U = cos (x);
%! L = 4*del2 (U, x);

## Test input validation
%!error <Invalid call> del2 ()
%!error <Invalid call> del2 (1, 1, 2, 3)
%!error <in spacing vector 1> del2 (1, 2, [1 1])
%!error <in spacing vector 2> del2 (1, [1 1], 2)
%!error <must be a scalar or vector> del2 (1, ones (2,2), 2)