Solving Ax=B by SVD

Solving Ax=B by inverting matrix A can be lot more computationally intensive than solving directly.

Python’s NumPy has linalg.solve(A, B), which returns the ‘x’ array

x = numpy.linalg.solve(A,B)

It uses a LU decomposition method for solving (not inversion).

SVD can also be used to solve without the need for inverting matrix A.

Here’s the function

def svdsolve(a,b):
    import numpy as np
    u,s,v = np.linalg.svd(a)
    c =,b)
    w = np.linalg.solve(np.diag(s),c)
    x =,w)
    return x

The following screencast shows the two methods of solving Ax=B in action.

You might like the post titled Beautiful Demo of How SVD works!

3 thoughts on “Solving Ax=B by SVD

  1. Thank you very much, you saved my day.

    BTW the line:
    w = np.linalg.solve(np.diag(s),c)

    Gives me an error related to the dimension of the matrices, so I change it by the following:
    w = np.divide(c[:len(s)],s)

    Which works without problems ..

  2. Cool!

    In any case, it seems w = np.linalg.solve(np.diag(s),c) is a bit too much if the matrix is diagonal; just w = np.divide(c , s) should be enough. Or, w = np.divide(c[:len(s)],s) if there are size issues, which sound kind of weird anyway

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s