Re: R-beta: solve

Simon Fear (
Fri, 24 Oct 1997 11:47:20 +0100

Date: Fri, 24 Oct 1997 11:47:20 +0100
From: (Simon Fear)
Message-Id: <>
Subject: Re: R-beta: solve

Content-MD5: L+YpNPzgEF6j9tN+h6Lb2g==
Precedence: bulk

Silly me thinking solve code wasn't accessible in R (cos it isn't in S-PLUS). All you 
need is a tiny edit:

> mysolve
function (a, b, tol = 1e-07) 
        if (!is.qr(a)) 
                a <- qr(a, tol = tol)
        nc <- ncol(a$qr)
        if (a$rank != nc) {
                warning("singular matrix \'a\' in solve")
                return(rep(NA, nc))
        if (missing(b)) {
                if (nc != nrow(a$qr)) {
                        warning("only square matrices can be inverted")
                        return(rep(NA, nc))
                b <- diag(1, nc)
        b <- as.matrix(b)
        return(qr.coef(a, b))

Well, not quite all; if b is the wrong length, you'll still get a fatal error 
from qr.coef, but that's just as easily edited.

Advise Jim (and anybody else) to edit nlm into mynlm with a call to mysolve (and 
a subsequent test that ![1]); you might get very strange 
compatability/porting problems if you overwrite the default solve.

Simon Fear
r-help mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: