Essential Classes in sympy.vector (docstrings)

CoordSys3D

class sympy.vector.coordsysrect.CoordSys3D(name, location=None, rotation_matrix=None, parent=None, vector_names=None, variable_names=None, latex_vects=None, pretty_vects=None, latex_scalars=None, pretty_scalars=None, transformation=None)[source]

Represents a coordinate system in 3-D space.

__init__(name, location=None, rotation_matrix=None, parent=None, vector_names=None, variable_names=None, latex_vects=None, pretty_vects=None, latex_scalars=None, pretty_scalars=None, transformation=None)[source]

The orientation/location parameters are necessary if this system is being defined at a certain orientation or location wrt another.

Parameters:

name : str

The name of the new CoordSys3D instance.

transformation : Lambda, Tuple, str

Transformation defined by transformation equations or chosen from predefined ones.

location : Vector

The position vector of the new system’s origin wrt the parent instance.

rotation_matrix : SymPy ImmutableMatrix

The rotation matrix of the new coordinate system with respect to the parent. In other words, the output of new_system.rotation_matrix(parent).

parent : CoordSys3D

The coordinate system wrt which the orientation/location (or both) is being defined.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

create_new(name, transformation, variable_names=None, vector_names=None)[source]

Returns a CoordSys3D which is connected to self by transformation.

Parameters:

name : str

The name of the new CoordSys3D instance.

transformation : Lambda, Tuple, str

Transformation defined by transformation equations or chosen from predefined ones.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> a = CoordSys3D('a')
>>> b = a.create_new('b', transformation='spherical')
>>> b.transformation_to_parent()
(b.r*sin(b.theta)*cos(b.phi), b.r*sin(b.phi)*sin(b.theta), b.r*cos(b.theta))
>>> b.transformation_from_parent()
(sqrt(a.x**2 + a.y**2 + a.z**2), acos(a.z/sqrt(a.x**2 + a.y**2 + a.z**2)), atan2(a.y, a.x))
locate_new(name, position, vector_names=None, variable_names=None)[source]

Returns a CoordSys3D with its origin located at the given position wrt this coordinate system’s origin.

Parameters:

name : str

The name of the new CoordSys3D instance.

position : Vector

The position vector of the new system’s origin wrt this one.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> A = CoordSys3D('A')
>>> B = A.locate_new('B', 10 * A.i)
>>> B.origin.position_wrt(A.origin)
10*A.i
orient_new(name, orienters, location=None, vector_names=None, variable_names=None)[source]

Creates a new CoordSys3D oriented in the user-specified way with respect to this system.

Please refer to the documentation of the orienter classes for more information about the orientation procedure.

Parameters:

name : str

The name of the new CoordSys3D instance.

orienters : iterable/Orienter

An Orienter or an iterable of Orienters for orienting the new coordinate system. If an Orienter is provided, it is applied to get the new system. If an iterable is provided, the orienters will be applied in the order in which they appear in the iterable.

location : Vector(optional)

The location of the new coordinate system’s origin wrt this system’s origin. If not specified, the origins are taken to be coincident.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
>>> N = CoordSys3D('N')

Using an AxisOrienter

>>> from sympy.vector import AxisOrienter
>>> axis_orienter = AxisOrienter(q1, N.i + 2 * N.j)
>>> A = N.orient_new('A', (axis_orienter, ))

Using a BodyOrienter

>>> from sympy.vector import BodyOrienter
>>> body_orienter = BodyOrienter(q1, q2, q3, '123')
>>> B = N.orient_new('B', (body_orienter, ))

Using a SpaceOrienter

>>> from sympy.vector import SpaceOrienter
>>> space_orienter = SpaceOrienter(q1, q2, q3, '312')
>>> C = N.orient_new('C', (space_orienter, ))

Using a QuaternionOrienter

>>> from sympy.vector import QuaternionOrienter
>>> q_orienter = QuaternionOrienter(q0, q1, q2, q3)
>>> D = N.orient_new('D', (q_orienter, ))
orient_new_axis(name, angle, axis, location=None, vector_names=None, variable_names=None)[source]

Axis rotation is a rotation about an arbitrary axis by some angle. The angle is supplied as a SymPy expr scalar, and the axis is supplied as a Vector.

Parameters:

name : string

The name of the new coordinate system

angle : Expr

The angle by which the new system is to be rotated

axis : Vector

The axis around which the rotation has to be performed

location : Vector(optional)

The location of the new coordinate system’s origin wrt this system’s origin. If not specified, the origins are taken to be coincident.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = CoordSys3D('N')
>>> B = N.orient_new_axis('B', q1, N.i + 2 * N.j)
orient_new_body(name, angle1, angle2, angle3, rotation_order, location=None, vector_names=None, variable_names=None)[source]

