R/ef_auxiliar.R

Defines functions ef_multiply2 ef_multiply ef_sandwich_cholesky ef_sandwich_power ef_sandwich_negative ef_sandwich

Documented in ef_multiply ef_multiply2 ef_sandwich ef_sandwich_cholesky ef_sandwich_negative ef_sandwich_power

#' @title Matrix product in sandwich form
#' @name ef_sandwich
#' @author Wagner Hugo Bonat
#'
#' @description The function \code{ef_sandwich} is just an auxiliar
#'     function to compute product matrix in the sandwich form
#'     \code{bord1 * middle * bord2}. An special case appears when
#'     computing the derivative of the covariance matrix with respect to
#'     the power parameter. Always the bord1 and bord2 should be
#'     diagonal matrix. If it is not true, this product is too slow.
#'
#' @param middle A matrix.
#' @param bord1 A matrix.
#' @param bord2 A matrix.
#' @keywords internal
#' @return The matrix product \code{bord1 * middle * bord2}.

## Auxiliar function to multiply matrices ------------------------------
ef_sandwich <- function(middle, bord1, bord2) {
    bord1 %*% middle %*% bord2
}

#' @rdname ef_sandwich
ef_sandwich_negative <- function(middle, bord1, bord2) {
    -bord1 %*% middle %*% bord2
}

#' @rdname ef_sandwich
ef_sandwich_power <- function(middle, bord1, bord2) {
    temp1 <- ef_sandwich(middle = middle, bord1 = bord1, bord2 = bord2)
    return(temp1 + t(temp1))
}

#' @rdname ef_sandwich
ef_sandwich_cholesky <- function(bord1, middle, bord2) {
    p1 <- bord1 %*% middle %*% bord2
    return(p1 + Matrix::t(p1))
}

#' @rdname ef_sandwich
ef_multiply <- function(bord1, bord2) {
    return(bord2 %*% bord1)
}

#' @rdname ef_sandwich
ef_multiply2 <- function(bord1, bord2) {
    return(bord1 %*% bord2)
}
wbonat/mglm4twin documentation built on Oct. 14, 2023, 9:37 p.m.