Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApplication41
- {
- class Program
- {
- static void Main()
- {
- double[,] matrix =
- {
- {1, 2, 3},
- {4, 10, 6},
- {7, 8, 9}
- };
- var det = DetRec(matrix);
- Console.WriteLine(det);
- }
- private static double DetRec(double[,] matrix)
- {
- if (matrix.Length == 4)
- {
- return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
- }
- double sign = 1, result = 0;
- for (int i = 0; i < matrix.GetLength(1); i++)
- {
- double[,] minor = GetMinor(matrix, i);
- result += sign * matrix[0, i] * DetRec(minor);
- sign = -sign;
- }
- return result;
- }
- private static double[,] GetMinor(double[,] matrix, int n)
- {
- double[,] result = new double[matrix.GetLength(0) - 1, matrix.GetLength(0) - 1];
- for (int i = 1; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < n; j++)
- result[i - 1, j] = matrix[i, j];
- for (int j = n + 1; j < matrix.GetLength(0); j++)
- result[i - 1, j - 1] = matrix[i, j];
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement