Hi All,
Can someone tell me if solve function shown below for my version of R is
proper or not?
I am using R 2.7.2 .Wherever i have used this function ,i got results which
were different from the expected results as computed using SPSS.
Description of this function says:--
Solve a System of EquationsDescription
This generic function solves the equation a %*% x = b for x, where b can be
either a vector or a matrix.
Usage
solve(a, b, ...)
## Default S3 method:
solve(a, b, tol, LINPACK = FALSE, ...)
Arguments a a square numeric or complex matrix containing the coefficients
of the linear system. b a numeric or complex vector or matrix giving the
right-hand side(s) of the linear system. If missing, b is taken to be an
identity matrix and solve will return the inverse of a. tol the tolerance
for detecting linear dependencies in the columns of a. If LINPACK is
TRUEthe default is
1e-7, otherwise it is .Machine$double.eps. Future versions of R may use a
tighter tolerance. Not presently used with complex matrices a.
*LINPACK* *logical.
Should LINPACK be used (for compatibility with R < 1.7.0)? Otherwise LAPACK
is used.* *...* *further arguments passed to or from other methods*
*This is the function for my version of R,don't understand if the
discrepancy in results are due to this function.I don't see any mention of
LAPACK in this function :----
*
solve.default<-function(a, b, tol = ifelse(LINPACK, 1e-07,
.Machine$double.eps),LINPACK = FALSE, ...)
{
print("inside solve.default")
if (is.complex(a) || (!missing(b) && is.complex(b))) {
a <- as.matrix(a)
if (missing(b)) {
if (nrow(a) != ncol(a))
stop("only square matrices can be inverted")
b <- diag(1 + (0+0i), nrow(a))
colnames(b) <- rownames(a)
}
else if (!is.complex(b))
b[] <- as.complex(b)
if (!is.complex(a))
a[] <- as.complex(a)
return(if (is.matrix(b)) {
if (ncol(a) != nrow(b)) stop("'b' must be compatible
with 'a'")
rownames(b) <- colnames(a)
print("inside Call La_zgesv a, b, PACKAGE base")
retur(NA)
.Call("La_zgesv", a, b, PACKAGE = "base")
#####################################
} else drop(.Call("La_zgesv", a, as.matrix(b), PACKAGE =
"base")))
}
if (is.qr(a)) {
warning("solve.default called with a \"qr\" object: use
'qr.solve'")
return(solve.qr(a, b, tol))
}
if (!LINPACK) {
a <- as.matrix(a)
if (missing(b)) {
if (nrow(a) != ncol(a))
stop("only square matrices can be inverted")
b <- diag(1, nrow(a))
colnames(b) <- rownames(a)
}
else storage.mode(b) <- "double"
storage.mode(a) <- "double"
return(if (is.matrix(b)) {
if (ncol(a) != nrow(b)) stop("'b' must be compatible
with 'a'")
rownames(b) <- colnames(a)
.Call("La_dgesv", a, b, tol, PACKAGE = "base")
} else drop(.Call("La_dgesv", a, as.matrix(b), tol, PACKAGE
"base")))
}
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)
colnames(b) <- rownames(a$qr)
}
qr.coef(a, b)
}
--
Thanks
Moumita
[[alternative HTML version deleted]]