1

As the title says, is there a way to find out which matrix decomposition is applying the function solve() for a given sparse matrix in R? For example, in Matlab there is spparms('spumoni', 2); , which return some info about the algorithm used to solve the decomposition for the sparse matrix.

1 Answers1

3

Well, running R one could get to

> methods(solve)

which will produce

[1] solve.default solve.qr

If you type

> solve.default

you'll get back

function (a, b, tol = .Machine$double.eps, LINPACK = FALSE, ...)
{
    if (!missing(LINPACK))
        warning("the LINPACK argument has been defunct since R 3.1.0")
    if (is.complex(a) || (!missing(b) && is.complex(b))) {
        a <- as.matrix(a)
        if (missing(b)) {
            b <- diag(1 + (0+0i), nrow(a))
            colnames(b) <- rownames(a)
        }
        return(.Internal(La_solve_cmplx(a, b)))
    }
    if (inherits(a, "qr")) {
        warning("solve.default called with a \"qr\" object: use 'qr.solve'")
        return(solve.qr(a, b, tol))
    }
    a <- as.matrix(a)
    if (missing(b)) {
        b <- diag(1, nrow(a))
        colnames(b) <- rownames(a)
    }
    .Internal(La_solve(a, b, tol))
}

which means it is either La_solve or La_solve_cmplx. Looking at their implementations e.g. here one could find out that La_solve will call LAPACK routine DGESV, and La_solve_cmplx will call LAPACK routine ZGESV.

Easy, huh?

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Thanks for the answer, but it is not what I was looking for. I'm looking for a way to find what solver is using for a certain matrix. For example, by setting the signature as `signature(a = "dsCMatrix", b = "....") `, R will find if a certain matrix can be decomposed by Cholesky or not, and then apply Cholesky or PALU decomposition. I want to know if R is using one of those methods for a given matrix. `methods(solve)`, you suggested list all the possible solver for `solve`. – JackFrost67 Jun 08 '21 at 08:10
  • @JackFrost67 Well, it is either solve.qr (QR decomposition) or DGESV, there is nothing else. You could avoid dispatch and call directly solve.default and check if result is the same, and then it is certainly DGESV. If you want something else, you have to use https://cran.r-project.org/web/packages/Matrix/index.html or something similar – Severin Pappadeux Jun 08 '21 at 12:28
  • Forgot to mention, beyond LAPACK there is R package to use Eigen C++ from R, https://cran.r-project.org/web/packages/RcppEigen/index.html. Never used it miyself – Severin Pappadeux Jun 10 '21 at 20:37