REDUCE

20.4 AVECTOR: A Vector Algebra and Calculus Package

This package [Har89] provides REDUCE with the ability to perform vector algebra using the same notation as scalar algebra. The basic algebraic operations are supported, as are differentiation and integration of vectors with respect to scalar variables, cross product and dot product, component manipulation and application of scalar functions (e.g. cosine) to a vector to yield a vector result.

Main author: David Harper

Other contributors: Malcolm MacCallum and Francis Wright

20.4.1 Introduction

In this chapter, the word vector means a physical vector in three dimensions. The dimensions cannot be changed; this package supports vectors in three dimensions only.

In addition to vector algebra, a set of vector calculus operators is provided for use with any orthogonal curvilinear coordinate system. These operators are gradient, divergence, curl and del-squared (Laplacian). The Laplacian operator can take scalar or vector arguments.

Several important coordinate systems are pre-defined and can be invoked by name. It is also possible to create new coordinate systems by specifying the names of the coordinates and the values of the scale factors.

This package is not auto-loaded, so in order to use it you first need to execute the command

   load_package avector;

20.4.2 Vector declaration and initialisation

Any name may be declared to be a vector, provided that it has not previously been declared as a matrix or an array. To declare one or more names to be vectors use the vec command:

   vec a,b,c;

declares the variables a, b and c to be vectors. If they have already been assigned (scalar) values, these will be lost. When a vector is declared using the vec command, its components are initially zero.

If a vector value is assigned to a scalar variable, then that variable will automatically be declared as a vector and the user will be notified that this has happened.

A vector may be initialised using the avec function which takes three scalar arguments and returns a vector made up from those scalars. The following example sets the components of the vector a to a1, a2 and a3, and displays the result:

   a := avec(a1, a2, a3);

\begin{gather*} vec(x)\,:=\,\mathit {a_{1}}\\ vec(y)\,:=\,\mathit {a_{2}}\\ vec(z)\,:=\,\mathit {a_{3}} \end{gather*} As illustrated above, vectors are displayed in the form of assignments to their components indexed by the coordinate names for the current coordinate system (see below).

The components of any vector may be accessed naturally by indexing using the coordinate names, or by using zero-based index notation in the same way as for an array:

v := avec(ax, ay, az);
v(x); v(0); both yield ax
v(y); v(1); both yield ay
v(z); v(2); both yield az

It is also possible to set values of individual components. Following from above:

   v(y) := b;

or equivalently

   v(1) := b;

The vector v now has components ax, b, az.

The predicate vecp returns t is its argument is a vector, and nil (which displays as nothing or 0 in algebraic mode) otherwise. It can be used in tests, for example:

   write "The variable v is",
      if vecp v then "" else " not",
      " a vector.";

20.4.3 Vector algebra

(In the examples which follow, v, v1, v2, etc. are assumed to be vectors while s, s1, s2, etc. are scalars.)

The scalar algebra operators +, -, * and / may be used with vector operands according to the rules of vector algebra. Thus multiplication and division of a vector by a scalar are both allowed, but it is an error to multiply or divide one vector by another.

v := v1 + v2 - v3; Addition and subtraction
v := s1*3*v1; Scalar multiplication
v := v1/s; Scalar division
v := -v1; Negation

Vector multiplication is carried out using the infix operators dot or ., and cross or ><. These are defined to have higher precedence than scalar multiplication and division, and cross has higher precedence than dot.

v := v1 cross v2; Vector (cross) product
v := v1 >< v2; Vector (cross) product
s := v1 dot v2; Scalar (dot) product
s := v1 . v2; Scalar (dot) product
v := v1 >< v2 + v3;
v := (v1 >< v2) + v3;

The last two expressions are equivalent due to the precedence of the cross operator.

Note that the vector triple product

   v1 >< v2 >< v3

parses as

   (v1 >< v2) >< v3

(by left-associativity), and is (generally) not equal to

   v1 >< (v2 >< v3)

whereas the scalar triple product

   v1 >< v2 . v3

is equal to

   v1 . v2 >< v3

and neither requires parentheses (because cross has higher precedence that dot).

The modulus (or magniture) of a vector may be calculated using the vmod operator:

   s := vmod v;

