Skip to content

Instantly share code, notes, and snippets.

@OldLipe
Created April 2, 2019 15:53
Show Gist options
  • Save OldLipe/85eefa9c7805220557e371fb737f57ae to your computer and use it in GitHub Desktop.
Save OldLipe/85eefa9c7805220557e371fb737f57ae to your computer and use it in GitHub Desktop.
#'@author Felipe Carvalho and Willian Vieira
#'
#'@see https://www.geeksforgeeks.org/determinant-of-a-matrix/
matriz_exercicio <- matrix(c(-1, 2, 3,-4,
4, 2, 0, 0,-1, 2,-3, 0,
2, 5, 3, 1),
nrow = 4,
ncol = 4) %>% t()
#' @method menor_completar
#'
#' @description Obter o menor complementar
#'
#' @param matriz
#'
#' @param linha_fixa
#'
#' @param coluna_var
#'
#' @param ordem
#'
#' @return submatriz
menor_complementar <- function(matriz,
linha_fixa,
coluna_var,
ordem) {
if (coluna_var == 1) {
return(matriz[(linha_fixa + 1):ordem,
(coluna_var + 1):ordem])
} else if (coluna_var > 1 & coluna_var < ordem) {
return(matriz[(linha_fixa + 1):ordem,
1:ordem != coluna_var])
} else{
return(matriz[(linha_fixa + 1):ordem,
1:(coluna_var - 1)])
}
}
#' @method calc_laplace
#'
#' @description Obter o determinante
#'
#' @param matriz
#'
#' @param ordem
#'
#' @return Determinante
calc_laplace <- function(matriz, ordem) {
cofator <- 0
if (ordem == 1 || is.null(ordem)) {
return(matriz)
}
for (j in 1:ordem) {
submatriz <-
menor_complementar(matriz, 1, j, ordem)
sinal <- (-1) ^ (1 + j)
cofator <- cofator +
sinal * matriz[1, j] * calc_laplace(submatriz, ordem - 1)
}
return(cofator)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment