0

I am trying to solve a large linear system of equations in Python, Ax=b type problem. A is square, not symmetric, 250.000x250.000, with 2e7 nonzero elements.

Sparsity pattern(K)

I solved it first in Matlab 2022a, with A\b or mldivide function. It takes approx 25 secs in my laptop. I then try to solve the same system of equations in python, with spsolve(K,f), and it takes 110 secs on the same laptop. K and f are csc matrices from scipy, as imported from matlab.

from scipy.io import loadmat
from scipy.io import loadmat
from scipy.io import loadmat
from scipy.sparse.linalg import spsolve
import time
data = loadmat('solverData.mat')
K=data['K']
f=data['f']
t = time.time()
x = spsolve(K, f)
t1=time.time()-t

Could you help me get similar performance to Matlab (or better :-) ) within pyhton?

Thanks!

Gorka
  • 1
  • 1
  • Update: It might not be obvious from the pattern, but there is coupling between the four large blocks by a reduced number of equations. This matrix represents a structural finite element model formed by four bodies connected at some local points. – Gorka Jun 09 '22 at 13:27
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Jun 09 '22 at 13:44
  • MATLAB is a paid software. Python is a free software. You may be able to find a python script that is as fast as MATLAB, but believe it or not, there are reasons why Mathworks can still charge quite a lot of money for MATLAB and people still pay it. One of them being its one of the fastest code around. If MATLAB and python would be essentially equivalent, then no one would pay for MATLAB. – Ander Biguri Jun 09 '22 at 14:00
  • If the coupling is such that you can express the coupled version as a low-rank update to the uncoupled version, then you could try applying Sherman-Morrison-Woodbury: https://en.wikipedia.org/wiki/Woodbury_matrix_identity That might be slightly faster, and you could potentially factor the decoupled parts in parallel. – bg2b Jun 09 '22 at 23:06

0 Answers0