distributed-computing/tasks/klementeva-ja/lab_6/Lab6/Alg1.cs

47 lines
1.1 KiB
C#
Raw Permalink Normal View History

2023-12-16 11:45:24 +04:00
namespace Lab6
{
public class Alg1
{
public int Begin(int[,] matrix)
{
return CalculateDeterminant(matrix);
}
static int CalculateDeterminant(int[,] matrix)
{
int n = matrix.GetLength(0);
if (n == 1)
{
return matrix[0, 0];
}
int determinant = 0;
for (int j = 0; j < n; j++)
{
int[,] submatrix = new int[n - 1, n - 1];
for (int i = 1; i < n; i++)
{
for (int k = 0; k < n; k++)
{
if (k < j)
{
submatrix[i - 1, k] = matrix[i, k];
}
else if (k > j)
{
submatrix[i - 1, k - 1] = matrix[i, k];
}
}
}
determinant += (int)Math.Pow(-1, j) * matrix[0, j] * CalculateDeterminant(submatrix);
}
return determinant;
}
}
}