如何在scipy / numpy中加速expm函数等矩阵函数?

时间:2022-06-21 00:54:11

I'm using scipy and numpy to calculate exponentiation of a 6*6 matrix for many times.

我正在使用scipy和numpy多次计算6 * 6矩阵的取幂。

Compared to Matlab, it's about 10 times slower.

与Matlab相比,它慢了大约10倍。

The function I'm using is scipy.linalg.expm, I have also tried deprecated methods scipy.linalg.expm2 and scipy.linalg.expm3, and those are only two times faster than expm. My question is:

我正在使用的函数是scipy.linalg.expm,我也尝试了不推荐使用的方法scipy.linalg.expm2和scipy.linalg.expm3,这些只比expm快两倍。我的问题是:

  1. What's wrong with expm2 and expm3 as they are faster than expm?

    expm2和expm3有什么问题,因为它们比expm更快?

  2. I'm using wheel package from http://www.lfd.uci.edu/~gohlke/pythonlibs/, and I found https://software.intel.com/en-us/articles/building-numpyscipy-with-intel-mkl-and-intel-fortran-on-windows. Is the wheel package compiled with MKL. If not, I think I can optimize and numpy, scipy by compile it by myself with MKL?

    我正在使用来自http://www.lfd.uci.edu/~gohlke/pythonlibs/的轮式包,我找到了https://software.intel.com/en-us/articles/building-numpyscipy-with-英特尔MKL和英特尔Fortran的上窗口。轮盘包是用MKL编译的。如果没有,我想我可以通过自己用MKL编译来优化和调整,scipy?

  3. Any other ways to optimize the performance?

    还有其他任何优化性能的方法吗?

2 个解决方案

#1


Well I think I have found answer for question 1 and 2 by myself 1. It seems expm2 and expm3 returns array rather than matrix. But they are about 2 times faster than expm

好吧,我想我已经找到了问题1和2的答案1.看起来expm2和expm3返回数组而不是矩阵。但它们比expm快2倍

  1. Well, after a whole day trying to compile scipy by MKL, I succeed. It's really hard to build the scipy, especially when I'm using windows, x64 and python3. It turned out to be a waste of time. It's not even a bit faster than the whl package from http://www.lfd.uci.edu/~gohlke/pythonlibs/ .
  2. 好吧,经过一整天试图用MKL编译scipy,我成功了。构建scipy真的很难,特别是当我使用windows,x64和python3时。事实证明这是浪费时间。它甚至不比http://www.lfd.uci.edu/~gohlke/pythonlibs/中的whl包快一点。

Hoping someone give answer to question 3.

希望有人回答问题3。

#2


Your matrix is relatively small, so maybe the numerical part is not the bottleneck. You should use a profiler to make sure that the limitation is in the exponentiation. You can also take a look at the source code of these implementations and write an equivalent function with less conditionals and checking.

你的矩阵相对较小,因此数字部分可能不是瓶颈。您应该使用分析器来确保限制在取幂中。您还可以查看这些实现的源代码,并编写具有较少条件和检查的等效函数。

#1


Well I think I have found answer for question 1 and 2 by myself 1. It seems expm2 and expm3 returns array rather than matrix. But they are about 2 times faster than expm

好吧,我想我已经找到了问题1和2的答案1.看起来expm2和expm3返回数组而不是矩阵。但它们比expm快2倍

  1. Well, after a whole day trying to compile scipy by MKL, I succeed. It's really hard to build the scipy, especially when I'm using windows, x64 and python3. It turned out to be a waste of time. It's not even a bit faster than the whl package from http://www.lfd.uci.edu/~gohlke/pythonlibs/ .
  2. 好吧,经过一整天试图用MKL编译scipy,我成功了。构建scipy真的很难,特别是当我使用windows,x64和python3时。事实证明这是浪费时间。它甚至不比http://www.lfd.uci.edu/~gohlke/pythonlibs/中的whl包快一点。

Hoping someone give answer to question 3.

希望有人回答问题3。

#2


Your matrix is relatively small, so maybe the numerical part is not the bottleneck. You should use a profiler to make sure that the limitation is in the exponentiation. You can also take a look at the source code of these implementations and write an equivalent function with less conditionals and checking.

你的矩阵相对较小,因此数字部分可能不是瓶颈。您应该使用分析器来确保限制在取幂中。您还可以查看这些实现的源代码,并编写具有较少条件和检查的等效函数。