我看到了这个[答案][1] https://stackoverflow.com/questions/46993819/how-to-create-a-vector-function-in-sympy 它使用
Matrix()
作为解决方法。
我需要将输入和输出向量从一个坐标系转换到另一个坐标系(并返回)。在这种情况下向量函数是如何完成的?我的功能很简单:
def B_el(r_vec):
r1 = r_vec.i
r2 = r_vec.j
u = sin(r1)+cos(r2)
v = -cos(r1)+sin(r2)
return Matrix([u, v, 0])
我建议您使用“向量”中描述的坐标系(下文中的 csys) https://docs.sympy.org/latest/modules/vector/coordsys.html
这是迄今为止我找到的最好的解决方案。
from sympy import symbols
from sympy.vector import CoordSys3D, express
grd = CoordSys3D('grd')
# this declares the existence of a csys, named grd
alpha, X, Y, Z = symbols('alpha, X, Y, Z')
slo = grd.orient_new_axis('slo', alpha, grd.k, location=2*grd.j)
# This creates a new csys named slo, rotated with an angle alpha around
# the axis k of grd (=grd.k), and locates it at 2 on the axis j of grd
# (x y z are replaced by i j k)
V = X*slo.i + Y*slo.j + Z*slo.k
# this defines a vector V with coordinates (X, Y, Z), in the csys slo.
display(V)
display(express(V, slo))
display(express(V, grd))
# this will display the coordinates of the vector V, and then express its coordinates
# in the csys slo & grd.