Is there a way to express an symbolic expression involving vectors and operations on them without evaluating them?
from sympy import symbols, acos, MatrixSymbol, Matrix
rA = MatrixSymbol("r^A", 2, 1)
rB = MatrixSymbol("r^B", 2, 1)
rA, rB = Matrix(rA), Matrix(rB) # Basically I want to skip this step
acos((rA.dot(rB)) / rA.norm())
This will evaluate the expression to:
⎛r_00__A⋅r_00__B + r_10__A⋅r_10__B ⎞
acos⎜─────────────────────────────────⎟
⎜ _________________________ ⎟
⎜ ╱ 2 2 ⎟
⎝ ╲╱ │r_00__A│ + │r_10__A│ ⎠
But instead I would like it to evaluate to something like this, while still being able to substitute the symbolic Vectors later on.
⎛ r_A⋅r_B ⎞
acos⎜─────────⎟
⎝ ||r_A|| ⎠
You'll just have to use the equation Eq(v. norm(), 1) in some way, at whatever step of computations requires using the information that v is a unit vector. It's probably easier for SymPy to use the equation in the form Eq(v. norm()**2, 1) , without the square root.
To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .
Note that by default in SymPy the base of the natural logarithm is E (capital E ). That is, exp(x) is the same as E**x .
To actually compute the transpose, use the transpose() function, or the . T attribute of matrices. Represents the trace of a matrix expression.
You could replace .dot
and .norm
by matrix product : acos(A.T*B/sqrt(A.T*A))
.
Minimal working example :
In [1]: from sympy import *
In [2]: init_printing(use_unicode=True)
In [3]: init_printing(use_latex=False)
In [4]: A = MatrixSymbol('A', 2, 1)
In [5]: B = MatrixSymbol('B', 2, 1)
In [6]: acos(A.T*B/sqrt(A.T*A))
Out[6]:
⎛ -1/2⎞
⎜ T ⎛ T ⎞ ⎟
acos⎝A ⋅B⋅⎝A ⋅A⎠ ⎠
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With