Body orientation takes this coordinate system through three successive simple rotations.

Body fixed rotations include both Euler Angles and Tait-Bryan Angles, see http://en.wikipedia.org/wiki/Euler_angles.

Parameters:

name : string

The name of the new coordinate system

angle1, angle2, angle3 : Expr

Three successive angles to rotate the coordinate system by

rotation_order : string

String defining the order of axes for rotation

location : Vector(optional)

The location of the new coordinate system’s origin wrt this system’s origin. If not specified, the origins are taken to be coincident.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q1, q2, q3 = symbols('q1 q2 q3')
>>> N = CoordSys3D('N')

A ‘Body’ fixed rotation is described by three angles and three body-fixed rotation axes. To orient a coordinate system D with respect to N, each sequential rotation is always about the orthogonal unit vectors fixed to D. For example, a ‘123’ rotation will specify rotations about N.i, then D.j, then D.k. (Initially, D.i is same as N.i) Therefore,

>>> D = N.orient_new_body('D', q1, q2, q3, '123')

is same as

>>> D = N.orient_new_axis('D', q1, N.i)
>>> D = D.orient_new_axis('D', q2, D.j)
>>> D = D.orient_new_axis('D', q3, D.k)

Acceptable rotation orders are of length 3, expressed in XYZ or 123, and cannot have a rotation about about an axis twice in a row.

>>> B = N.orient_new_body('B', q1, q2, q3, '123')
>>> B = N.orient_new_body('B', q1, q2, 0, 'ZXZ')
>>> B = N.orient_new_body('B', 0, 0, 0, 'XYX')
orient_new_quaternion(name, q0, q1, q2, q3, location=None, vector_names=None, variable_names=None)[source]

Quaternion orientation orients the new CoordSys3D with Quaternions, defined as a finite rotation about lambda, a unit vector, by some amount theta.

This orientation is described by four parameters:

q0 = cos(theta/2)

q1 = lambda_x sin(theta/2)

q2 = lambda_y sin(theta/2)

q3 = lambda_z sin(theta/2)

Quaternion does not take in a rotation order.

Parameters:

name : string

The name of the new coordinate system

q0, q1, q2, q3 : Expr

The quaternions to rotate the coordinate system by

location : Vector(optional)

The location of the new coordinate system’s origin wrt this system’s origin. If not specified, the origins are taken to be coincident.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
>>> N = CoordSys3D('N')
>>> B = N.orient_new_quaternion('B', q0, q1, q2, q3)
orient_new_space(name, angle1, angle2, angle3, rotation_order, location=None, vector_names=None, variable_names=None)[source]

Space rotation is similar to Body rotation, but the rotations are applied in the opposite order.

Parameters:

name : string

The name of the new coordinate system

angle1, angle2, angle3 : Expr

Three successive angles to rotate the coordinate system by

rotation_order : string

String defining the order of axes for rotation

location : Vector(optional)

The location of the new coordinate system’s origin wrt this system’s origin. If not specified, the origins are taken to be coincident.

vector_names, variable_names : iterable(optional)

Iterables of 3 strings each, with custom names for base vectors and base scalars of the new system respectively. Used for simple str printing.

See also

CoordSys3D.orient_new_body
method to orient via Euler angles

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q1, q2, q3 = symbols('q1 q2 q3')
>>> N = CoordSys3D('N')

To orient a coordinate system D with respect to N, each sequential rotation is always about N’s orthogonal unit vectors. For example, a ‘123’ rotation will specify rotations about N.i, then N.j, then N.k. Therefore,

>>> D = N.orient_new_space('D', q1, q2, q3, '312')

is same as

>>> B = N.orient_new_axis('B', q1, N.i)
>>> C = B.orient_new_axis('C', q2, N.j)
>>> D = C.orient_new_axis('D', q3, N.k)
position_wrt(other)[source]

Returns the position vector of the origin of this coordinate system with respect to another Point/CoordSys3D.

Parameters:

other : Point/CoordSys3D

If other is a Point, the position of this system’s origin wrt it is returned. If its an instance of CoordSyRect, the position wrt its origin is returned.

Examples

>>> from sympy.vector import CoordSys3D
>>> N = CoordSys3D('N')
>>> N1 = N.locate_new('N1', 10 * N.i)
>>> N.position_wrt(N1)
(-10)*N.i
rotation_matrix(other)[source]

Returns the direction cosine matrix(DCM), also known as the ‘rotation matrix’ of this coordinate system with respect to another system.

