# AW: [R] General Matrix Inverse

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 18 11:15:03 CEST 2001

```On Thu, 18 Oct 2001, Trenkler, Dietrich wrote:

> Thorsten is right. There is a direct formula for computing the Moore-Penrose
> inverse
> using the singular value composition of a matrix. This is incorporated in
> the following:
>
> mpinv <- function(A, eps = 1e-13) {
>     s <- svd(A)
>     e <- s\$d
>     e[e > eps] <- 1/e[e > eps]
>     return(s\$v %*% diag(e) %*% t(s\$u))
> }
>
> Hope it helps.

That's what ginv in library MASS already does, except that it uses a
relative tolerance (yours will fail for a matrix of the order of
10^(-20)), handles the complex case, the rank 1 case, .... In short, it
has been proven over a decade.  It's by the other author of MASS, BTW.

>
> > -----Ursprüngliche Nachricht-----
> > Von:	Torsten Hothorn
> > Gesendet am:	Thursday, October 18, 2001 9:52 AM
> > An:	Philippe Grosjean
> > Cc:	r-help at stat.math.ethz.ch
> > Betreff:	RE: [R] General Matrix Inverse
> >
> >
> >
> > > I use solve(x) to find the inverse of a matrix (don't know what a
> > "general
> > > inverse" is). By the way, what is better: solve(x), qr.solve(x) or
> > ginv(x)?
> > > ginv(x) seems to give results for matrices where solve and qr.solve
> > return
> > > an error:
> > >
> > > > x <- matrix(1:9, 3, 3)
> > > > x
> > >      [,1] [,2] [,3]
> > > [1,]    1    4    7
> > > [2,]    2    5    8
> > > [3,]    3    6    9
> > > > solve(x)
> > > Error in solve.default(x) : singular matrix `x' in solve
> > > > qr.solve(x)
> > > Error in qr.solve(x) : singular matrix `x' in solve
> > > > ginv(x)
> > >            [,1]          [,2]       [,3]
> > > [1,] -0.6388889 -5.555556e-02  0.5277778
> > > [2,] -0.1666667  4.163336e-17  0.1666667
> > > [3,]  0.3055556  5.555556e-02 -0.1944444
> > >
> >
> > if A is singular, A^-1 is not defined but a generalized inverse G is,
> > namely
> >
> > G is generalized inverse of A <=>
> >
> > A G A = A  (sometimes G is written as A^-)
> >
> > G is not unique, but adding 3 conditions
> >
> > - G A G = G
> >
> > - t(G A) = G A
> >
> > - t(A G) = A G
> >
> > makes G unique (Moore-Penrose-Inverse)
> >
> > Torsten
> >
> >
> > > Regards,
> > >
> > > Philippe Grosjean
> > >
> > >
> > > ...........]<(({?<...............<?}))><...............................
> > >  ) ) ) ) )	 __               	 __
> > > ( ( ( ( ( 	|__)              	|  _
> > >  ) ) ) ) )	|   hilippe       	|__)rosjean
> > > ( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
> > >  ) ) ) ) )	                  	 __
> > > ( ( ( ( ( 	|\  /|            	|__)
> > >  ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
> > > ( ( ( ( (
> > >  ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
> > > ( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
> > >  ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
> > > ( ( ( ( (
> > >  ) ) ) ) )      "I'm 100% confident that p is between 0 and 1"
> > > ( ( ( ( (                                  L. Gonick & W. Smith (1993)
> > >  ) ) ) ) )
> > > .......................................................................
> > >
> > >
> > > -----Message d'origine-----
> > > De : owner-r-help at stat.math.ethz.ch
> > > [mailto:owner-r-help at stat.math.ethz.ch]De la part de Prof Brian Ripley
> > > Envoye : jeudi 18 octobre 2001 04:25
> > > A : Randall Skelton
> > > Cc : r-help at stat.math.ethz.ch
> > > Objet : Re: [R] General Matrix Inverse
> > >
> > >
> > > On Wed, 17 Oct 2001, Randall Skelton wrote:
> > >
> > > > What is the easiest (not the fastest) way to find the general inverse
> > of a
> > > > matrix in R?
> > >
> > > If you mean the generalized inverse, ginv() in package MASS.  Otherwise,
> > > pleae tell us what a `general inverse' is.
> > >
> > >
> > > --
> > > Brian D. Ripley,                  ripley at stats.ox.ac.uk
> > > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> > > University of Oxford,             Tel:  +44 1865 272861 (self)
> > > 1 South Parks Road,                     +44 1865 272860 (secr)
> > > Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> > >
> > >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> > -.
> > > -.-
> > > r-help mailing list -- Read
> > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > > Send "info", "help", or "[un]subscribe"
> > > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > >
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> > _.
> > > _._
> > >
> > >
> > >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> > -.-.-
> > > r-help mailing list -- Read
> > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > > Send "info", "help", or "[un]subscribe"
> > > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > >
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> > _._._
> > >
> >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> > -.-.-
> > r-help mailing list -- Read
> > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> > _._._
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```