在numpy mesgrid上评估sympy lambdify的结果 [英] Evaluating the result of sympy lambdify on a numpy mesgrid

查看:111
本文介绍了在numpy mesgrid上评估sympy lambdify的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个numpy mgrid上评估sympy.lambdify的输出.我尝试了以下方法:

I would like to evaluate the output of sympy.lambdify on a numpy mgrid. I tried the following:

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])

但是我得到一个错误,说矩阵必须是二维的.

but I get an error saying the matrix must be 2-dimensional.

推荐答案

我找到了解决方案.

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]

这篇关于在numpy mesgrid上评估sympy lambdify的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