How do I work with vectors in cylindrical and spherical systems with SymPy?

Viewed 213

With SymPy, I can perform basic mathematical operations with vectors in cartesian system. For example:

from sympy.vector import CoordSys3D

N = CoordSys3D('N')

v1 = 3*N.i - 2*N.j - 4*N.k # Vector 1
v2 = 2*N.i - 4*N.j + 2*N.k # Vector 2

print(v1 + v2) # Sum
print(v1 - v2) # Difference
print(v1 & v2) # Dot product
print(v1 ^ v2) # Cross product
print(v1.magnitude()) # Magnitude
print(v1.normalize()) # Normalize

But when I try to do the same in another coordinate system. For example, lets calculate the magnitude of a vector in cylindrical system:

from sympy import pi
from sympy.vector import CoordSys3D

N = CoordSys3D('N', transformation='cylindrical', variable_names=("rho", "phi", "z"))

v1 = 4*N.i + (pi/2)*N.j + 3*N.k

print(v1.magnitude())

The output is sqrt(pi**2/4 + 25) but the spected is 5.

What is the correct way to perform all these operations with vectors in different coordinate systems?

0 Answers
Related