sympy矩阵真的那么慢吗? [英] Are sympy matrices really that slow?

查看:429
本文介绍了sympy矩阵真的那么慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看过sympy,它看起来非常慢. 我在做错什么吗?

I just had a look on sympy and it looks like it's extremely slow. Am I doing something wrong?

from sympy.matrices import zeros
from time import time
import numpy as np

t = time()
M = zeros(500,500)
print("Time", time()-t)

t = time()
M = np.zeros((500,500))
print("Time", time()-t)

Sympy需要1.2秒,而numpy需要0.0006秒. 这里真的没有发生任何事情.需要这么长时间?

Sympy takes 1.2s and numpy takes 0.0006s. There is nothing really going on here. What takes so long?

我真正想要的是一个可以在分数值上使用快速矩阵填充(高斯消除,乘法)而又不损失精度的库.我现在将numpy与python中的dtype=objectFraction一起使用.它不是超级快,但是比这更好;)

What I was really looking for was a library where I can use fast matrix stuff (Gauss eliminiation, multiplication) on fractional values without losing precision. I am now using numpy with dtype=object and Fraction from python. It's not super fast but better than this ;)

推荐答案

Sympy ,顾名思义,它是用于符号数学的软件包,即(强调我的) :

Sympy, as its name shows is a package for symbolic mathematics, that is (emphasis mine):

[…]科学领域,涉及研究和开发用于操纵数学表达式和其他数学对象的算法和软件.

[…] a scientific area that refers to the study and development of algorithms and software for manipulating mathematical expressions and other mathematical objects.

维基百科

因此, Sympy 并未针对矩阵计算进行优化.

So, Sympy is not optimized for matrix calculation.

正如@Michelle在评论中指出的那样, SymPy 矩阵是符号性的.每个条目(甚至为零)都是一个 SymPy 对象,准确地说,是sympy.core.numbers.Zero.

As @Michelle pointed out in the comments SymPy matrices are symbolic; each entry (even zero) is a SymPy object, sympy.core.numbers.Zero to be precise.

此外,如果您看一下源代码, Sympy 是一个纯Python包,而 Numpy 的代码库中有50%以上是用C编写的.这可能解释了您在 Sympy 中注意到的性能问题.

Also, if you look at the source code, Sympy is a pure Python package, whereas Numpy has over 50% of its codebase written in C. That probably explains the performance issue you noticed with Sympy.

这篇关于sympy矩阵真的那么慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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