Scalar and Vector Field Functionality#
Introduction#
Vectors and Scalars#
In physics, we deal with two kinds of quantities – scalars and vectors.
A scalar is an entity which only has a magnitude – no direction. Examples of scalar quantities include mass, electric charge, temperature, distance, etc.
A vector, on the other hand, is an entity that is characterized by a magnitude and a direction. Examples of vector quantities are displacement, velocity, magnetic field, etc.
A scalar can be depicted just by a number, for e.g. a temperature of 300 K.
On the other hand, vectorial quantities like acceleration are usually denoted
by a vector. Given a vector , the magnitude of the
corresponding quantity can be calculated as the magnitude of the vector
itself
, while the direction would be specified
by a unit vector in the direction of the original vector,
.
For example, consider a displacement of
m,
where , as per standard convention,
,
and
represent unit vectors
in the
,
and
directions respectively. Therefore, it can be concluded that the distance
traveled is
m =
m. The direction of travel is given by the unit vector
.
Fields#
In general, a is a vector or scalar quantity that can be
specified everywhere in space as a function of position (Note that in general
a field may also be dependent on time and other custom variables). In this
module, we deal with 3-dimensional spaces only. Hence, a field is defined as
a function of the
,
and
coordinates corresponding
to a location in 3D space.
For example, temperate in 3 dimensional space (a temperature field) can be
written as – a scalar function of the position.
An example of a scalar field in electromagnetism is the electric potential.
In a similar manner, a vector field can be defined as a vectorial function
of the location of any point in space.
For instance, every point on the earth may be considered to be in the
gravitational force field of the earth. We may specify the field by the
magnitude and the direction of acceleration due to gravity
(i.e. force per unit mass ) at every point in space.
To give an example from electromagnetism, consider an electric potential
of form , a scalar field in 3D space. The corresponding
conservative electric field can be computed as the gradient of the electric
potential function, and expressed as
.
The magnitude of this electric field can in turn be expressed
as a scalar field of the form
.
Implementation of fields in sympy.physics.vector#
In sympy.physics.vector
, every ReferenceFrame
instance is
assigned basis vectors corresponding to the ,
and
directions. These can be accessed using the attributes named
x
, y
and
z
respectively. Hence, to define a vector of the form
with respect
to a given frame
, you would do
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> v = 3*R.x + 4*R.y + 5*R.z
Vector math and basic calculus operations with respect to vectors have already been elaborated upon in other sections of this module’s documentation.
On the other hand, base scalars (or coordinate variables) are implemented
as special SymPy Symbol
s assigned to every frame, one for each
direction from ,
and
. For a frame
R
, the ,
and
base scalar
Symbol
s can be accessed using the R[0]
, R[1]
and R[2]
expressions respectively.
Therefore, to generate the expression for the aforementioned electric
potential field , you would have to do
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> electric_potential
2*R_x**2*R_y
In string representation, R_x
denotes the base
scalar assigned to
ReferenceFrame
R
. Essentially, R_x
is
the string representation of R[0]
.
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 (i.e.
R[0]
), you would have to use the
diff
function.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> from sympy import diff
>>> diff(electric_potential, R[0])
4*R_x*R_y
Like vectors (and vector fields), scalar fields can also be re-expressed in
other frames of reference, apart from the one they were defined in – assuming
that an orientation relationship exists between the concerned frames. This can
be done using the sympy.physics.vector.vector.Vector.express
method, in a way
similar to vectors - but with the variables
parameter set to
True
.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> from sympy.physics.vector import dynamicsymbols, express
>>> q = dynamicsymbols('q')
>>> R1 = R.orientnew('R1', rot_type = 'Axis', amounts = [q, R.z])
>>> express(electric_potential, R1, variables=True)
2*(R1_x*sin(q(t)) + R1_y*cos(q(t)))*(R1_x*cos(q(t)) - R1_y*sin(q(t)))**2
Moreover, considering scalars can also be functions of time just as vectors,
differentiation with respect to time is also possible. Depending on the
Symbol
s present in the expression and the frame
with respect to which the time differentiation is being done, the output will
change/remain the same.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> q = dynamicsymbols('q')
>>> R1 = R.orientnew('R1', rot_type = 'Axis', amounts = [q, R.z])
>>> from sympy.physics.vector import time_derivative
>>> time_derivative(electric_potential, R)
0
>>> time_derivative(electric_potential, R1).simplify()
2*(R1_x*cos(q(t)) - R1_y*sin(q(t)))*(3*R1_x**2*cos(2*q(t))/2 -
R1_x**2/2 - 3*R1_x*R1_y*sin(2*q(t)) - 3*R1_y**2*cos(2*q(t))/2 -
R1_y**2/2)*Derivative(q(t), t)