Backslash inversion vs inv-function
Quentin Spencer
qspencer at ieee.org
Fri Jan 18 13:30:06 CST 2008
Rolf Fabian wrote:
> Because of its underlying algorithm, I expected that
> inversion of a (nonsingular) square matrix should
> be faster using backslash operator 'y2 = x\eye(n)'
> instead of Octave's builtin 'y1 = inv(x)' function.
>
> Thus I conducted dimension n - depending tests
> using random matrices input in order to sched
> some light on this issue.
>
> octave-3.0.0.exe:48> n=5; x=randn(n); tic; y1 = inv(x); toc,
> tic; y2 = x\eye(n); toc,
> max(max(abs(y1-y2)))
>
> Elapsed time is sec.
> n inv(x) x\eye(n) Speed Gain Fac max(max(abs(result
> difference)))
> 5 0.05531 0.000258 214 1.1102e-016
> 10 0.06063 0.000313 194 4.4409e-016
> 25 0.05705 0.000705 81 6.9944e-015
> 50 0.05889 0.002197 27 1.2212e-015
> 100 0.07195 0.01153 6.2 1.8952e-015
> 150 0.08721 0.03425 2.6 2.2649e-014
> 200 0.1271 0.06117 2.1 3.9413e-015
> 300 0.2892 0.1991 1.5 8.5820e-014
> 400 0.6255 0.609 1.03 8.9484e-014
> 500 1.176 1.263 0.93 2.2163e-014
> 600 2.05 2.197 0.93 7.0499e-015
> 700 3.228 3.458 0.93 2.4092e-014
> 800 4.855 5.137 0.95 2.2243e-014
> 900 6.873 7.272 0.95 3.0781e-014
> 1000 9.506 9.988 0.95 2.8283e-014
>
> I've done this on a relative old laptop
> (Windows XP, SP2, Intel III Mobile CPU 933 MHz 512 MB RAM)
> so do not wonder about absolute performance times.
>
> >From the the table above it can be seen that the results confirm my
> expectations.
> Up to about 400x400 sized matrices the speed gain (column 4) can be
> enormous.
> Consequently replacing 'inv(x)' in existing code by 'x\eye(n)' might be an
> option and should be considered.
>
> Unfortunately the maximal absolute difference between the two results
> also increases with dimension n. Does anybody know which algorithm
> is considered to be the more precise ?
>
Generally, the "\" algorithm is considered more numerically stable. In
fact, I was always told in school that you should avoid inverting a
matrix directly for this reason. In my experience with signal processing
applications, I don't think I've ever run into an application of a
matrix inverse that couldn't be avoided using a linear solver by posing
the problem the right way. However, it could be justifiable in some
situations if an inverse matrix is needed repeatedly for multiple
subsequent calculations.
Quentin
More information about the Octave-maintainers
mailing list