/// <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); }
/// <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); }
/// <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); }
/// <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; }
/// <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)); }