Mercurial > octave-nkf
annotate scripts/general/divergence.m @ 19867: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 | 4197fc428c7d |
children | 7503499a252b |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2009-2015 Kai Habel |
11428 | 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 3 of the License, or (at | |
8 ## your option) 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, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {@var{div} =} divergence (@var{x}, @var{y}, @var{z}, @var{fx}, @var{fy}, @var{fz}) |
11428 | 21 ## @deftypefnx {Function File} {@var{div} =} divergence (@var{fx}, @var{fy}, @var{fz}) |
22 ## @deftypefnx {Function File} {@var{div} =} divergence (@var{x}, @var{y}, @var{fx}, @var{fy}) | |
23 ## @deftypefnx {Function File} {@var{div} =} divergence (@var{fx}, @var{fy}) | |
11575
d6619410e79c
Spellcheck documentation before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11563
diff
changeset
|
24 ## Calculate divergence of a vector field given by the arrays @var{fx}, |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
25 ## @var{fy}, and @var{fz} or @var{fx}, @var{fy} respectively. |
11428 | 26 ## @tex |
27 ## $$ | |
28 ## div F(x,y,z) = \partial_x{F} + \partial_y{F} + \partial_z{F} | |
29 ## $$ | |
30 ## @end tex | |
31 ## @ifnottex | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
32 ## |
11428 | 33 ## @example |
34 ## @group | |
35 ## d d d | |
36 ## div F(x,y,z) = -- F(x,y,z) + -- F(x,y,z) + -- F(x,y,z) | |
37 ## dx dy dz | |
38 ## @end group | |
39 ## @end example | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
40 ## |
11428 | 41 ## @end ifnottex |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 ## The coordinates of the vector field can be given by the arguments @var{x}, |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
43 ## @var{y}, @var{z} or @var{x}, @var{y} respectively. |
11428 | 44 ## |
12520
ad05e1547398
Add function chop to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
45 ## @seealso{curl, gradient, del2, dot} |
11428 | 46 ## @end deftypefn |
47 | |
48 ## Author: Kai Habel <kai.habel@gmx.de> | |
49 | |
50 function retval = divergence (varargin) | |
51 | |
52 fidx = 1; | |
53 if (nargin == 2) | |
54 sz = size (varargin{fidx}); | |
55 dx = (1:sz(2))(:); | |
56 dy = (1:sz(1))(:); | |
57 elseif (nargin == 3) | |
58 sz = size (varargin{fidx}); | |
59 dx = 1:sz(2); | |
60 dy = 1:sz(1); | |
61 dz = 1:sz(3); | |
62 elseif (nargin == 4) | |
63 fidx = 3; | |
64 dx = varargin{1}(1,:); | |
65 dy = varargin{2}(:,1); | |
66 elseif (nargin == 6) | |
67 fidx = 4; | |
68 dx = varargin{1}(1,:,1)(:); | |
69 dy = varargin{2}(:,1,1)(:); | |
70 dz = varargin{3}(1,1,:)(:); | |
71 else | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
72 print_usage (); |
11428 | 73 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
74 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
75 if (nargin == 4 || nargin == 2) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
76 if (! size_equal (varargin{fidx},varargin{fidx + 1})) |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
77 error ("divergence: size of X and Y must match"); |
11428 | 78 elseif (ndims (varargin{fidx}) != 2) |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
79 error ("divergence: expected two-dimensional matrices X and Y"); |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
80 elseif (length (dx) != columns (varargin{fidx}) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
81 || length (dy) != rows (varargin{fidx})) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11428
diff
changeset
|
82 error ("divergence: size of dx and dy must match the respective dimension of X and Y"); |
11428 | 83 endif |
84 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
85 retval = gradient (varargin{fidx}, dx, dy); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
86 retval += gradient (varargin{fidx + 1}.', dy, dx).'; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
87 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
88 elseif (nargin == 6 || nargin == 3) |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
89 if (! size_equal (varargin{fidx},varargin{fidx + 1},varargin{fidx + 2})) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
90 error ("divergence: size of X, Y, and Z must match"); |
11428 | 91 elseif (ndims (varargin{fidx}) != 3) |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
92 error ("divergence: expected two-dimensional matrices X, Y, and Z"); |
11428 | 93 elseif ((length (dx) != size (varargin{fidx}, 2)) |
94 || (length (dy) != size (varargin{fidx}, 1)) | |
95 || (length (dz) != size (varargin{fidx}, 3))) | |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
96 error ("divergence: size of dx, dy, and dz must match the respective dimesion of X, Y, and Z"); |
11428 | 97 endif |
98 | |
99 ## x-direction | |
100 retval = gradient (varargin{fidx}, dx, dy, dz); | |
101 ## y-direction | |
102 retval += shiftdim (gradient (shiftdim (varargin{fidx + 1}, 2), dy), 1); | |
103 ## z-direction | |
104 retval += shiftdim (gradient (shiftdim (varargin{fidx + 2}, 1), dz), 2); | |
105 endif | |
106 | |
107 endfunction | |
108 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 |
11428 | 110 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 %! [X,Y] = meshgrid (-20:20,-22:22); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
112 %! div = divergence (X-Y,Y); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
113 %! assert (all (div(:) == 2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
114 %! assert (size_equal (X,Y,div)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 |