A unit vector may be generated from any vector using the vmod operator:

   v1 := v/(vmod v);

Vectors may be given as arguments to unary functions such as sin, log and tan. The result is a vector obtained by applying the function component-wise to the argument vector, e.g.

   v := avec(a1, a2, a3)$
   sin(v);

\begin{gather*} vec(x)\,:=\,\sin (a_{1})\\ vec(y)\,:=\,\sin (a_{2})\\ vec(z)\,:=\,\sin (a_{3}) \end{gather*}

A vector may be pre-multiplied by a \(3\times 3\) matrix, or a matrix expression that evaluates to a \(3\times 3\) matrix. In such a product, the vector behaves like a \(3\times 1\) column matrix, but the result is a vector.

20.4.4 Vector calculus

Vectors may be used as operands in the differentiation and integration operators in place of the dependent expression.

v := avec(x^2, sin(x), y);

\begin{gather*} vec(x)\,:=\,x^2\\ vec(y)\,:=\,\sin (x)\\ vec(z)\,:=\,y \end{gather*}

df(v,x);

\begin{gather*} vec(x)\,:=\,2\,x\\ vec(y)\,:=\,\cos (x)\\ vec(z)\,:=\,0 \end{gather*}

int(v,x);

\begin{gather*} vec(x)\,:=\,\frac {x^3}{3}\\ vec(y)\,:=\,\,-\,\cos (x)\\ vec(z)\,:=\,x\,y \end{gather*}

The vector calculus operators div, grad and curl are recognised. The Laplacian operator is also available and may be applied to scalar and vector arguments.

v := grad s; Gradient of a scalar field
s := div v; Divergence of a vector field
v := curl v1; Curl of a vector field
s := delsq s1; Laplacian of a scalar field
v := delsq v1; Laplacian of a vector field

These operators may be used in any orthogonal curvilinear coordinate system. The user may alter the names of the coordinates and the values of the scale factors. Initially the coordinate system is cartesian, the names of the coordinates are x, y and z, and the scale factors are all unity.

There are two special vectors: coords contains the names of the coordinates in the current system and hfactors contains the values of the scale factors.

The current coordinate names may be changed using the coordinates command.

   coordinates r, theta, phi;

This command changes the coordinate names to \(r\), \(\theta \) and \(\phi \).

The current scale factors may be altered using the scalefactors command.

   scalefactors 1, r, r*sin(theta);

This command changes the scale factors to \(1\), \(r\) and \(r\sin (\theta )\).

The arguments to coordinates and scalefactors can be enclosed in parentheses, but this is not necessary.

When vector differential operators are applied to an expression, the current set of coordinates is used as the independent variables and the scale factors are employed in the calculation. (See, for example, Batchelor G. K. An Introduction to Fluid Mechanics, Appendix 2.)

Several coordinate systems are pre-defined and may be invoked by name. To see a list of valid names execute the command

   csystems;

and REDUCE will respond with something like

   (cartesian spherical cylindrical)

To choose a coordinate system by name, use the command getcsystem. For example, to choose the Cartesian coordinate system:

   getcsystem cartesian;

The argument to getcsystem should be an identifier (which, for backward compatibility, may be preceded by a single quote). It is not evaluated and must be a literal coordinate system name.

REDUCE responds by displaying a list of the coordinate names in that coordinate system. The example above would produce the response

   (x y z)

whilst

   getcsystem spherical;

would produce

   (r theta phi)

Note that any attempt to invoke a coordinate system is subject to the same restrictions as the implied calls to coordinates and scalefactors. In particular, getcsystem fails if any of the coordinate names has been assigned a value, and the previous coordinate system remains in effect.

A user-defined coordinate system can be assigned a name using the command putcsystem. It may then be re-invoked at a later stage using getcsystem. The argument to putcsystem is as for getcsystem.

Example 1

We define a general coordinate system with coordinate names c1, c2, c3 and scale factors h1, h2, h3:

   coordinates c1, c2, c3;
   scalefactors h1, h2, h3;
   putcsystem general;

This system may later be invoked by entering

   getcsystem general;

20.4.5 Integration

