Essential Classes

CoordinateSym

class sympy.physics.vector.frame.CoordinateSym[source]

A coordinate symbol/base scalar associated wrt a Reference Frame.

Ideally, users should not instantiate this class. Instances of this class must only be accessed through the corresponding frame as ‘frame[index]’.

CoordinateSyms having the same frame and index parameters are equal (even though they may be instantiated separately).

Parameters

name : string

The display name of the CoordinateSym

frame : ReferenceFrame

The reference frame this base scalar belongs to

index : 0, 1 or 2

The index of the dimension denoted by this coordinate variable

Examples

>>> from sympy.physics.vector import ReferenceFrame, CoordinateSym
>>> A = ReferenceFrame('A')
>>> A[1]
A_y
>>> type(A[0])
<class 'sympy.physics.vector.frame.CoordinateSym'>
>>> a_y = CoordinateSym('a_y', A, 1)
>>> a_y == A[1]
True

ReferenceFrame

class sympy.physics.vector.frame.ReferenceFrame(name, indices=None, latexs=None, variables=None)[source]

A reference frame in classical mechanics.

ReferenceFrame is a class used to represent a reference frame in classical mechanics. It has a standard basis of three unit vectors in the frame’s x, y, and z directions.

It also can have a rotation relative to a parent frame; this rotation is defined by a direction cosine matrix relating this frame’s basis vectors to the parent frame’s basis vectors. It can also have an angular velocity vector, defined in another frame.

ang_acc_in(otherframe)[source]

Returns the angular acceleration Vector of the ReferenceFrame.

Effectively returns the Vector: ^N alpha ^B which represent the angular acceleration of B in N, where B is self, and N is otherframe.

Parameters

otherframe : ReferenceFrame

The ReferenceFrame which the angular acceleration is returned in.

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> N = ReferenceFrame('N')
>>> A = ReferenceFrame('A')
>>> V = 10 * N.x
>>> A.set_ang_acc(N, V)
>>> A.ang_acc_in(N)
10*N.x
ang_vel_in(otherframe)[source]

Returns the angular velocity Vector of the ReferenceFrame.

Effectively returns the Vector: ^N omega ^B which represent the angular velocity of B in N, where B is self, and N is otherframe.

Parameters

otherframe : ReferenceFrame

The ReferenceFrame which the angular velocity is returned in.

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> N = ReferenceFrame('N')
>>> A = ReferenceFrame('A')
>>> V = 10 * N.x
>>> A.set_ang_vel(N, V)
>>> A.ang_vel_in(N)
10*N.x
dcm(otherframe)[source]

The direction cosine matrix between frames.

This gives the DCM between this frame and the otherframe. The format is N.xyz = N.dcm(B) * B.xyz A SymPy Matrix is returned.

Parameters

otherframe : ReferenceFrame

The otherframe which the DCM is generated to.

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = ReferenceFrame('N')
>>> A = N.orientnew('A', 'Axis', [q1, N.x])
>>> N.dcm(A)
Matrix([
[1,       0,        0],
[0, cos(q1), -sin(q1)],
[0, sin(q1),  cos(q1)]])
orient(parent, rot_type, amounts, rot_order='')[source]

Defines the orientation of this frame relative to a parent frame.

Parameters

parent : ReferenceFrame

The frame that this ReferenceFrame will have its orientation matrix defined in relation to.

rot_type : str

The type of orientation matrix that is being created. Supported types are ‘Body’, ‘Space’, ‘Quaternion’, ‘Axis’, and ‘DCM’. See examples for correct usage.

amounts : list OR value

The quantities that the orientation matrix will be defined by. In case of rot_type=’DCM’, value must be a sympy.matrices.MatrixBase object (or subclasses of it).

rot_order : str or int

If applicable, the order of a series of rotations.

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> from sympy import symbols, eye, ImmutableMatrix
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
>>> N = ReferenceFrame('N')
>>> B = ReferenceFrame('B')

