5.0

5.0 | 7 ratings Rate this file 189 downloads (last 30 days) File Size: 108.79 KB File ID: #24119

Don't let that INV go past your eyes; to solve that system, FACTORIZE!

by Tim Davis

 

14 May 2009 (Updated 04 Jun 2009)

Code covered by BSD License  

A simple-to-use object-oriented method for solving linear systems and least-squares problems.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Download Now | Watch this File

File Information
Description

What's the best way to solve a linear system? Backslash is simple, but the factorization it computes internally can't be reused to solve multiple systems (x=A\b then y=A\c). You might be tempted to use inv(A), as S=inv(A) ; x=S*b ; y=S*c, but's that's slow and inaccurate. The best solution is to use a matrix factorization (LU, CHOL, or QR) followed by forward/backsolve, but figuring out the correct method to use is not easy.

In textbooks, linear algebraic expressions often use the inverse. For example, the Schur complement is written as S=A-B*inv(D)*C. It is never meant to be used that way. S=A-B*(D\C) or S=A-(B/D)*C are better, but the syntax doesn't match the formulas in your textbook.

The solution is to use the FACTORIZE object. It overloads the backslash and mtimes operators (among others) so that solving two linear systems A*x=b and A*y=c can be done with this:

F=factorize(A) ;
x=F\b ;
y=F\c ;

An INVERSE method is provided which does not actually compute the inverse itself (unless it is explicitly requested). The statements above can be done equivalently with this:

S=inverse(A) ;
x=S*b ;
y=S*c ;

That looks like a use of INV(A), but what happens is that S is a factorized representation of the inverse of A, and multiplying S times another matrix is implemented with a forward/backsolve.

Wiith the INVERSE function, your Schur complement computation becomes S = A - B*inverse(D)*C which looks just like the equation in your book ... except that it computes a factorization and then does a forward/backsolve, WITHOUT computing the inverse at all.

An extensive demo is included, as well as a simple and easy-to-read version (FACTORIZE1) with fewer features, as an example of how to create objects in MATLAB.

If the system is over-determined, QR factorization is used to solve the least-squares problem. If the system is under-determined, the transpose of A is factorized via QR, and the minimum 2-norm solution is found. In MATLAB, x=A\b finds a "basic" solution to an under-determined system.

You can compute a subset of the entries of the inverse just by referencing them. For example, S=inverse(A), S(n,n) computes the (n,n) entry of the inverse, without computing all of them.

In the RARE case that you need the entire inverse (or pseduo-inverse of a full-rank rectangular matrix) you can force the object to become "double", with S = double (inverse (A)). This works for both full and sparse matrices (pinv(A) only works for full matrices).

And remember ... don't ever multiply a matrix by inv(A).

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
LINFACTOR: uses LU or CHOL to factorize a matrix, or previously computed factors to solve Ax=b

MATLAB release MATLAB 7.8 (R2009a)
Other requirements MATLAB 7.6 (R2008a) or later is required.
Zip File Content  
Published M Files THE FACTORIZE OBJECT for solving linear systems
Other Files
Factorize/Contents.m,
Factorize/factorization_dense_chol.m,
Factorize/factorization_dense_lu.m,
Factorize/factorization_dense_qr.m,
Factorize/factorization_dense_qrt.m,
Factorize/factorization_generic.m,
Factorize/factorization_sparse_chol.m,
Factorize/factorization_sparse_lu.m,
Factorize/factorization_sparse_qr.m,
Factorize/factorization_sparse_qrt.m,
Factorize/factorize.m,
Factorize/factorize1.m,
Factorize/factorize_demo.m,
Factorize/fdemo.m,
Factorize/html/factorize_demo.pdf,
Factorize/html/factorize_demo.tex,
Factorize/inverse.m,
Factorize/Test/test_accuracy.m,
Factorize/Test/test_all.m,
Factorize/Test/test_disp.m,
Factorize/Test/test_errors.m,
Factorize/Test/test_factorize.m,
Factorize/Test/test_performance.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
15 May 2009 us

a truly professional and INValuable package written with a twinkle in the (author's) eye and a must look-at for people dealing with linear systems...
one pedestrian thought: why not create a stand-alone object given ML's new and powerful OOP-strength...
us

15 May 2009 John D'Errico

Absolutely splendid! Many thanks to Tim for writing this. A great title too.

18 May 2009 Sebastien Paris

Great !!!

27 May 2009 Petter

This is an excellent piece of software. Makes great use of Matlab's OOP.

05 Jun 2009 Petter

Nice with an update to modern MATLAB OOP.

19 Jun 2009 Miao LI  
05 Aug 2009 Ben

All in all this package is a great idea and it looks like a first class implementation. As such, I'd like to help improve it by pointing out an issue. I am trying to solve Ax=-b, so I type in x=-inverse(A)*b, but I get the following error:

??? Undefined function or method 'uminus' for input arguments of type 'factorization_dense_lu'.

The work around is simple: x=inverse(A)*(-b). However, these are mathematically equivalent, so both should work.

27 Sep 2009 Bruno Luong

What mathworks are waiting to incorporate this package into Matlab?

Please login to add a comment or rating.
Updates
18 May 2009

New overload for plus and minus (cholupdate). Improved performance. More extensive demo.

19 May 2009

Bug fix so that inverse(A)\b does the same thing as A*b (a peculiar case, of course...)

20 May 2009

Added "(SetAccess = protected)" to class properties.

27 May 2009

Added link to LINFACTOR file.

04 Jun 2009

Substantial update. The factorization class is now a superclass, with 8 specific subclasses beneath it (one for each kind of matrix factorization). Minor changes to the demo. No change to the user interface.

Tag Activity for this File
Tag Applied By Date/Time
factorization Tim Davis 15 May 2009 10:11:53
backslash Tim Davis 15 May 2009 10:11:54
mldivide Tim Davis 15 May 2009 10:11:54
mrdivide Tim Davis 15 May 2009 10:11:54
pinv Tim Davis 15 May 2009 10:11:54
inv Tim Davis 15 May 2009 10:11:54
inverse Tim Davis 15 May 2009 12:13:03
linear algebra Tim Davis 25 May 2009 20:15:21
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com