Definite integrals of scalar and vector expressions may be calculated using the defint function. In general, definite integrals with respect to an independent variable \(x\) between the limits \(x_\mathit {min}\) and \(x_\mathit {max}\) can be specified by using the syntax x = x_min .. x_max, and this syntax will generally be used below, but for backward compatibility the independent variable and limits can also always be given as the sequence x, x_min, x_max.

If the switch travecint is turned on then the functions defint, lineint, surfint and volint described below display the final unevaluated (multiple) integral before evaluating and returning it. (The legacy function volintegral displays three single unevaluated integrals because it calls defint three times.)

Example 2

To calculate the definite integral of \(\sin (x)^2\) between \(0\) and \(2\pi \) we enter either

   defint(sin(x)^2, x = 0 .. 2pi);

\[ \pi \] or equivalently

   defint(sin(x)^2, x, 0, 2pi);

\[ \pi \] This function simply calls the extension of the int operator taking two extra arguments, the lower and upper bounds of integration, after checking that the independent integration variable and limits are not vectors.

Several functions are provided to perform line, surface and volume integrals. These operate in any orthogonal curvilinear coordinate system and make use of the scale factors described in the previous section.

Line integrals of vector and scalar fields

For background, see e.g. Wikipedia.

Consider the line integral of a vector field \(\mathbf {v}\) over a finite curve \(C\) specified by the vector function \(\mathbf {r}(t)\) of the (scalar) parameter \(t \in [t_\mathit {min},t_\mathit {max}]\), namely \[ \int _C \mathbf {v} \cdot \mathrm {d}\mathbf {r} = \int _{t_\mathit {min}}^{t_\mathit {max}} \mathbf {v}(\mathbf {r}(t)) \cdot \mathbf {r}'(t) \, \mathrm {d}t \] and the related line integral of a scalar field \(f\), namely \[ \int _C f \,\mathrm {d}s = \int _{t_\mathit {min}}^{t_\mathit {max}} f(\mathbf {r}(t)) \, \|\mathbf {r}'(t)\| \, \mathrm {d}t. \] They can both be computed using

   lineint(v_or_f, r, t = t_min .. t_max)

or equivalently

   deflineint(v_or_f, r, t = t_min .. t_max)

and the related indefinite integrals can both be computed using

   lineint(v_or_f, r, t)

where v_or_f is a vector or scalar expression, as appropriate. The operator lineint performs either indefinite or definite integration, depending on whether it is given limits. The operator deflineint is a synonym for lineint, retained for backward compatibility.

Example 3

In spherical polar coordinates \((r,\theta ,\phi )\) we may integrate round a line of constant \(\theta \) (‘latitude’) on a sphere of radius \(\mathit {rr}\) to find the length of such a line.1 The vector function is thus the tangent to the ‘line of latitude’ \((0,0,1)\) and the path is \((\mathit {rr},\mathit {lat},\phi )\) where \(\mathit {lat}\) is the complement of the latitude2 and \(\phi \) is the independent variable. The length of the line as a function of \(\phi \) is given by the indefinite integral:

getcsystem spherical;
lineint(avec(0,0,1), avec(rr,lat,phi), phi);

\[ \sin (lat)\,\phi \,rr \] The length of the whole line of latitude is given by the definite integral from \(\phi =0\) to \(2\pi \):

lineint(avec(0,0,1), avec(rr,lat,phi), phi = 0 .. 2pi);

\[ 2\,\sin (lat)\,\pi \,rr \]

Perhaps more obviously, we can find the length of a line of latitude by integrating the constant unit scalar field 1:

lineint(1, avec(rr,lat,phi), phi);

\[ \sin (lat)\,\phi \,rr \]

lineint(1, avec(rr,lat,phi), phi = 0 .. 2pi);

\[ 2\,\sin (lat)\,\pi \,rr \]

Surface integrals of vector and scalar fields

For background, see e.g. Wikipedia.

