import sympy as sm
import sympy.physics.mechanics as me
Create some measure numbers. It is good practice to declare symbols as real to encourage SymPy to provide simpler results.
v1, v2, v3 = sm.symbols('v1, v2, v3', real=True)
A reference frame must be created before constructing vectors.
A = me.ReferenceFrame('A')
The three orthornormal vectors (orthogonal unit vectors) associated with the frame can be accessed with the.x,.y,.z
attributes and will be used to construct vectors.
A.x
A.y
A.z
Vectors are created by multiplying measure numbers by a frame's associated unit vectors.
v = v1*A.x + v2*A.y + v3*A.z
The vectors stored in variable v
can be displayed:
v
There are alternative ways for accessing the frames unit vectors and how they display. See the help for details.
me.ReferenceFrame?
Init signature: me.ReferenceFrame(name, indices=None, latexs=None, variables=None) Docstring: 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. Init docstring: ReferenceFrame initialization method. A ReferenceFrame has a set of orthonormal basis vectors, along with orientations relative to other ReferenceFrames and angular velocities relative to other ReferenceFrames. Parameters ========== indices : list (of strings) If custom indices are desired for console, pretty, and LaTeX printing, supply three as a list. The basis vectors can then be accessed with the get_item method. latexs : list (of strings) If custom names are desired for LaTeX printing of each basis vector, supply the names here in a list. Examples ======== >>> from sympy.physics.vector import ReferenceFrame, vlatex >>> N = ReferenceFrame('N') >>> N.x N.x >>> O = ReferenceFrame('O', indices=('1', '2', '3')) >>> O.x O['1'] >>> O['1'] O['1'] >>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3')) >>> vlatex(P.x) 'A1' File: /opt/conda/lib/python3.6/site-packages/sympy/physics/vector/frame.py Type: type
For example, if you prefer a 1, 2, and 3 instead of x, y, z for the subscripts you can change them like so:
B = me.ReferenceFrame('B', indices=['1', '2', '3'])
B.x
The unit vector can be accessed using the square brackets and the indice if preferred.
B['1']
v
The scalar magnitude of the vector can be found:
v.magnitude()
And a unit vector in the same direction as $\mathbf{v}$ is found with:
v.normalize()
This is equivalent to:
v / v.magnitude()
Create another vector expressed in $A$.
b1, b2, b3 = sm.symbols("b1, b2, b3", real=True)
w = b1*A.x + b2*A.y + b3 * A.z
w
You can add vectors:
v + w
v + v
Multipy by scalars:
10 * v
Compute the scaler dot product between to vectors:
v.dot(v)
v.dot(w)
Cross products:
v.cross(w)
You can also comput the outer product, which creats a dyadic (a reference frame aware resprenstation of a 3D tensor)
v.outer(w)
The matrix form of both vectors and dyadics can be found if the frame of interest is provided.
v.to_matrix(A)
d = v.outer(w)
d.to_matrix(A)
You can find all of the scalar variables present in a vector with free_symbols()
.
v.free_symbols(A)
{v1, v2, v3}
Parital derivatives of vectors can be computed in the specified refrence frame.
v.diff(v2, A)
You can substitute values or other symbols into the measure number expressions.
v.subs({v1: 1.34, v2: 5})
z = v.subs({v1: 1.34, v2: 5})
z.subs({5: v2})
v1
v2
m = v.to_matrix(A)
m.subs({v1: 5})
m
m = v.subs({v1: 1, v2: 5})
m
v