示例#1
0
        /// <summary>
        /// Inverts the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
        /// <exception cref="System.ArgumentException">The matrix is not square.</exception>
        public static Matrix Invert(Matrix matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", "The matrix is null.");
            }

            LUDecomposition luDecomposition = new LUDecomposition(matrix);

            luDecomposition.Compute();

            Matrix inverse = new Matrix(matrix.NumberOfColumns, matrix.NumberOfRows);

            for (int i = 0; i < matrix.NumberOfRows; ++i)
            {
                Vector b = new Vector(matrix.NumberOfColumns, 0);
                b[i] = 1;       // identity vector

                Vector y = SolveEquation(luDecomposition, b);

                for (int j = 0; j < inverse.NumberOfRows; ++j)
                {
                    inverse[j, i] = y[j];
                }
            }
            return(inverse);
        }
示例#2
0
        /// <summary>
        /// Computes the determinant of the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <returns>The determinant of the specified matrix.</returns>
        /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
        /// <exception cref="System.ArgumentException">The matrix is not square.</exception>
        public static Double ComputeDeterminant(Matrix matrix)
        {
            LUDecomposition luDecomposition = new LUDecomposition(matrix);

            luDecomposition.Compute();
            return(luDecomposition.Determinant);
        }
示例#3
0
        /// <summary>
        /// Decomposes the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
        /// <exception cref="System.ArgumentException">The matrix is not square.</exception>
        public static Matrix Decompose(Matrix matrix)
        {
            LUDecomposition luDecomposition = new LUDecomposition(matrix);

            luDecomposition.Compute();
            return(luDecomposition.LU);
        }
示例#4
0
        /// <summary>
        /// Decomposes the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix to decompose.</param>
        /// <param name="l">The L (lower triangular) matrix.</param>
        /// <param name="u">The U (upper triangular) matrix.</param>
        /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
        /// <exception cref="System.ArgumentException">The matrix is not square.</exception>
        public static void Decompose(Matrix matrix, out Matrix l, out Matrix u)
        {
            LUDecomposition luDecomposition = new LUDecomposition(matrix);

            luDecomposition.Compute();
            l = luDecomposition.L;
            u = luDecomposition.U;
        }
示例#5
0
        /// <summary>
        /// Solves a linear equation system.
        /// </summary>
        /// <param name="luDecomposition">The LU decomposition.</param>
        /// <param name="b">The vector.</param>
        /// <returns>The vector containing the unknown variables of the equation.</returns>
        private static Vector SolveEquation(LUDecomposition luDecomposition, Vector b)
        {
            Vector pb = new Vector(b.Size);

            // P*b
            for (int i = 0; i < b.Size; ++i)
            {
                double s = 0;
                for (int j = 0; j < luDecomposition.P.NumberOfColumns; ++j)
                {
                    s += luDecomposition.P[i, j] * b[j];
                }
                pb[i] = s;
            }

            // L*y = P*b with forward substitution
            Vector y = new Vector(pb.Size);

            for (int i = 0; i < luDecomposition.L.NumberOfRows; ++i)
            {
                y[i] = pb[i];
                for (int j = 0; j < i; ++j)
                {
                    y[i] -= luDecomposition.L[i, j] * y[j];
                }
                y[i] /= luDecomposition.L[i, i] == 0 ? 1 : luDecomposition.L[i, i];
            }

            // U*x = y with back substitution
            Vector x = new Vector(y.Size);

            for (int i = x.Size - 1; i >= 0; --i)
            {
                x[i] = y[i];
                for (int j = i + 1; j < luDecomposition.U.NumberOfRows; ++j)
                {
                    x[i] -= luDecomposition.U[i, j] * x[j];
                }

                x[i] /= (luDecomposition.U[i, i] == 0 ? 1 : luDecomposition.U[i, i]);
            }

            return(x);
        }
示例#6
0
        /// <summary>
        /// Solves a linear equation system.
        /// </summary>
        /// <param name="a">The left side of the equation represented by a matrix.</param>
        /// <param name="b">The right side of the equation represented by a vector.</param>
        /// <returns>The vector containing the unknown variables of the equation.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// The matrix is null.
        /// or
        /// The vector is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The matrix is not square.
        /// or
        /// The size of the matrix does not match the size of the vector.
        /// </exception>
        public static Vector SolveEquation(Matrix a, Vector b)
        {
            if (a == null)
            {
                throw new ArgumentNullException("a", "The matrix is null.");
            }
            if (b == null)
            {
                throw new ArgumentNullException("b", "The vector is null.");
            }
            if (!a.IsSquare)
            {
                throw new ArgumentException("The matrix is not square.", "a");
            }
            if (a.NumberOfRows != b.Size)
            {
                throw new ArgumentException("The size of the matrix does not match the size of the vector.", "b");
            }

            LUDecomposition luDecomposition = new LUDecomposition(a);

            luDecomposition.Compute();
            return(SolveEquation(luDecomposition, b));
        }
示例#7
0
 /// <summary>
 /// Computes the determinant of the matrix.
 /// </summary>
 /// <param name="matrix">The matrix.</param>
 /// <returns>The determinant of the matrix.</returns>
 /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
 public static Double ComputeDeterminant(this Matrix matrix)
 {
     return(LUDecomposition.ComputeDeterminant(matrix));
 }
示例#8
0
 /// <summary>
 /// Inverts the specified matrix.
 /// </summary>
 /// <param name="matrix">The matrix.</param>
 /// <returns>The inverse matrix.</returns>
 public static Matrix Invert(this Matrix matrix)
 {
     return(LUDecomposition.Invert(matrix));
 }