コード例 #1
0
        /// <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);
        }
コード例 #2
0
        public static double Determinant0(double[][] matrix)
        {
            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            var rows = matrix.Length;
            //var cols = matrix[0].Length;

            var result = 0d; // Initialize result

            // Base case : if matrix contains single element
            if (rows == 1)
            {
                return(matrix[0][0]);
            }
            else if (rows == 2)
            {
                result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
                return(result);
            }

            var sign = 1d; // To store sign multiplier

            // Iterate for each element of first row
            for (var f = 0; f < rows; f++)
            {
                // Getting Cofactor of A[0][f]
                var temp = JaggedMatrixSparseCofactorTests.Cofactor(matrix, 0, f);
                result += sign * matrix[0][f] * Determinant(temp);

                // terms are to be added with alternate sign
                sign = -sign;
            }

            return(result);
        }