用于查找nxn矩阵行列式的R代码不适用于n> = 4

时间:2018-06-19 18:12:28

标签: r algorithm

nxn矩阵的行列式

dmat<- function(x=matrix()){
n<- nrow(x)
s<- 0
   der<- function(x=matrix()){
     a<- x[1,1]*x[2,2]
     b<- x[1,2]*x[2,1]
     d<- a-b
     return(d)
   }

   if(n>2){
     for(i in 1:(n-2)){
       for(j in 1:n){
         if(j%%2==0){p<- -1
         }else{p<- 1}
         s<- s + p*x[i,j]*dmat(x[-i,-j])
       }
     }
   }else{
     s<- der(x)
   }
   return(s)
 }

示例:

  

x <-matrix(c(2,3,5,8,9,7,4,7,8),3,3)

     

dmat(x)

     

38

     

y <-matrix(c(1,5,8,4,9,6,7,5,4,8,9,4,4,2,6,8),4,4)

     

dmat(y)

     

0

dmat(y)的答案是354 *尽管我知道有一个det()函数可以使用。

0 个答案:

没有答案