计算NxN递归C#的阵列行列式 [英] Computing Array Determinant for NxN Recursive C#

查看:182
本文介绍了计算NxN递归C#的阵列行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,这让我很头疼。我正在构建一个矩阵行列式函数来计算NxN行列式,我正在使用递归。逻辑正常,但我无法正确计算最终值。



这是我的Matrix Determinant代码:



  public   static   double  determinant( double  [,] array){
double det = 0 ;
double total = 0 ;
double [,] tempArr = new double [array.GetLength( 0 ) - 1 ,array.GetLength( 1 ) - 1 ];

if (array.GetLength( 0 )== 2
{
det = array [ 0 0 ] *数组[ 1 1 ] - 数组[ 0 1 ] * array [ 1 0 ];
}

else {

for int i = 0 ; i< 1; i ++)
{
for int j = 0 ; j < array.GetLength( 1 ); j ++)
{
if (j% 2 != 0 )array [i,j] = array [i,j] * -1;
tempArr = fillNewArr(array,i,j);
det + = determinant(tempArr);
total = total +(det * array [i,j]);
}
}
}
return det;
}



和关于fillNewArr方法它只是一种修剪数组的方法,方法如下:

  public   static   double  [,] fillNewArr(  double  [,] originalArr, int 行, int  col)
{
double [,] tempArray = new double [originalArr.GetLength( 0 ) - 1 ,originalArr.GetLength( 1 ) - 1 ];

for int i = 0 ,newRow = 0 ; i < originalArr.GetLength( 0 ); i ++)
{
if (i == row)
< span class =code-keyword>继续;
for int j = 0 ,newCol = 0 ; j < originalArr.GetLength( 1 ); j ++)
{
if (j == col)继续;
tempArray [newRow,newCol] = originalArr [i,j];

newCol ++;
}
newRow ++;
}
return tempArray;

}



该方法正常工作,因为它应该我假设,但最终结果不是以正确的方式计算,为什么会那是?!



4x4数组示例:



 {< span class =code-digit> 2   6   6   2 } 
{ 2 7 3 6 }
{ 1 5 0 1 }
{ 3 7 0 7 }



最终结果应该是-168,而我的是104!

解决方案

< blockquote>这是应该修改的部分:)

  for  int  j =  0 ; j <  array.GetLength( 1 ); j ++)
{
tempArr = fillNewArr(array, 0 ,j);
det = determinant(tempArr)*(Math.Pow(-1,j)* array [ 0 ,j]);
总计+ = det; ;
}


Well, this is giving me a real headache. I'm building a matrix determinant function to compute NxN determinant, and I'm using recursion. The logic is working right but I'm not able to get the final value computed correctly.

Here is my code for Matrix Determinant:

public static double determinant(double[,]array){
           double det=0;
           double total = 0;
           double[,] tempArr = new double[array.GetLength(0) - 1, array.GetLength(1) - 1];

           if(array.GetLength(0)==2)
           {
               det = array[0, 0] * array[1, 1] - array[0, 1] * array[1, 0];
           }

           else {

               for (int i = 0; i <1; i++)
               {
                   for (int j = 0; j < array.GetLength(1); j++)
                   {
                       if (j % 2 != 0) array[i, j] = array[i, j] * -1;
                      tempArr= fillNewArr(array, i, j);
                      det+=determinant(tempArr);
                      total =total + (det * array[i, j]);
                   }
               }
                }
           return det;
       }


and about fillNewArr method it's just a method to trim the array, method is as follow:

public static double[,] fillNewArr(double[,] originalArr, int row, int col)
        {
            double[,] tempArray = new double[originalArr.GetLength(0) - 1, originalArr.GetLength(1) - 1];

            for (int i = 0, newRow = 0; i < originalArr.GetLength(0); i++)
            {
                if (i == row)
                    continue;
                for (int j = 0, newCol=0; j < originalArr.GetLength(1); j++)
                {
                    if ( j == col) continue;
                    tempArray[newRow, newCol] = originalArr[i, j];

                    newCol++;
                }
                newRow++;
            }
            return tempArray;

        }


The method is working as it supposed to "I assume" but the final result is not computed in the right way, why would that be?!

4x4 Array Example:

{2 6 6 2}
{2 7 3 6}
{1 5 0 1}
{3 7 0 7}


Final result should be -168, while mine is 104!

解决方案

This is the part where should be modified :)

for (int j = 0; j < array.GetLength(1); j++)
               {
               tempArr= fillNewArr(array, 0, j);
               det=determinant(tempArr) *( Math.Pow(-1,j)* array[0,j]);
               total += det; ;
               }


这篇关于计算NxN递归C#的阵列行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