Essential Classes in sympy.vector (doctrings)

CoordSysCartesian

class sympy.vector.coordsysrect.CoordSysCartesian(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)[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)[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 CoordSysCartesian instance.

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 : CoordSysCartesian

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.

locate_new(name, position, vector_names=None, variable_names=None)[source]

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

Parameters:

name : str

The name of the new CoordSysCartesian 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 CoordSysCartesian
>>> A = CoordSysCartesian('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 CoordSysCartesian 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 CoordSysCartesian 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 CoordSysCartesian
>>> from sympy import symbols
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
>>> N = CoordSysCartesian('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 CoordSysCartesian
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = CoordSysCartesian('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 CoordSysCartesian
>>> from sympy import symbols
>>> q1, q2, q3 = symbols('q1 q2 q3')
>>> N = CoordSysCartesian('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 CoordSysCartesian 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 CoordSysCartesian
>>> from sympy import symbols
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
>>> N = CoordSysCartesian('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

CoordSysCartesian.orient_new_body
method to orient via Euler angles

Examples

>>> from sympy.vector import CoordSysCartesian
>>> from sympy import symbols
>>> q1, q2, q3 = symbols('q1 q2 q3')
>>> N = CoordSysCartesian('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(*args, **kwargs)[source]

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

Parameters:

other : Point/CoordSysCartesian

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 CoordSysCartesian
>>> N = CoordSysCartesian('N')
>>> N1 = N.locate_new('N1', 10 * N.i)
>>> N.position_wrt(N1)
(-10)*N.i
rotation_matrix(*args, **kwargs)[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 : CoordSysCartesian

The system which the DCM is generated to.

Examples

>>> from sympy.vector import CoordSysCartesian
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = CoordSysCartesian('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 : CoordSysCartesian

The other system to map the variables to.

Examples

>>> from sympy.vector import CoordSysCartesian
>>> from sympy import Symbol
>>> A = CoordSysCartesian('A')
>>> q = Symbol('q')
>>> B = A.orient_new_axis('B', q, A.k)
>>> A.scalar_map(B)
{A.x: -sin(q)*B.y + cos(q)*B.x, A.y: sin(q)*B.x + cos(q)*B.y, 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 CoordSysCartesian
>>> C = CoordSysCartesian('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 CoordSysCartesian
>>> C = CoordSysCartesian('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 derivate 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 CoordSysCartesian
>>> C = CoordSysCartesian('C')
>>> 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 & C.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 CoordSysCartesian
>>> N = CoordSysCartesian('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 CoordSysCartesian
>>> from sympy.vector.vector import Vector, BaseVector
>>> C = CoordSysCartesian('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 CoordSysCartesian to the corresponding constituent Vector.

Examples

>>> from sympy.vector import CoordSysCartesian
>>> R1 = CoordSysCartesian('R1')
>>> R2 = CoordSysCartesian('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 : CoordSysCartesian

The system wrt which the matrix form is to be computed

Examples

>>> from sympy.vector import CoordSysCartesian
>>> C = CoordSysCartesian('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

[R537]http://en.wikipedia.org/wiki/Dyadic_tensor
[R538]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 CoordSysCartesian
>>> N = CoordSysCartesian('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 CoordSysCartesian
>>> N = CoordSysCartesian('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 : CoordSysCartesian

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 : CoordSysCartesian, optional, default=None

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

Examples

>>> from sympy.vector import CoordSysCartesian
>>> N = CoordSysCartesian('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 CoordSysCartesian
>>> C = CoordSysCartesian('C')
>>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
>>> C.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
>>> (C.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 CoordSysCartesian
>>> C = CoordSysCartesian('C')
>>> 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)
>>> (C.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 CoordSysCartesian
>>> C = CoordSysCartesian('C')
>>> C.delop.gradient(9)
(Derivative(9, C.x))*C.i + (Derivative(9, C.y))*C.j +
    (Derivative(9, C.z))*C.k
>>> C.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