Now we have a choice of how to implement the orientation. First is Body. Body orientation takes this reference frame through three successive simple rotations. 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.orient(N, 'Body', [q1, q2, q3], 123)
>>> B.orient(N, 'Body', [q1, q2, 0], 'ZXZ')
>>> B.orient(N, 'Body', [0, 0, 0], 'XYX')

Next is Space. Space is like Body, but the rotations are applied in the opposite order.

>>> B.orient(N, 'Space', [q1, q2, q3], '312')

Next is Quaternion. This orients the new ReferenceFrame 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.

>>> B.orient(N, 'Quaternion', [q0, q1, q2, q3])

Next is Axis. This is a rotation about an arbitrary, non-time-varying axis by some angle. The axis is supplied as a Vector. This is how simple rotations are defined.

>>> B.orient(N, 'Axis', [q1, N.x + 2 * N.y])

Last is DCM (Direction Cosine Matrix). This is a rotation matrix given manually.

>>> B.orient(N, 'DCM', eye(3))
>>> B.orient(N, 'DCM', ImmutableMatrix([[0, 1, 0], [0, 0, -1], [-1, 0, 0]]))
orientnew(newname, rot_type, amounts, rot_order='', variables=None, indices=None, latexs=None)[source]

Creates a new ReferenceFrame oriented with respect to this Frame.

See ReferenceFrame.orient() for acceptable rotation types, amounts, and orders. Parent is going to be self.

Parameters

newname : str

The name for the new ReferenceFrame

rot_type : str

The type of orientation matrix that is being created.

amounts : list OR value

The quantities that the orientation matrix will be defined by.

rot_order : str

If applicable, the order of a series of rotations.

Examples

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

Now we have a choice of how to implement the orientation. First is Body. Body orientation takes this reference frame through three successive simple rotations. 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.

>>> A = N.orientnew('A', 'Body', [q1, q2, q3], '123')
>>> A = N.orientnew('A', 'Body', [q1, q2, 0], 'ZXZ')
>>> A = N.orientnew('A', 'Body', [0, 0, 0], 'XYX')

Next is Space. Space is like Body, but the rotations are applied in the opposite order.

>>> A = N.orientnew('A', 'Space', [q1, q2, q3], '312')

Next is Quaternion. This orients the new ReferenceFrame 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.

>>> A = N.orientnew('A', 'Quaternion', [q0, q1, q2, q3])

Last is Axis. This is a rotation about an arbitrary, non-time-varying axis by some angle. The axis is supplied as a Vector. This is how simple rotations are defined.

>>> A = N.orientnew('A', 'Axis', [q1, N.x])
partial_velocity(frame, *gen_speeds)[source]

Returns the partial angular velocities of this frame in the given frame with respect to one or more provided generalized speeds.

Parameters

frame : ReferenceFrame

The frame with which the angular velocity is defined in.

gen_speeds : functions of time

The generalized speeds.

Returns

partial_velocities : tuple of Vector

The partial angular velocity vectors corresponding to the provided generalized speeds.

Examples

>>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
>>> N = ReferenceFrame('N')
>>> A = ReferenceFrame('A')
>>> u1, u2 = dynamicsymbols('u1, u2')
>>> A.set_ang_vel(N, u1 * A.x + u2 * N.y)
>>> A.partial_velocity(N, u1)
A.x
>>> A.partial_velocity(N, u1, u2)
(A.x, N.y)
set_ang_acc(otherframe, value)[source]

Define the angular acceleration Vector in a ReferenceFrame.

Defines the angular acceleration of this ReferenceFrame, in another. Angular acceleration can be defined with respect to multiple different ReferenceFrames. Care must be taken to not create loops which are inconsistent.

Parameters

otherframe : ReferenceFrame

A ReferenceFrame to define the angular acceleration in

value : Vector

The Vector representing angular acceleration

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> N = ReferenceFrame('N')
>>> A = ReferenceFrame('A')
>>> V = 10 * N.x
>>> A.set_ang_acc(N, V)
>>> A.ang_acc_in(N)
10*N.x
set_ang_vel(otherframe, value)[source]

Define the angular velocity vector in a ReferenceFrame.

