我想在 numpy mggrid 上评估 sympy.lambdify 的输出。我尝试了以下方法:
import sympy as sp
import numpy as np
theta, v = sp.symbols("theta v")
coeff = (-sp.sin(theta/2)*sp.sin(2*v) + sp.sin(v)*sp.cos(theta/2) + 3)
kb = sp.Matrix([[coeff*sp.cos(theta),
coeff*sp.sin(theta),
sp.sin(theta/2)*sp.sin(v) + sp.sin(2*v)*sp.cos(theta/2)]])
f = sp.lambdify((theta, v), kb, modules='numpy')
f(*np.mgrid[0:2*np.pi:101j, 0:2*np.pi:101j])
但我收到一条错误消息,指出矩阵必须是二维的。
我找到了解决方案。
import sympy as sp
import numpy as np
theta, v = sp.symbols("theta v")
coeff = (-sp.sin(theta/2)*sp.sin(2*v) + sp.sin(v)*sp.cos(theta/2) + 3)
kb = sp.Matrix([[coeff*sp.cos(theta),
coeff*sp.sin(theta),
sp.sin(theta/2)*sp.sin(v) +
sp.sin(2*v)*sp.cos(theta/2)]])
f = sp.lambdify((theta, v), kb, [{'ImmutableMatrix': np.array}, "numpy"])
x, y = np.mgrid[0:2*np.pi:101j, 0:2*np.pi:101j]
g = f(x, y)
x, y, z = g[0]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)