If v_a is a vector defined in system ‘A’ (in matrix format) and v_b is the same vector defined in system ‘B’, then v_a = A.rotation_matrix(B) * v_b.

A SymPy Matrix is returned.

Parameters:

other : CoordSys3D

The system which the DCM is generated to.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = CoordSys3D('N')
>>> A = N.orient_new_axis('A', q1, N.i)
>>> N.rotation_matrix(A)
Matrix([
[1,       0,        0],
[0, cos(q1), -sin(q1)],
[0, sin(q1),  cos(q1)]])
scalar_map(other)[source]

Returns a dictionary which expresses the coordinate variables (base scalars) of this frame in terms of the variables of otherframe.

Parameters:

otherframe : CoordSys3D

The other system to map the variables to.

Examples

>>> from sympy.vector import CoordSys3D
>>> from sympy import Symbol
>>> A = CoordSys3D('A')
>>> q = Symbol('q')
>>> B = A.orient_new_axis('B', q, A.k)
>>> A.scalar_map(B)
{A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}

Vector

class sympy.vector.vector.Vector[source]

Super class for all Vector classes. Ideally, neither this class nor any of its subclasses should be instantiated by the user.

components

Returns the components of this vector in the form of a Python dictionary mapping BaseVector instances to the corresponding measure numbers.

Examples

>>> from sympy.vector import CoordSys3D
>>> C = CoordSys3D('C')
>>> v = 3*C.i + 4*C.j + 5*C.k
>>> v.components
{C.i: 3, C.j: 4, C.k: 5}
cross(other)[source]

Returns the cross product of this Vector with another Vector or Dyadic instance. The cross product is a Vector, if ‘other’ is a Vector. If ‘other’ is a Dyadic, this returns a Dyadic instance.

Parameters:

other: Vector/Dyadic

The Vector or Dyadic we are crossing with.

Examples

>>> from sympy.vector import CoordSys3D
>>> C = CoordSys3D('C')
>>> C.i.cross(C.j)
C.k
>>> C.i ^ C.i
0
>>> v = 3*C.i + 4*C.j + 5*C.k
>>> v ^ C.i
5*C.j + (-4)*C.k
>>> d = C.i.outer(C.i)
>>> C.j.cross(d)
(-1)*(C.k|C.i)
dot(other)[source]

Returns the dot product of this Vector, either with another Vector, or a Dyadic, or a Del operator. If ‘other’ is a Vector, returns the dot product scalar (Sympy expression). If ‘other’ is a Dyadic, the dot product is returned as a Vector. If ‘other’ is an instance of Del, returns the directional derivative operator as a Python function. If this function is applied to a scalar expression, it returns the directional derivative of the scalar field wrt this Vector.

Parameters:

other: Vector/Dyadic/Del

The Vector or Dyadic we are dotting with, or a Del operator .

Examples

>>> from sympy.vector import CoordSys3D, Del
>>> C = CoordSys3D('C')
>>> delop = Del()
>>> C.i.dot(C.j)
0
>>> C.i & C.i
1
>>> v = 3*C.i + 4*C.j + 5*C.k
>>> v.dot(C.k)
5
>>> (C.i & delop)(C.x*C.y*C.z)
C.y*C.z
>>> d = C.i.outer(C.i)
>>> C.i.dot(d)
C.i
magnitude()[source]

Returns the magnitude of this vector.

normalize()[source]

Returns the normalized version of this vector.

outer(other)[source]

Returns the outer product of this vector with another, in the form of a Dyadic instance.

Parameters:

other : Vector

The Vector with respect to which the outer product is to be computed.

Examples

>>> from sympy.vector import CoordSys3D
>>> N = CoordSys3D('N')
>>> N.i.outer(N.j)
(N.i|N.j)
projection(other, scalar=False)[source]

Returns the vector or scalar projection of the ‘other’ on ‘self’.

Examples

>>> from sympy.vector.coordsysrect import CoordSys3D
>>> from sympy.vector.vector import Vector, BaseVector
>>> C = CoordSys3D('C')
>>> i, j, k = C.base_vectors()
>>> v1 = i + j + k
>>> v2 = 3*i + 4*j
>>> v1.projection(v2)
7/3*C.i + 7/3*C.j + 7/3*C.k
>>> v1.projection(v2, scalar=True)
7/3
separate()[source]

The constituents of this vector in different coordinate systems, as per its definition.

Returns a dict mapping each CoordSys3D to the corresponding constituent Vector.

Examples

>>> from sympy.vector import CoordSys3D
>>> R1 = CoordSys3D('R1')
>>> R2 = CoordSys3D('R2')
>>> v = R1.i + R2.i
>>> v.separate() == {R1: R1.i, R2: R2.i}
True
to_matrix(system)[source]

