Scalar and Vector Field Functionality

Implementation in sympy.vector

Scalar and vector fields

In sympy.vector, every CoordSys3D instance is assigned basis vectors corresponding to the \(X\), \(Y\) and \(Z\) axes. These can be accessed using the properties named i, j and k respectively. Hence, to define a vector \(\mathbf{v}\) of the form \(3\mathbf{\hat{i}} + 4\mathbf{\hat{j}} + 5\mathbf{\hat{k}}\) with respect to a given frame \(\mathbf{R}\), you would do

>>> from sympy.vector import CoordSys3D
>>> R = CoordSys3D('R')
>>> v = 3*R.i + 4*R.j + 5*R.k

Vector math and basic calculus operations with respect to vectors have already been elaborated upon in the earlier section of this module’s documentation.

On the other hand, base scalars (or coordinate variables) are implemented in a special class called BaseScalar, and are assigned to every coordinate system, one for each axis from \(X\), \(Y\) and \(Z\). These coordinate variables are used to form the expressions of vector or scalar fields in 3D space. For a system R, the \(X\), \(Y\) and \(Z\) BaseScalars instances can be accessed using the R.x, R.y and R.z expressions respectively.

Therefore, to generate the expression for the aforementioned electric potential field \(2{x}^{2}y\), you would have to do

>>> from sympy.vector import CoordSys3D
>>> R = CoordSys3D('R')
>>> electric_potential = 2*R.x**2*R.y
>>> electric_potential

It is to be noted that BaseScalar instances can be used just like any other SymPy Symbol, except that they store the information about the coordinate system and axis they correspond to.

Scalar fields can be treated just as any other SymPy expression, for any math/calculus functionality. Hence, to differentiate the above electric potential with respect to \(x\) (i.e. R.x), you would use the diff method.

>>> from sympy.vector import CoordSys3D
>>> R = CoordSys3D('R')
>>> electric_potential = 2*R.x**2*R.y
>>> from sympy import diff
>>> diff(electric_potential, R.x)

It is worth noting that having a BaseScalar in the expression implies that a ‘field’ changes with position, in 3D space. Technically speaking, a simple Expr with no BaseScalar s is still a field, though constant.

Like scalar fields, vector fields that vary with position can also be constructed using BaseScalar s in the measure-number expressions.

>>> from sympy.vector import CoordSys3D
>>> R = CoordSys3D('R')
>>> v = R.x**2*R.i + 2*R.x*R.z*R.k

The Del operator

The Del, or ‘Nabla’ operator - written as \(\mathbf{\nabla}\) is commonly known as the vector differential operator. Depending on its usage in a mathematical expression, it may denote the gradient of a scalar field, the divergence of a vector field, or the curl of a vector field.

Essentially, \(\mathbf{\nabla}\) is not technically an ‘operator’, but a convenient mathematical notation to denote any one of the aforementioned field operations.

In sympy.vector, \(\mathbf{\nabla}\) has been implemented as the Del() class. The instance of this class is independent of coordinate system. Hence, the \(\mathbf{\nabla}\) operator would be accessible as Del().

Given below is an example of usage of the Del() class.

>>> from sympy.vector import CoordSys3D, Del
>>> C = CoordSys3D('C')
>>> delop = Del()
>>> gradient_field = delop(C.x*C.y*C.z)
>>> gradient_field
(Derivative(C.x*C.y*C.z, C.x))*C.i + (Derivative(C.x*C.y*C.z, C.y))*C.j
+ (Derivative(C.x*C.y*C.z, C.z))*C.k

The above expression can be evaluated using the SymPy doit() routine.

>>> gradient_field.doit()
C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k

Usage of the \(\mathbf{\nabla}\) notation in sympy.vector has been described in greater detail in the subsequent subsections.

Field operator in orthogonal curvilinear coordinate system

vector package supports calculation in different kind of orthogonal curvilinear coordinate system. To do that, scaling factor (also known as Lame coefficients) are used to express curl, divergence or gradient in desired type of coordinate system.

For example if we want to calculate gradient in cylindrical coordinate system all we need to do is to create proper coordinate system

>>> from sympy.vector import CoordSys3D
>>> c = CoordSys3D('c', transformation='cylindrical', variable_names=("r", "theta", "z"))
>>> gradient(c.r*c.theta*c.z)
    c.theta*c.z*c.i + c.z*c.j + c.r*c.theta*c.k

Conservative and Solenoidal fields

In vector calculus, a conservative field is a field that is the gradient of some scalar field. Conservative fields have the property that their line integral over any path depends only on the end-points, and is independent of the path travelled. A conservative vector field is also said to be ‘irrotational’, since the curl of a conservative field is always zero.

In physics, conservative fields represent forces in physical systems where energy is conserved.

To check if a vector field is conservative in sympy.vector, the is_conservative function can be used.

>>> from sympy.vector import CoordSys3D, is_conservative
>>> R = CoordSys3D('R')
>>> field = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
>>> is_conservative(field)
>>> curl(field)

A solenoidal field, on the other hand, is a vector field whose divergence is zero at all points in space.

To check if a vector field is solenoidal in sympy.vector, the is_solenoidal function can be used.

>>> from sympy.vector import CoordSys3D, is_solenoidal
>>> R = CoordSys3D('R')
>>> field = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
>>> is_solenoidal(field)
>>> divergence(field)

Scalar potential functions

We have previously mentioned that every conservative field can be defined as the gradient of some scalar field. This scalar field is also called the ‘scalar potential field’ corresponding to the aforementioned conservative field.

The scalar_potential function in sympy.vector calculates the scalar potential field corresponding to a given conservative vector field in 3D space - minus the extra constant of integration, of course.

Example of usage -

>>> from sympy.vector import CoordSys3D, scalar_potential
>>> R = CoordSys3D('R')
>>> conservative_field = 4*R.x*R.y*R.z*R.i + 2*R.x**2*R.z*R.j + 2*R.x**2*R.y*R.k
>>> scalar_potential(conservative_field, R)

Providing a non-conservative vector field as an argument to scalar_potential raises a ValueError.

The scalar potential difference, or simply ‘potential difference’, corresponding to a conservative vector field can be defined as the difference between the values of its scalar potential function at two points in space. This is useful in calculating a line integral with respect to a conservative function, since it depends only on the endpoints of the path.

This computation is performed as follows in sympy.vector.

>>> from sympy.vector import CoordSys3D, Point
>>> from sympy.vector import scalar_potential_difference
>>> R = CoordSys3D('R')
>>> P = R.origin.locate_new('P', 1*R.i + 2*R.j + 3*R.k)
>>> vectfield = 4*R.x*R.y*R.i + 2*R.x**2*R.j
>>> scalar_potential_difference(vectfield, R, R.origin, P)

If provided with a scalar expression instead of a vector field, scalar_potential_difference returns the difference between the values of that scalar field at the two given points in space.