Sympy Cross and dot product without evaluation
Check out vector package I didn’t find any way to get the cross/dot product of two vectors without evaluating the expression, e.g. without simplifying the duplicates in the result of the operation. Is this possible?
Solution
Let’s say you have the latest development version:
In the vector module there are dot and cross functions to calculate the dot
product and cross
product, classes Dot
and Cross
, creating unevaluated expressions representing the same product.
Import vector modules and SymPy:
In [1]: from sympy import *; from sympy.vector import *
Define coordinate system:
In [2]: C = CoordSys3D("C")
In this case, C.i, C.j, C.k
are the basis vectors.
Cross product with immediate evaluation (lowercase cross
):
In [3]: cross(C.i, C.j)
Out[3]: C.k
Let’s print the cross product using a pretty-print operator :
In [4]: init_printing()
The cross product in uncalculated form (with the uppercase C letter Cross:) in the name
In [5]: Cross(C.i, C.j)
Out[5]: (C_i)×(C_j)
To perform the calculation, simply use .doit()
:
In [6]: Cross(C.i, C.j).doit()
Out[6]: C_k