Are sympy matrices really that slow?

拥有回忆 提交于 2019-12-10 17:25:07

问题


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 takes 1.2s and numpy takes 0.0006s. There is nothing really going on here. What takes so long?

Edit: 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 ;)


回答1:


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.

Wikipedia

So, Sympy is not optimized for matrix calculation.

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.

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.



来源:https://stackoverflow.com/questions/45796747/are-sympy-matrices-really-that-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!