R/isposdef.R

Defines functions isposdef

Documented in isposdef

isposdef <- function(A, psd = FALSE, tol = 1e-10) {
    if (nrow(A) != ncol(A)) {
        warning("Matrix 'A' is not quadratic.\n", .call = FALSE)
        a <- FALSE
    } else if (any(abs(A - t(A)) > tol)) {
        warning("Matrix 'A' is not symmetric.\n", .call = FALSE)
        a <- FALSE
    } else {
        e <- try(chol(A, pivot = psd), silent = TRUE)
        if(inherits(e, "try-error")) {
            a <- FALSE
        } else {
            a <- TRUE
        }
    }
    return(a)
}

Try the pracma package in your browser

Any scripts or data that you put into this service are public.

pracma documentation built on March 19, 2024, 3:05 a.m.