我想实现代码在这里找到&算法:deteminant N * N矩阵
deteminant of matrix 这里: How to calculate matrix determinant? n*n or just 5*5
但我还是坚持了下来。
我的第一个问题实际上是什么统治这个算法使用(因为有明显的数学由某人可以计算行列式的几个规则) - 所以我想,如果算法是正确应用上首先要检查。
我的第二个问题是我做错了(我指的是执行)或什么是错的算法本身,因为它看起来像3×3和正常工作的4x4,但5x5的它给人NaN的。用几个在线矩阵行列式计算器检查结果,除了5x5以外,结果都很好。
这是我的代码:
using System;
public class Matrix
{
private int row_matrix; //number of rows for matrix
private int column_matrix; //number of colums for matrix
private double[,] matrix; //holds values of matrix itself
//create r*c matrix and fill it with data passed to this constructor
public Matrix(double[,] double_array)
{
matrix = double_array;
row_matrix = matrix.GetLength(0);
column_matrix = matrix.GetLength(1);
Console.WriteLine("Contructor which sets matrix size {0}*{1} and fill it with initial data executed.", row_matrix, column_matrix);
}
//returns total number of rows
public int countRows()
{
return row_matrix;
}
//returns total number of columns
public int countColumns()
{
return column_matrix;
}
//returns value of an element for a given row and column of matrix
public double readElement(int row, int column)
{
return matrix[row, column];
}
//sets value of an element for a given row and column of matrix
public void setElement(double value, int row, int column)
{
matrix[row, column] = value;
}
public double deterMatrix()
{
double det = 0;
double value = 0;
int i, j, k;
i = row_matrix;
j = column_matrix;
int n = i;
if (i != j)
{
Console.WriteLine("determinant can be calculated only for sqaure matrix!");
return det;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
det = (this.readElement(j, i)/this.readElement(i, i));
//Console.WriteLine("readElement(j, i): " + this.readElement(j, i));
//Console.WriteLine("readElement(i, i): " + this.readElement(i, i));
//Console.WriteLine("det is" + det);
for (k = i; k < n; k++)
{
value = this.readElement(j, k) - det * this.readElement(i, k);
//Console.WriteLine("Set value is:" + value);
this.setElement(value, j, k);
}
}
}
det = 1;
for (i = 0; i < n; i++)
det = det * this.readElement(i, i);
return det;
}
}
internal class Program
{
private static void Main(string[] args)
{
Matrix mat03 = new Matrix(new[,]
{
{1.0, 2.0, -1.0},
{-2.0, -5.0, -1.0},
{1.0, -1.0, -2.0},
});
Matrix mat04 = new Matrix(new[,]
{
{1.0, 2.0, 1.0, 3.0},
{-2.0, -5.0, -2.0, 1.0},
{1.0, -1.0, -3.0, 2.0},
{4.0, -1.0, -3.0, 1.0},
});
Matrix mat05 = new Matrix(new[,]
{
{1.0, 2.0, 1.0, 2.0, 3.0},
{2.0, 1.0, 2.0, 2.0, 1.0},
{3.0, 1.0, 3.0, 1.0, 2.0},
{1.0, 2.0, 4.0, 3.0, 2.0},
{2.0, 2.0, 1.0, 2.0, 1.0},
});
double determinant = mat03.deterMatrix();
Console.WriteLine("determinant is: {0}", determinant);
determinant = mat04.deterMatrix();
Console.WriteLine("determinant is: {0}", determinant);
determinant = mat05.deterMatrix();
Console.WriteLine("determinant is: {0}", determinant);
}
}
南可能是由于“0.0/0.0”造成的。 – leppie 2013-03-20 17:16:32
我知道是因为这一点,或者甚至是因为n/0.0,但问题是**为什么算法甚至会出现这种情况**。它应该是它的工作算法,正如其他帖子中所建议的那样(我在我的问题的开头提到了它们,此外,它适用于3x3和4x4。 – 2013-03-20 17:25:27
算法似乎使用了高斯消元法,但假设除法如果这是你想要做的,我的建议是*正确地实现高斯消元*可能这个算法在给定的假设下是正确的;或许方程组允许解决方案吗?无论如何,我会 – 2013-03-20 17:40:33