Consider the surface integral of a vector field \(\mathbf {v}\) over a finite surface \(S\) specified by the vector function \(\mathbf {r}(s,t)\) of two (scalar) parameters \(s \in [s_\mathit {min},s_\mathit {max}]\) and \(t \in [t_\mathit {min},t_\mathit {max}]\), \((s,t)\in T\), namely \[ \iint _S\mathbf {v} \cdot \mathrm {d}\mathbf {s} = \iint _T\mathbf {v}(\mathbf {r}(s,t))\cdot \left (\frac {\partial \mathbf {r}}{\partial {s}} \times \frac {\partial \mathbf {r}}{\partial {t}}\right ) \mathrm {d}s\,\mathrm {d}t. \] The \(s\) limits \(s_\mathit {min},s_\mathit {max}\) can depend on the parameter \(t\), so the region in parameter space need not be rectangular. The integral can be computed using

surfint(v, r, s = s_min .. s_max, t = t_min .. t_max)

Integration is performed in the order that the integration variables are specified, i.e. with respect to s first and then with respect to t. Note that the order of the integration variables affects the sign of the integral, because it determines the order of the tangent vectors in the cross product, and hence its sign, in the integrand.

Example 4

As a trivial example, the integral of the unit radial vector field over the surface of a sphere of radius \(\mathit {rr}\) gives the area of the sphere:

getcsystem spherical;
surfint(avec(1,0,0), avec(rr,theta,phi),
   theta = 0 .. pi, phi = 0 .. 2pi);

\[ 4\,\pi \,rr^2 \] Now consider the related surface integral of a scalar field \(f\), namely \[ \iint _Sf\mathrm {d}\mathbf {s}= \iint _Tf(\mathbf {r}(s,t)) \left \|\frac {\partial \mathbf {r}}{\partial {s}} \times \frac {\partial \mathbf {r}}{\partial {t}}\right \| \mathrm {d}s\,\mathrm {d}t. \] It can be computed using

surfint(f, r, s = s_min .. s_max, t = t_min .. t_max)

As above, integration is performed in the order that the integration variables are specified, i.e. with respect to s first and then with respect to t. But, in this case, the order of the integration variables has no effect mathematically (although it might affect the computation).

Example 5

As a trivial example, the integral of the unit scalar field 1 over the surface of a sphere of radius \(\mathit {rr}\) gives the area of the sphere:

getcsystem spherical;
surfint(1, avec(rr,theta,phi),
   theta = 0 .. pi, phi = 0 .. 2pi);

\[ 4\,\pi \,rr^2 \]

Volume integrals of scalar and vector fields

For background, see e.g. Wikipedia.

Consider the volume integral of a scalar or vector field \(f\) over a finite volume \(V\) corresponding to the region in coordinate space \(x \in [x_\mathit {min},x_\mathit {max}]\), \(y \in [y_\mathit {min},y_\mathit {max}]\), and \(z \in [z_\mathit {min},z_\mathit {max}]\), \((x,y,z)\in W\), namely \[ \iiint _V f\,\mathrm {d}v = \iiint _W f(x,y,z)\,\mathrm {d}x\,\mathrm {d}y\,\mathrm {d}z. \] If the coordinate system \((x,y,z)\) is curvilinear then the required scale factors are included automatically, but they are not shown here for simplicity. The \(x\) limits \(x_\mathit {min},x_\mathit {max}\) can depend on \(y,z\) and the \(y\) limits \(y_\mathit {min},y_\mathit {max}\) can depend on \(z\), so the region in parameter space need not be rectangular. The volume integral can be computed using

volint(f, x = x_min .. x_max,
          y = y_min .. y_max,
          z = z_min .. z_max)

If f is a vector field then volint maps over the components; there is no other special processing. Integration is performed in the order that the integration variables are specified, which matters if limits are functions of other coordinates.

Example 6

In spherical polar coordinates \((r,\theta ,\phi )\) we may calculate the volume of a sphere of radius \(\mathit {rr}\) by integrating unity over the range \(r=0\) to \(\mathit {rr}\), \(\theta =0\) to \(\pi \), \(\phi =0\) to \(2\pi \) as follows:

getcsystem spherical;
volint(1, r = 0 .. rr,
          theta = 0 .. pi,
          phi = 0 .. 2pi);

\[ \frac {4\,\pi \,rr^3}{3} \] The integration order makes no difference in this example.

Example 7