Defines the angular velocity of this ReferenceFrame, in another. Angular velocity can be defined with respect to multiple different ReferenceFrames. Care must be taken to not create loops which are inconsistent.

Parameters

otherframe : ReferenceFrame

A ReferenceFrame to define the angular velocity in

value : Vector

The Vector representing angular velocity

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> N = ReferenceFrame('N')
>>> A = ReferenceFrame('A')
>>> V = 10 * N.x
>>> A.set_ang_vel(N, V)
>>> A.ang_vel_in(N)
10*N.x
variable_map(otherframe)[source]

Returns a dictionary which expresses the coordinate variables of this frame in terms of the variables of otherframe.

If Vector.simp is True, returns a simplified version of the mapped values. Else, returns them without simplification.

Simplification of the expressions may take time.

Parameters

otherframe : ReferenceFrame

The other frame to map the variables to

Examples

>>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
>>> A = ReferenceFrame('A')
>>> q = dynamicsymbols('q')
>>> B = A.orientnew('B', 'Axis', [q, A.z])
>>> A.variable_map(B)
{A_x: B_x*cos(q(t)) - B_y*sin(q(t)), A_y: B_x*sin(q(t)) + B_y*cos(q(t)), A_z: B_z}
x

The basis Vector for the ReferenceFrame, in the x direction.

y

The basis Vector for the ReferenceFrame, in the y direction.

z

The basis Vector for the ReferenceFrame, in the z direction.

Vector

class sympy.physics.vector.vector.Vector(inlist)[source]

The class used to define vectors.

It along with ReferenceFrame are the building blocks of describing a classical mechanics system in PyDy and sympy.physics.vector.

Attributes

simp

(Boolean) Let certain methods use trigsimp on their outputs

applyfunc(f)[source]

Apply a function to each component of a vector.

cross(other)[source]

The cross product operator for two Vectors.

Returns a Vector, expressed in the same ReferenceFrames as self.

Parameters

other : Vector

The Vector which we are crossing with

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = ReferenceFrame('N')
>>> N.x ^ N.y
N.z
>>> A = N.orientnew('A', 'Axis', [q1, N.x])
>>> A.x ^ N.y
N.z
>>> N.y ^ A.x
- sin(q1)*A.y - cos(q1)*A.z
diff(var, frame, var_in_dcm=True)[source]

Returns the partial derivative of the vector with respect to a variable in the provided reference frame.

Parameters

var : Symbol

What the partial derivative is taken with respect to.

frame : ReferenceFrame

The reference frame that the partial derivative is taken in.

var_in_dcm : boolean

If true, the differentiation algorithm assumes that the variable may be present in any of the direction cosine matrices that relate the frame to the frames of any component of the vector. But if it is known that the variable is not present in the direction cosine matrices, false can be set to skip full reexpression in the desired frame.

Examples

>>> from sympy import Symbol
>>> from sympy.physics.vector import dynamicsymbols, ReferenceFrame
>>> from sympy.physics.vector import Vector
>>> Vector.simp = True
>>> t = Symbol('t')
>>> q1 = dynamicsymbols('q1')
>>> N = ReferenceFrame('N')
>>> A = N.orientnew('A', 'Axis', [q1, N.y])
>>> A.x.diff(t, N)
- q1'*A.z
>>> B = ReferenceFrame('B')
>>> u1, u2 = dynamicsymbols('u1, u2')
>>> v = u1 * A.x + u2 * B.y
>>> v.diff(u2, N, var_in_dcm=False)
B.y
doit(**hints)[source]

Calls .doit() on each term in the Vector

dot(other)[source]

Dot product of two vectors.

Returns a scalar, the dot product of the two Vectors

Parameters

other : Vector

The Vector which we are dotting with

Examples

>>> from sympy.physics.vector import ReferenceFrame, dot
>>> from sympy import symbols
>>> q1 = symbols('q1')
>>> N = ReferenceFrame('N')
>>> dot(N.x, N.x)
1
>>> dot(N.x, N.y)
0
>>> A = N.orientnew('A', 'Axis', [q1, N.x])
>>> dot(N.y, A.y)
cos(q1)
dt(otherframe)[source]

