[R] Re: solve vs. qr.solve

Douglas Bates bates at stat.wisc.edu
Mon May 1 19:14:27 CEST 2000


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?


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