[Python] Sympy vector module: Change of vector field coordinate system with CoordSys3D

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    I want a python script to express a given 2D/3D (cartesian) vector field in polar/spherical coordinates, including transformed basis vectors. That is, transform the vector field v = vx i + vy j (+ vz k) according to these formulas:

    • x = r cos(theta)
    • y = r sin(theta)
    • i = cos(theta) i_r - sin(theta) i_theta
    • j = sin(theta) i_r + cos(theta) i_theta

    As far as I can tell from documentation, Sympy's vector module has functionality to do this with CoordSys3D, but I cannot make it work.

    As a workaround, I have tried the following in the 2D:

    C= vec.CoordSys3D('C')
    P= vec.CoordSys3D('P')

    def polar(v):
    substitutions = [(x, r*sp.cos(theta)) ,(y, r * sp.sin(theta)), (C.i, sp.cos(theta)*P.i - sp.sin(theta)*P.j), (C.j, sp.sin(theta)*P.i + sp.cos(theta)*P.j)]
    v_pol = sp.simplify(v.subs(substitutions))
    return v_pol


    This doesn't work, and I'm not sure I understand why. Feel like it ought to be a fairly simple problem to solve.

    Any tips appreciated.

    Continue reading...

Compartilhe esta Página