1########################################################################
2##
3## Copyright (C) 2009-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {[@var{cx}, @var{cy}, @var{cz}, @var{v}] =} curl (@var{x}, @var{y}, @var{z}, @var{fx}, @var{fy}, @var{fz})
28## @deftypefnx {} {[@var{cz}, @var{v}] =} curl (@var{x}, @var{y}, @var{fx}, @var{fy})
29## @deftypefnx {} {[@dots{}] =} curl (@var{fx}, @var{fy}, @var{fz})
30## @deftypefnx {} {[@dots{}] =} curl (@var{fx}, @var{fy})
31## @deftypefnx {} {@var{v} =} curl (@dots{})
32## Calculate curl of vector field given by the arrays @var{fx}, @var{fy}, and
33## @var{fz} or @var{fx}, @var{fy} respectively.
34## @tex
35## $$ curl F(x,y,z) = \left( {\partial{d} \over \partial{y}} F_z - {\partial{d} \over \partial{z}} F_y, {\partial{d} \over \partial{z}} F_x - {\partial{d} \over \partial{x}} F_z, {\partial{d} \over \partial{x}} F_y - {\partial{d} \over \partial{y}} F_x \right)$$
36## @end tex
37## @ifnottex
38##
39## @example
40## @group
41##                   / d         d       d         d       d         d     \
42## curl F(x,y,z)  =  | -- Fz  -  -- Fy,  -- Fx  -  -- Fz,  -- Fy  -  -- Fx |
43##                   \ dy        dz      dz        dx      dx        dy    /
44## @end group
45## @end example
46##
47## @end ifnottex
48## The coordinates of the vector field can be given by the arguments @var{x},
49## @var{y}, @var{z} or @var{x}, @var{y} respectively.  @var{v} calculates the
50## scalar component of the angular velocity vector in direction of the z-axis
51## for two-dimensional input.  For three-dimensional input the scalar
52## rotation is calculated at each grid point in direction of the vector field
53## at that point.
54## @seealso{divergence, gradient, del2, cross}
55## @end deftypefn
56
57function varargout = curl (varargin)
58
59  fidx = 1;
60  if (nargin == 2)
61    sz = size (varargin{fidx});
62    dx = (1:sz(2))(:);
63    dy = (1:sz(1))(:);
64  elseif (nargin == 3)
65    sz = size (varargin{fidx});
66    dx = (1:sz(2))(:);
67    dy = (1:sz(1))(:);
68    dz = (1:sz(3))(:);
69  elseif (nargin == 4)
70    fidx = 3;
71    dx = varargin{1}(1,:);
72    dy = varargin{2}(:,1);
73  elseif (nargin == 6)
74    fidx = 4;
75    dx = varargin{1}(1,:,1)(:);
76    dy = varargin{2}(:,1,1)(:);
77    dz = varargin{3}(1,1,:)(:);
78  else
79    print_usage ();
80  endif
81
82  if (nargin == 4 || nargin == 2)
83    if (! size_equal (varargin{fidx}, varargin{fidx + 1}))
84      error ("curl: size of X and Y must match");
85    elseif (ndims (varargin{fidx}) != 2)
86      error ("curl: X and Y must be 2-D matrices");
87    elseif ((length (dx) != columns (varargin{fidx}))
88         || (length (dy) != rows (varargin{fidx})))
89      error ("curl: size of dx and dy must match the respective dimension of X and Y");
90    endif
91
92    dFx_dy = gradient (varargin{fidx}.', dy, dx).';
93    dFy_dx = gradient (varargin{fidx + 1}, dx, dy);
94    rot_z = dFy_dx - dFx_dy;
95    av = rot_z / 2;
96    if (nargout == 0 || nargout == 1)
97      varargout{1} = av;
98    else
99      varargout{1} = rot_z;
100      varargout{2} = av;
101    endif
102
103  elseif (nargin == 6 || nargin == 3)
104    if (! size_equal (varargin{fidx}, varargin{fidx + 1}, varargin{fidx + 2}))
105      error ("curl: size of X, Y, and Z must match");
106    elseif (ndims (varargin{fidx}) != 3)
107      error ("curl: X, Y, and Z must be 2-D matrices");
108    elseif ((length (dx) != size (varargin{fidx}, 2))
109         || (length (dy) != size (varargin{fidx}, 1))
110         || (length (dz) != size (varargin{fidx}, 3)))
111      error ("curl: size of dx, dy, and dz must match the respective dimesion of X, Y, and Z");
112    endif
113
114    [~, dFx_dy, dFx_dz] = gradient (varargin{fidx}, dx, dy, dz);
115    [dFy_dx, ~, dFy_dz] = gradient (varargin{fidx + 1}, dx, dy, dz);
116    [dFz_dx, dFz_dy] = gradient (varargin{fidx + 2}, dx, dy, dz);
117    rot_x = dFz_dy - dFy_dz;
118    rot_y = dFx_dz - dFz_dx;
119    rot_z = dFy_dx - dFx_dy;
120    l = sqrt(varargin{fidx}.^2 + varargin{fidx + 1}.^2 + varargin{fidx + 2}.^2);
121    av = (rot_x .* varargin{fidx} +
122          rot_y .* varargin{fidx + 1} +
123          rot_z .* varargin{fidx + 2}) ./ (2 * l);
124
125    if (nargout == 0 || nargout == 1)
126      varargout{1} = av;
127    else
128      varargout{1} = rot_x;
129      varargout{2} = rot_y;
130      varargout{3} = rot_z;
131      varargout{4} = av;
132    endif
133  endif
134
135endfunction
136
137
138%!test
139%! [X,Y] = meshgrid (-20:20,-22:22);
140%! av = curl (2*(X-Y), Y);
141%! assert (all (av(:) == 1));
142%! [cz,av] = curl (2*(X-Y), Y);
143%! assert (all (cz(:) == 2));
144%! assert (all (av(:) == 1));
145%! [cz,av] = curl (X/2, Y/2, 2*(X-Y), Y);
146%! assert (all (cz(:) == 4));
147%! assert (all (av(:) == 2));
148%! assert (size_equal (X,Y,cz,av));
149