Solving Ax=B by SVD Posted on March 26, 2013 by sukhbinder 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 = np.dot(u.T,b) w = np.linalg.solve(np.diag(s),c) x = np.dot(v.T,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! Share this:ShareTwitterLinkedInFacebookGoogleEmailPrintLike this:Like Loading... Related

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 .. Reply

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 Reply

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 ..

Thanks. I do not get that error, will check. Thanks for reading.

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