Exemple #1
0
        /// <summary>
        ///   Solves a set of equation systems of type <c>A * X = B</c>.
        /// </summary>
        /// <param name="value">Right hand side matrix with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>Matrix <c>X</c> so that <c>L * U * X = B</c>.</returns>
        ///
        public decimal[,] Solve(decimal[,] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.GetLength(0) != rows)
            {
                throw new DimensionMismatchException("value", "The matrix should have the same number of rows as the decomposition.");
            }

            if (!Nonsingular)
            {
                throw new InvalidOperationException("Matrix is singular.");
            }


            // Copy right hand side with pivoting
            int count = value.GetLength(1);

            decimal[,] X = value.Submatrix(pivotVector, null);


            // Solve L*Y = B(piv,:)
            for (int k = 0; k < cols; k++)
            {
                for (int i = k + 1; i < cols; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * lu[i, k];
                    }
                }
            }

            // Solve U*X = Y;
            for (int k = cols - 1; k >= 0; k--)
            {
                for (int j = 0; j < count; j++)
                {
                    X[k, j] /= lu[k, k];
                }

                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * lu[i, k];
                    }
                }
            }

            return(X);
        }
Exemple #2
0
        /// <summary>
        ///   Solves a set of equation systems of type <c>X * A = B</c>.
        /// </summary>
        /// <param name="value">Right hand side matrix with as many columns as <c>A</c> and any number of rows.</param>
        /// <returns>Matrix <c>X</c> so that <c>X * L * U = A</c>.</returns>
        ///
        public decimal[,] SolveTranspose(decimal[,] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.GetLength(0) != rows)
            {
                throw new DimensionMismatchException("value", "The matrix should have the same number of rows as the decomposition.");
            }

            if (!Nonsingular)
            {
                throw new SingularMatrixException("Matrix is singular.");
            }


            // Copy right hand side with pivoting
            var X = value.Submatrix(null, pivotVector);

            int count = X.GetLength(1);

            // Solve L*Y = B(piv,:)
            for (int k = 0; k < rows; k++)
            {
                for (int i = k + 1; i < rows; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[j, i] -= X[j, k] * lu[i, k];
                    }
                }
            }

            // Solve U*X = Y;
            for (int k = rows - 1; k >= 0; k--)
            {
                for (int j = 0; j < count; j++)
                {
                    X[j, k] /= lu[k, k];
                }

                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[j, i] -= X[j, k] * lu[i, k];
                    }
                }
            }

            return(X);
        }