In cylindrical polar coordinates \((r,z,\phi )\) we may calculate the volume of a right circular cone of base radius \(\mathit {rr}\) and height \(h\) sitting on the plane \(z=0\) by integrating unity over the range \(r=0\) to \(\frac {h-z}{h}\mathit {rr}\), \(z=0\) to \(h\), \(\phi =0\) to \(2\pi \) as follows:

getcsystem cylindrical;
volint(1, r = 0 .. (h-z)/h*rr,
          z = 0 .. h,
          phi = 0 .. 2pi);

\[ \frac {h\,\pi \,rr^2}{3} \] In this example, the \(z\) integration must be performed after the \(r\) integration because the upper \(r\) limit depends on \(z\), but apart from that the integration order makes no difference.

Legacy volume integration function

Definite volume integrals of scalar fields may be calculated using the volintegral function whose syntax is as follows:

volintegral(〈integrand:scalar expression⟩,
〈lower-bound:vector⟩, 〈upper-bound:vector⟩)

However, it is first necessary to specify the order of integration by assigning one of the three numbers \(0,1,2\) to each of the components of the special vector volintorder (where \((0,1,2)\) is the default). Integration is performed first with respect to the component assigned \(0\), then the component assigned \(1\), then the component assigned \(2\).

Example 8

In spherical polar coordinates \((r,\theta ,\phi )\) we may calculate the volume of a sphere of radius \(\mathit {rr}\) by integrating unity over the range \(r=0\) to \(\mathit {rr}\), \(\theta =0\) to \(\pi \), \(\phi =0\) to \(2\pi \) as follows:

getcsystem spherical;
vlb := avec(0,0,0)$ Lower bound
vub := avec(rr,pi,2pi)$ Upper bound in \(r, \theta , \phi \) respectively
volintorder := avec(0,1,2)$ The order of integration
volintegral(1,vlb,vub);
\[ \frac {4\,\pi \,rr^3}{3} \]

Example 9

Suppose we wish to calculate the volume of a right circular cone. This is equivalent to integrating unity over a conical region with the bounds, using cylindrical polar coordinates \((r,z,\phi )\):3

\(z = 0\) to \(h\) (\(h\) = the height of the cone)
\(r = 0\) to \(p z\) (\(p\) = ratio of base diameter to height)
\(\phi = 0\) to \(2\pi \)

We evaluate the volume by integrating a series of infinitesimally thin circular disks of constant \(z\)-value. The integration is thus performed in the order: \(\phi \) from 0 to \(2\pi \), \(r\) from 0 to \(pz\), \(z\) from 0 to \(h\). The order of the indices is thus 2, 0, 1.

   getcsystem cylindrical;
   vlb := avec(0,0,0)$
   vub := avec(p*z,h,2pi)$
   volintorder := avec(2,0,1)$
   volintegral(1,vlb,vub);

\[ \frac {h^3\,p^2\,\pi }{3} \] (At this stage, we could replace p*h by rr, the base radius of the cone, to obtain the result in its more familiar form.)

Numerical integration

If the switch rounded is on then the functions defint, lineint and surfint perform approximate numerical integration by calling num_int instead of int. This works only if the result will be a pure number, with no symbolic content. Provided this is the case, it works well for defint and lineint. For surfint, it produces noticeable numerical error, even for simple problems, but may still be useful. If one of these integrals fails to evaluate symbolically then numerical approximation may provide a way forwards.

(For volintegral, numerical integration appears to fail, so it is not currently supported. Numerical integration in more than one dimension is difficult; see e.g. Wikipedia!)

20.4.6 Defining new functions and procedures

Most of the procedures in this package are defined in symbolic mode and are invoked by the REDUCE expression-evaluator when a vector expression is encountered. It is not generally possible to define procedures which accept or return vector values in algebraic mode. This is a consequence of the way in which the REDUCE interpreter operates and it affects other non-scalar data types as well: arrays cannot be passed as algebraic procedure arguments, for example.

20.4.7 Acknowledgements

This package was originally written whilst the author was the U.K. Computer Algebra Support Officer at the University of Liverpool Computer Laboratory.


Hosted by Download REDUCE Powered by MathJax