Returns a Vector which is the time derivative of the self Vector, taken in frame otherframe.

Calls the global time_derivative method

Parameters

otherframe : ReferenceFrame

The frame to calculate the time derivative in

express(otherframe, variables=False)[source]

Returns a Vector equivalent to this one, expressed in otherframe. Uses the global express method.

Parameters

otherframe : ReferenceFrame

The frame for this Vector to be described in

variables : boolean

If True, the coordinate symbols(if present) in this Vector are re-expressed in terms otherframe

Examples

>>> from sympy.physics.vector import ReferenceFrame, Vector, dynamicsymbols
>>> q1 = dynamicsymbols('q1')
>>> N = ReferenceFrame('N')
>>> A = N.orientnew('A', 'Axis', [q1, N.y])
>>> A.x.express(N)
cos(q1)*N.x - sin(q1)*N.z
free_symbols(reference_frame)[source]

Returns the free symbols in the measure numbers of the vector expressed in the given reference frame.

Parameter

reference_frameReferenceFrame

The frame with respect to which the free symbols of the given vector is to be determined.

magnitude()[source]

Returns the magnitude (Euclidean norm) of self.

normalize()[source]

Returns a Vector of magnitude 1, codirectional with self.

outer(other)[source]

Outer product between two Vectors.

A rank increasing operation, which returns a Dyadic from two Vectors

Parameters

other : Vector

The Vector to take the outer product with

Examples

>>> from sympy.physics.vector import ReferenceFrame, outer
>>> N = ReferenceFrame('N')
>>> outer(N.x, N.x)
(N.x|N.x)
separate()[source]

The constituents of this vector in different reference frames, as per its definition.

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

Examples

>>> from sympy.physics.vector import ReferenceFrame
>>> R1 = ReferenceFrame('R1')
>>> R2 = ReferenceFrame('R2')
>>> v = R1.x + R2.x
>>> v.separate() == {R1: R1.x, R2: R2.x}
True
simplify()[source]

Returns a simplified Vector.

subs(*args, **kwargs)[source]

Substitution on the Vector.

Examples

>>> from sympy.physics.vector import ReferenceFrame
>>> from sympy import Symbol
>>> N = ReferenceFrame('N')
>>> s = Symbol('s')
>>> a = N.x * s
>>> a.subs({s: 2})
2*N.x
to_matrix(reference_frame)[source]

Returns the matrix form of the vector with respect to the given frame.

Parameters

reference_frame : ReferenceFrame

The reference frame that the rows of the matrix correspond to.

Returns

matrix : ImmutableMatrix, shape(3,1)

The matrix that gives the 1D vector.

Examples

>>> from sympy import symbols
>>> from sympy.physics.vector import ReferenceFrame
>>> from sympy.physics.mechanics.functions import inertia
>>> a, b, c = symbols('a, b, c')
>>> N = ReferenceFrame('N')
>>> vector = a * N.x + b * N.y + c * N.z
>>> vector.to_matrix(N)
Matrix([
[a],
[b],
[c]])
>>> beta = symbols('beta')
>>> A = N.orientnew('A', 'Axis', (beta, N.x))
>>> vector.to_matrix(A)
Matrix([
[                         a],
[ b*cos(beta) + c*sin(beta)],
[-b*sin(beta) + c*cos(beta)]])

Dyadic

class sympy.physics.vector.dyadic.Dyadic(inlist)[source]

A Dyadic object.

See: https://en.wikipedia.org/wiki/Dyadic_tensor Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill

A more powerful way to represent a rigid body’s inertia. While it is more complex, by choosing Dyadic components to be in body fixed basis vectors, the resulting matrix is equivalent to the inertia tensor.

applyfunc(f)[source]

Apply a function to each component of a Dyadic.

cross(other)

For a cross product in the form: Dyadic x Vector.

Parameters

other : Vector

The Vector that we are crossing this Dyadic with

Examples

>>> from sympy.physics.vector import ReferenceFrame, outer, cross
>>> N = ReferenceFrame('N')
>>> d = outer(N.x, N.x)
>>> cross(d, N.y)
(N.x|N.z)
doit(**hints)[source]

