[R] Re: solve vs. qr.solve

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon May 1 20:22:15 CEST 2000


On 1 May 2000, Douglas Bates wrote:

> gb <gb at stat.umu.se> writes:
> 
> > > solve
> > function(a, b, ...) UseMethod("solve")
> > > methods(solve)
> > [1] "solve.default" "solve.qr"    =20
> > 
> > I guess that I have to find the underlying  C  code to find out
> > what "solve.default" is, and when which method is used. I have
> > looked around in R-1.0.1/src/* with no great success. Can I
> > get a hint where to search?
> 
> solve.default is just another function in the base R library.
> 
> > solve.default
> function(a, b, tol = 1e-7)
> {
>     if( !is.qr(a) )
> 	a <- qr(a, tol = tol)
>     nc <- ncol(a$qr)
>     if( a$rank != nc )
> 	stop("singular matrix `a' in solve")
>     if( missing(b) ) {
> 	if( nc != nrow(a$qr) )
> 	    stop("only square matrices can be inverted")
> 	b <- diag(1,nc)
>     }
>     ## pre 0.63.3: b <- as.matrix(b)
>     return(qr.coef(a,b))
> }
> 
> As you can see, it checks if the first argument is a qr object and, if
> not, it takes a qr decomposition.
> 
> It appears that solve.default, solve.qr, and qr.solve are identical.
> I imagine the reason for identical functions under multiple names is 
> for S-PLUS compatibility although I'm not sure.  Does anyone else
> know?

qr.solve does not exist in S-PLUS.  The other two do, and are different
in that solve.qr will only only work if A is already a QR decomposition.

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



More information about the R-help mailing list