public static double Determinant2(double[,] matrix) //{ // var result = 0d; // var rows = matrix.GetLength(0); // var cols = matrix.GetLength(1); // if (rows == 1) // { // return matrix[0, 0]; // } // else if (rows == 2) // { // return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]; // } // for (var i = 0; i < cols; i++) // { // var temp = new double[rows - 1, cols - 1]; // for (var j = 1; j < rows; j++) // { // Array.Copy(matrix, 0, temp[j - 1], 0, i); // Array.Copy(matrix, i + 1, temp[j - 1], i, cols - i - 1); // } // result += matrix[0, i] * Math.Pow(-1, i) * Determinant(temp); // } // return result; //} => JaggedMatrixDeterminantTests.Determinant(matrix.ToJaggedArray()); // Convert to jagged array until the above copy code can be understood and fixed.
public static double[][] Inverse1(double[][] matrix) { if (matrix is null) { throw new ArgumentNullException(nameof(matrix)); } // Find determinant of [][]A var det = JaggedMatrixDeterminantTests.Determinant(matrix); if (det == 0) { return(default);
/// <summary> /// The co-factor is the determinant of the matrix that remains when the row and column containing the /// specified element is removed. The co-factor may also be multiplied by -1, depending on the element's position: /// + - + - /// - + - + /// + - + - /// </summary> /// <param name="matrix">The matrix.</param> /// <param name="col">column number (starting at 0)</param> /// <param name="row">row number (starting at 0)</param> /// <returns> /// The cofactor of the specified element /// </returns> /// <exception cref="InvalidOperationException">Matrix must have the same number of rows and columns for the co-factor to be calculated</exception> public static double CoFactor1(double[][] matrix, int col, int row) { var rows = matrix.Length; var cols = matrix[0].Length; if (cols != rows) { throw new InvalidOperationException("Matrix must have the same number of rows and columns for the co-factor to be calculated"); } var array = JaggedGetMatrixMinorTests.GetMinor(matrix, col, row); var cofactor = JaggedMatrixDeterminantTests.Determinant(array); // need to work out sign: var i = col - row; if ((i % 2) != 0) { cofactor = -cofactor; } return(cofactor); }
/// <summary> /// Function to get adjoint of the specified matrix. /// </summary> /// <param name="matrix">The matrix.</param> /// <returns></returns> /// <acknowledgment> /// https://www.geeksforgeeks.org/adjoint-inverse-matrix/ /// </acknowledgment> //[DisplayName("Matrix Adjoint")] //[Description("Adjoint a matrix.")] //[Acknowledgment("https://www.geeksforgeeks.org/adjoint-inverse-matrix/")] //[SourceCodeLocationProvider] //[DebuggerStepThrough] //[MethodImpl(MethodImplOptions.AggressiveInlining)] public static double[][] Adjoint1(double[][] matrix) { if (matrix is null) { throw new ArgumentNullException(nameof(matrix)); } var rows = matrix.Length; var cols = matrix[0].Length; if (rows == 1) { return(new double[][] { new double[] { 1d } }); } var adj = new double[rows][]; for (var i = 0; i < rows; i++) { adj[i] = new double[cols]; } for (var i = 0; i < rows; i++) { for (var j = 0; j < cols; j++) { // Get cofactor of A[i,j] var temp = JaggedMatrixSparseCofactorTests.Cofactor(matrix, i, j); // Sign of adj[j,i] positive if sum of row and column indexes is even. var sign = ((i + j) % 2d == 0d) ? 1d : -1d; // Interchanging rows and columns to get the transpose of the cofactor matrix adj[j][i] = sign * JaggedMatrixDeterminantTests.Determinant(temp); } } return(adj); }