我想實現代碼在這裏找到&算法: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