Returns the matrix form of this vector with respect to the specified coordinate system.

Parameters:

system : CoordSys3D

The system wrt which the matrix form is to be computed

Examples

>>> from sympy.vector import CoordSys3D
>>> C = CoordSys3D('C')
>>> from sympy.abc import a, b, c
>>> v = a*C.i + b*C.j + c*C.k
>>> v.to_matrix(C)
Matrix([
[a],
[b],
[c]])

Dyadic

class sympy.vector.dyadic.Dyadic[source]

Super class for all Dyadic-classes.

References

[R561]http://en.wikipedia.org/wiki/Dyadic_tensor
[R562]Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill
components

Returns the components of this dyadic in the form of a Python dictionary mapping BaseDyadic instances to the corresponding measure numbers.

cross(other)[source]

Returns the cross product between this Dyadic, and a Vector, as a Vector instance.

Parameters:

other : Vector

The Vector that we are crossing this Dyadic with

Examples

>>> from sympy.vector import CoordSys3D
>>> N = CoordSys3D('N')
>>> d = N.i.outer(N.i)
>>> d.cross(N.j)
(N.i|N.k)
dot(other)[source]

Returns the dot product(also called inner product) of this Dyadic, with another Dyadic or Vector. If ‘other’ is a Dyadic, this returns a Dyadic. Else, it returns a Vector (unless an error is encountered).

Parameters:

other : Dyadic/Vector

The other Dyadic or Vector to take the inner product with

Examples

>>> from sympy.vector import CoordSys3D
>>> N = CoordSys3D('N')
>>> D1 = N.i.outer(N.j)
>>> D2 = N.j.outer(N.j)
>>> D1.dot(D2)
(N.i|N.j)
>>> D1.dot(N.j)
N.i
to_matrix(system, second_system=None)[source]

Returns the matrix form of the dyadic with respect to one or two coordinate systems.

Parameters:

system : CoordSys3D

The coordinate system that the rows and columns of the matrix correspond to. If a second system is provided, this only corresponds to the rows of the matrix.

second_system : CoordSys3D, optional, default=None

The coordinate system that the columns of the matrix correspond to.

Examples

>>> from sympy.vector import CoordSys3D
>>> N = CoordSys3D('N')
>>> v = N.i + 2*N.j
>>> d = v.outer(N.i)
>>> d.to_matrix(N)
Matrix([
[1, 0, 0],
[2, 0, 0],
[0, 0, 0]])
>>> from sympy import Symbol
>>> q = Symbol('q')
>>> P = N.orient_new_axis('P', q, N.k)
>>> d.to_matrix(N, P)
Matrix([
[  cos(q),   -sin(q), 0],
[2*cos(q), -2*sin(q), 0],
[       0,         0, 0]])

Del

class sympy.vector.deloperator.Del[source]

Represents the vector differential operator, usually represented in mathematical expressions as the ‘nabla’ symbol.

cross(vect, doit=False)[source]

Represents the cross product between this operator and a given vector - equal to the curl of the vector field.

Parameters:

vect : Vector

The vector whose curl is to be calculated.

doit : bool

If True, the result is returned after calling .doit() on each component. Else, the returned expression contains Derivative instances

Examples

>>> from sympy.vector import CoordSys3D, Del
>>> C = CoordSys3D('C')
>>> delop = Del()
>>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
>>> delop.cross(v, doit = True)
(-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j +
    (-C.x*C.z + C.y*C.z)*C.k
>>> (delop ^ C.i).doit()
0
dot(vect, doit=False)[source]

Represents the dot product between this operator and a given vector - equal to the divergence of the vector field.

Parameters:

vect : Vector

The vector whose divergence is to be calculated.

doit : bool

If True, the result is returned after calling .doit() on each component. Else, the returned expression contains Derivative instances

Examples

>>> from sympy.vector import CoordSys3D, Del
>>> delop = Del()
>>> C = CoordSys3D('C')
>>> delop.dot(C.x*C.i)
Derivative(C.x, C.x)
>>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
>>> (delop & v).doit()
C.x*C.y + C.x*C.z + C.y*C.z
gradient(scalar_field, doit=False)[source]

Returns the gradient of the given scalar field, as a Vector instance.

Parameters:

scalar_field : SymPy expression

The scalar field to calculate the gradient of.

doit : bool

If True, the result is returned after calling .doit() on each component. Else, the returned expression contains Derivative instances

Examples

>>> from sympy.vector import CoordSys3D, Del
>>> C = CoordSys3D('C')
>>> delop = Del()
>>> delop.gradient(9)
0
>>> delop(C.x*C.y*C.z).doit()
C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k