Example #1
0
        public static double[,] Inverse1(double[,] matrix)
        {
            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            // Find determinant of [,]A
            var det = GeneralMatrixDeterminantTests.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.GetLength(0);
            var cols = matrix.GetLength(1);

            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    = GeneralGetMatrixMinorTests.GetMinor(matrix, col, row);
            var cofactor = GeneralMatrixDeterminantTests.Determinant(array);
            // need to work out sign:
            var i = col - row;

            if ((i % 2) != 0)
            {
                cofactor = -cofactor;
            }

            return(cofactor);
        }
        public static double[,] Adjoint1(double[,] matrix)
        {
            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            var rows = matrix.GetLength(0);
            var cols = matrix.GetLength(1);

            if (rows == 1)
            {
                return(new double[1, 1] {
                    { 1d }
                });
            }

            var adj = new double[rows, cols];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    // Get cofactor of A[i,j]
                    var temp = GeneralMatrixSparseCofactorTests.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 * GeneralMatrixDeterminantTests.Determinant(temp);
                }
            }

            return(adj);
        }