sympyhelpers package

Submodules

sympyhelpers.sympyhelpers module

sympy helper functions

sympyhelpers.sympyhelpers.DCM2EulerAngSet(DCM, rotSet, body=True)[source]
Parameters:
  • DCM (sympy.matrices.dense.MutableDenseMatrix) – Direction Cosine Matrix

  • rotSet (iterable) – 3-element iterable defining order of rotations of a body Euler angle set. Indexing is 1-based, so valid rotation sets may only contains 1, 2, or 3. A valid rotation set contains exactly 3 elements, at least 2 of which are distinct, and with no rotations about the same axis repeated in a row. [1, 2, 3] and [1, 3, 1] are valid, but [1, 1, 2] is not.

  • body (bool) – True for body rotations, False for space rotations. Defaults to True.

Returns:

sympyhelpers.sympyhelpers.DCM2angVel(dcm, diffmap, diffby=t)[source]

Given a direction cosine matrix \({}^\mathcal{B}C^\mathcal{A}\) compute the angular velocity \({}^\mathcal{I}\boldsymbol{\omega}^\mathcal{B}\)

Parameters:
Returns:

Angular velocity in components of frame B

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.DCM2axang(DCM)[source]

Given a direction cosine matrix \({}^\mathcal{B}C^\mathcal{A}\) compute the axis and angle of the rotation. Inverse of calcDCM.

Parameters:

DCM (sympy.matrices.dense.MutableDenseMatrix) – Direction cosine matrix transforming vector components from frame A to frame B

Returns:

n (sympy.matrices.dense.MutableDenseMatrix):

3x1 matrix representation of the unit vector of the axis of rotation

th (sympy.core.*):

Expression for the angle of rotation

Return type:

tuple

sympyhelpers.sympyhelpers.EulerAngSet(rotSet, angs)[source]

Calculate the equivalent direction cosine matrix for a body Euler Angle set

Parameters:
  • rotSet (iterable) – 3-element iterable defining order of rotations of a body Euler angle set. For example, a Body-2 3-1-3 rotation would be [3,1,3] and a Body-3 3-2-1 rotation would be [3,2,1].

  • angs (iterable) – 3-elements iterable of symbols or expressions defining the angle of each rotation.

Returns:

The equivalent direction cosine matrix \({}^\mathcal{B}C^\mathcal{A}\)

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.EulerLagrange(L, qs, diffmap, diffby=t)[source]

Apply the Euler-Lagrange equations

Parameters:
  • L (sympy.core.*) – Any expression representing the Lagrangian

  • qs (iterable) – The generalized coordinates. Must be an iterable even if there is only one coordinate

  • diffmap (dict) – Dictionary of dependent variables and their first derivatives

  • diffby (sympy.core.symbol.Symbol) – Inependent variable to differentiate with respect to. Defaults to t

Returns:

Equations of motion

Return type:

dict

sympyhelpers.sympyhelpers.calcDCM(n, th)[source]

Compute a direction cosine matrix via the Euler-Rodrigues equation. Evaluates the DCM \({}^\mathcal{A}C^\mathcal{B}\) for frames \(\mathcal{A}\) and \(\mathcal{B}\) for axis of rotation \(\mathbf{n}\) and angle \(\theta\)

Parameters:
Returns:

The direction cosine matrix

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.difftotal(expr, diffby, diffmap)[source]

Take the total derivative with respect to a variable of an expression where dependent variables are not defined as functions of that variable

Parameters:
  • expr (sympy.core.*) – Any valid sympy expression.

  • diffby (sympy.core.symbol.Symbol) – Inependent variable to differentiate with respect to

  • diffmap (dict) – Dictionary of dependent variables and their first derivatives

Returns:

Differentiated expression

Return type:

sympy.core.*

Examples

>>> theta, t, theta_dot = symbols("theta t theta_dot")
>>> difftotal(cos(theta), t, {theta: theta_dot})
-theta_dot*sin(theta)

Note

Adapted from code by Chris Wagner http://robotfantastic.org/total-derivatives-in-sympy.html

sympyhelpers.sympyhelpers.difftotalmat(mat, diffby, diffmap)[source]

Apply total derivative element by element to a matrix

Parameters:
Returns:

Differentiated matrix (same dimensions as input matrix)

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.fancyMat(prefix, shape)[source]

Create an indexed 2D matrix akin to symarray :param prefix: string representation of symbol to use as the matrix contents :type prefix: str :param shape: 2-element iterable of the shape of the matrix :type shape: iterable

Returns:

The matrix

Return type:

sympy.matrices.dense.MutableDenseMatrix

Note

Indexing is 1-based.

Examples

>>> fancyMat('{}^\mathcal{B}C^{\mathcal{A}}',(3,3))
Matrix([
[{}^\mathcal{B}C^{\mathcal{A}}_{11}, {}^\mathcal{B}C^{\mathcal{A}}_{12}, {}^\mathcal{B}C^{\mathcal{A}}_{13}],
[{}^\mathcal{B}C^{\mathcal{A}}_{21}, {}^\mathcal{B}C^{\mathcal{A}}_{22}, {}^\mathcal{B}C^{\mathcal{A}}_{23}],
[{}^\mathcal{B}C^{\mathcal{A}}_{31}, {}^\mathcal{B}C^{\mathcal{A}}_{32}, {}^\mathcal{B}C^{\mathcal{A}}_{33}]])
sympyhelpers.sympyhelpers.fancyVec(prefix, n)[source]

Create an indexed column matrix akin to symarray

Parameters:
  • prefix (str) – string representation of symbol to use as the matrix contents

  • n (int) – Dimension of vector

