[英]Get adjoint matrix in R
矩陣的第(i,j)個次要是刪除了第i行和第j列的矩陣。
minor <- function(A, i, j)
{
A[-i, -j]
}
第(i,j)個輔助因子是功率i + j的第(i,j)個次要時間-1。
cofactor <- function(A, i, j)
{
-1 ^ (i + j) * minor(A, i, j)
}
通過這種方式,我得到了A的輔助因子,那我怎么能得到伴隨矩陣?
你需要大約-1
括號和次要定義的決定因素。
之后,您可以使用循環或outer
# Sample data
n <- 5
A <- matrix(rnorm(n*n), n, n)
# Minor and cofactor
minor <- function(A, i, j) det( A[-i,-j] )
cofactor <- function(A, i, j) (-1)^(i+j) * minor(A,i,j)
# With a loop
adjoint1 <- function(A) {
n <- nrow(A)
B <- matrix(NA, n, n)
for( i in 1:n )
for( j in 1:n )
B[j,i] <- cofactor(A, i, j)
B
}
# With `outer`
adjoint2 <- function(A) {
n <- nrow(A)
t(outer(1:n, 1:n, Vectorize(
function(i,j) cofactor(A,i,j)
)))
}
# Check the result: these should be equal
det(A) * diag(nrow(A))
A %*% adjoint1(A)
A %*% adjoint2(A)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.