Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterative solving of sparse systems of linear equations with (M, N) right-hand size matrix

I would like to solve a sparse linear equations system: A x = b, where A is a (M x M) array, b is an (M x N) array and x is and (M x N) array.

I solve this in three ways using the:

  • scipy.linalg.solve(A.toarray(), b.toarray()),
  • scipy.sparse.linalg.spsolve(A, b),
  • scipy.sparse.linalg.splu(A).solve(b.toarray()) # returns a dense array

I wish to solve the problem using the iterative scipy.sparse.linalg methods:

  • scipy.sparse.linalg.cg,
  • scipy.sparse.linalg.bicg,
  • ...

However, the metods suport only a right hand side b with a shape (M,) or (M, 1). Any ideas on how to expand these methods to (M x N) array b?

like image 294
blaz Avatar asked Nov 05 '15 07:11

blaz


People also ask

How do you solve a system of linear equations using iterations?

There are two types of methods for solving linear equations A*x = b : Direct methods are variants of Gaussian elimination. These methods use the individual matrix elements directly, through matrix operations such as LU, QR, or Cholesky factorization.

What is iterative matrix?

An iterative method is defined by. and for a given linear system with exact solution the error by. An iterative method is called linear if there exists a matrix such that. and this matrix is called the iteration matrix. An iterative method with a given iteration matrix is called convergent if the following holds.

Which method is an iterative method for solving Ax B?

Perhaps the simplest iterative method for solving Ax = b is Jacobi's Method.


1 Answers

A key difference between iterative solvers and direct solvers is that direct solvers can more efficiently solve for multiple right-hand values by using a factorization (usually either Cholesky or LU), while iterative solvers can't. This means that for direct solvers there is a computational advantage to solving for multiple columns simultaneously.

For iterative solvers, on the other hand, there's no computational gain to be had in simultaneously solving multiple columns, and this is probably why matrix solutions are not supported natively in the API of cg, bicg, etc.

Because of this, a direct solution like scipy.sparse.linalg.spsolve will probably be optimal for your case. If for some reason you still desire an iterative solution, I'd just create a simple convenience function like this:

from scipy.sparse.linalg import bicg

def bicg_solve(M, B):
    X, info = zip(*(bicg(M, b) for b in B.T))
    return np.transpose(X), info

Then you can create some data and call it as follows:

import numpy as np
from scipy.sparse import csc_matrix

# create some matrices
M = csc_matrix(np.random.rand(5, 5))
B = np.random.rand(5, 4)

X, info = bicg_solve(M, B)
print(X.shape)
# (5, 4)

Any iterative solver API which accepts a matrix on the right-hand-side will essentially just be a wrapper for something like this.

like image 118
jakevdp Avatar answered Sep 20 '22 00:09

jakevdp