Returns:

The matrix representation of the vector

Return type:

sympy.matrices.dense.MutableDenseMatrix

Note

Indexing is 1-based.

Examples

>>> fancyVec('a',3)
Matrix([
[a_{1}],
[a_{2}],
[a_{3}]])
sympyhelpers.sympyhelpers.genRefFrame(basis, hat=True, commutative=False)[source]

Generate symbols corresponding to unit vectors of a reference frame

Parameters:
  • basis (str) – Common character of unit vectors. For example, basis = ‘e’ results in a basis set of: ‘mathbf{hat{e}}_1, mathbf{hat{e}}_2, mathbf{hat{e}}_3’

  • hat (bool) – If true, basis vectors are typeset as bold and hatted (e.g. \(\mathbf{\hat{e}}_1\). If false, vectors are only bolded. Defaults True.

  • commutative (bool) – Sets commutativity of unit vectors. Defaults to False. This is useful for display purposes, but will prevent these from being used in solving vector expressions.

Returns:

sympy.Symbol

sympyhelpers.sympyhelpers.gendiffvars(syms, real=True)[source]

Generate symbolic variables and their derivatives

Parameters:
  • syms (iterable) – List (or any iterable) of symbols to create. Each element is either a string or another iterable, whose contents are: (variable name, [symbol name], [number of derivatives]) If number of derivatives is not set, 2 is assumed. If symbol name is not set use the same name for the variable and the symbol. Thus, an input of ‘x’ is equivalent to (‘x’, ‘x’, 2).

  • real (bool) – True for real value.

Returns:

allsyms (dict):

All generated symbols (can be used to populate calling namespace)

diffmap (dict):

Differentiation map

Return type:

tuple

Examples

>>> # create 2nd order derivatives for theta and phi:
>>> allsyms, diffmap = gendiffvars([('th','theta'), ('ph', 'phi')])
>>> locals().update(allsyms)

Note

When the name of a symbol includes an underscore, the ‘dot’ will be placed preceeding the underscore such that the leading term is dotted. However, the corresponding variable name will always have the ‘d’ placed at the end, regardless of whether the variable name includes and underscore. Thus, a variable definition like (‘th_1’, ‘theta_1’) will result in the first derivative being named thetadot_1 and assigned to varaible th_1d. To avoid confusion, it is recommended to avoid underscores in variable names - e.g., for this example to define the variable as (‘th1’, ‘theta_1’), which would result in a variable th1d mapping to a symbol with name ‘thetadot_1’.

sympyhelpers.sympyhelpers.mat2vec(mat, basis='e', hat=True)[source]

Transform matrix representation of a vector to the vector equation for a given basis

Parameters:
  • mat (sympy.matrices.dense.MutableDenseMatrix) – 3x1 Matrix representation of a vector in components of some frame

  • basis (str or iterable) – If a string, generate unit vector basis for the frame as basis_i (e.g. ‘e’ becomes basis e_1,e_2,e_3). If an iterable of strings (must be of length 3) use directly as the basis representation. For example, the default (basis = ‘e’) results in a basis set of: ‘mathbf{hat{e}}_1, mathbf{hat{e}}_2, mathbf{hat{e}}_3’ If basis is an iterable, then the contents are used exactly to represent the basis vectors.

  • hat (bool) – Only applies if basis input is a string. If set, basis vectors are typeset as bold and hatted (e.g. \(\mathbf{\hat{e}}_1\). If false, vectors are only bolded. Defaults True.

Returns:

The full vector in the specified basis (reference frame).

Return type:

sympy.Add

sympyhelpers.sympyhelpers.parallelAxis(I_G, r_QG, m)[source]

Applies the parallel axis theorem to matrix of inertia I_G (where G is the body’s center of mass) to find the matrix of inertia I_Q where the vector from G to Q is r_QG and the total mass of the body is m. I_G and I_QG are assumed to be in components of the same (body-fixed) frame

Parameters:
Returns:

resulting matrix of inertia about Q in components of the same body-fixed frame

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.rodriguesEq(nhat, th)[source]

This is a wrapper for calcDCM, but computes the \({}^\mathcal{B}C^\mathcal{A}\) matrix (the inverse/trasnpose of calcDCM).

Parameters:
Returns:

The direction cosine matrix

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.rotMat(axis, angle)[source]

Generate direction cosine matrix \({}^\mathcal{B}C^\mathcal{A}\) about one of the unit directions defining frame \(\mathcal{A}\)

Parameters:
Returns:

The DCM

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.skew(v)[source]

Compute the skew-symmetric cross-produce equivalent matrix of a vector

Parameters:

v (sympy.matrices.dense.MutableDenseMatrix) – 3x1 matrix representation of a vector with respect to some reference frame

Returns:

Cross-product equivalent matrix of the vector

Return type:

sympy.matrices.dense.MutableDenseMatrix

sympyhelpers.sympyhelpers.transportEq(vec, diffby, diffmap, omega)[source]

Apply the transport equation to a vector. For any pair of reference frames \(\mathcal{I}\) and \(\mathcal{B}\) and any vector \(\mathbf{c}\):

\[\vphantom{\frac{d}{d}}^{\mathcal{I}}\frac{\mathrm{d}}{\mathrm{d}t} \mathbf{c}= \vphantom{\frac{d}{d}}^{\mathcal{B}}\frac{\mathrm{d}}{\mathrm{d}t} \mathbf{c} + {}^\mathcal{I}\boldsymbol{\omega}^\mathcal{B} \times \mathbf{c}\]
Parameters:

Module contents