什么是一个SymPy矩阵转换为numpy的阵列/矩阵的最佳方式 [英] What is the best way to convert a SymPy matrix to a numpy array/matrix

查看:1684
本文介绍了什么是一个SymPy矩阵转换为numpy的阵列/矩阵的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如果我一直使用 sympy 的方式,以 MutableDenseMatrix 转换为 numpy.array numpy.matrix 是一个很好的做法。

I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice.

我有一个象征性的矩阵,如:

I have a symbolic matrix like:

g = sympy.Matrix( [[   x,  2*x,  3*x,  4*x,  5*x,  6*x,  7*x,  8*x,   9*x,  10*x]
                   [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]] )

和我转换为 numpy.array 这样做的:

g_func = lambda val: numpy.array( g.subs( {x:val} ).tolist(), dtype=float )

在那里我得到的给定值的数组 X

有没有SymPy更好的内置解决方案,这样做吗?

Is there a better built-in solution in SymPy to do that?

感谢您!

推荐答案

我在这里回答从取和Krastanov的asmeurer建议。这个小片段使用 lambdify 从sympy:

I am answering here taking the advices from Krastanov and asmeurer. This little snippet uses lambdify from sympy:

from sympy import lambdify
g_func = lambdify( (x), g )

这似乎是达到什么样的问题是要求的最佳途径。

This seems to be the best way to achieve what the question is asking for.

这篇关于什么是一个SymPy矩阵转换为numpy的阵列/矩阵的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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