Calls .doit() on each term in the Dyadic

dot(other)

The inner product operator for a Dyadic and a Dyadic or Vector.

Parameters

other : Dyadic or Vector

The other Dyadic or Vector to take the inner product with

Examples

>>> from sympy.physics.vector import ReferenceFrame, outer
>>> N = ReferenceFrame('N')
>>> D1 = outer(N.x, N.y)
>>> D2 = outer(N.y, N.y)
>>> D1.dot(D2)
(N.x|N.y)
>>> D1.dot(N.y)
N.x
dt(frame)[source]

Take the time derivative of this Dyadic in a frame.

This function calls the global time_derivative method

Parameters

frame : ReferenceFrame

The frame to take the time derivative in

Examples

>>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
>>> N = ReferenceFrame('N')
>>> q = dynamicsymbols('q')
>>> B = N.orientnew('B', 'Axis', [q, N.z])
>>> d = outer(N.x, N.x)
>>> d.dt(B)
- q'*(N.y|N.x) - q'*(N.x|N.y)
express(frame1, frame2=None)[source]

Expresses this Dyadic in alternate frame(s)

The first frame is the list side expression, the second frame is the right side; if Dyadic is in form A.x|B.y, you can express it in two different frames. If no second frame is given, the Dyadic is expressed in only one frame.

Calls the global express function

Parameters

frame1 : ReferenceFrame

The frame to express the left side of the Dyadic in

frame2 : ReferenceFrame

If provided, the frame to express the right side of the Dyadic in

Examples

>>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
>>> N = ReferenceFrame('N')
>>> q = dynamicsymbols('q')
>>> B = N.orientnew('B', 'Axis', [q, N.z])
>>> d = outer(N.x, N.x)
>>> d.express(B, N)
cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)
simplify()[source]

Returns a simplified Dyadic.

subs(*args, **kwargs)[source]

Substitution on the Dyadic.

Examples

>>> from sympy.physics.vector import ReferenceFrame
>>> from sympy import Symbol
>>> N = ReferenceFrame('N')
>>> s = Symbol('s')
>>> a = s*(N.x|N.x)
>>> a.subs({s: 2})
2*(N.x|N.x)
to_matrix(reference_frame, second_reference_frame=None)[source]

Returns the matrix form of the dyadic with respect to one or two reference frames.

Parameters

reference_frame : ReferenceFrame

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

second_reference_frame : ReferenceFrame, optional, default=None

The reference frame that the columns of the matrix correspond to.

Returns

matrix : ImmutableMatrix, shape(3,3)

The matrix that gives the 2D tensor form.

Examples

>>> from sympy import symbols
>>> from sympy.physics.vector import ReferenceFrame, Vector
>>> Vector.simp = True
>>> from sympy.physics.mechanics import inertia
>>> Ixx, Iyy, Izz, Ixy, Iyz, Ixz = symbols('Ixx, Iyy, Izz, Ixy, Iyz, Ixz')
>>> N = ReferenceFrame('N')
>>> inertia_dyadic = inertia(N, Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
>>> inertia_dyadic.to_matrix(N)
Matrix([
[Ixx, Ixy, Ixz],
[Ixy, Iyy, Iyz],
[Ixz, Iyz, Izz]])
>>> beta = symbols('beta')
>>> A = N.orientnew('A', 'Axis', (beta, N.x))
>>> inertia_dyadic.to_matrix(A)
Matrix([
[                           Ixx,                                           Ixy*cos(beta) + Ixz*sin(beta),                                           -Ixy*sin(beta) + Ixz*cos(beta)],
[ Ixy*cos(beta) + Ixz*sin(beta), Iyy*cos(2*beta)/2 + Iyy/2 + Iyz*sin(2*beta) - Izz*cos(2*beta)/2 + Izz/2,                 -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2],
[-Ixy*sin(beta) + Ixz*cos(beta),                -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2, -Iyy*cos(2*beta)/2 + Iyy/2 - Iyz*sin(2*beta) + Izz*cos(2*beta)/2 + Izz/2]])