/// <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 decomposition = new LUDecomposition(matrix); decomposition.Compute(); return(decomposition.Determinant); }
/// <summary> /// Decomposes the specified matrix. /// </summary> /// <param name="matrix">The matrix.</param> /// <returns>The decomposed (LU) matrix.</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 decomposition = new LUDecomposition(matrix); decomposition.Compute(); return(decomposition.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 decomposition = new LUDecomposition(matrix); decomposition.Compute(); l = decomposition.L; u = decomposition.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(nameof(a)); } if (b == null) { throw new ArgumentNullException(nameof(b)); } if (!a.IsSquare) { throw new ArgumentException(NumericsMessages.MatrixIsNotSquare, nameof(a)); } if (a.NumberOfRows != b.Size) { throw new ArgumentException(NumericsMessages.MatrixSizeDoesNotMatchVector, nameof(b)); } LUDecomposition decomposition = new LUDecomposition(a); decomposition.Compute(); return(SolveEquation(decomposition, b)); }
/// <summary> /// Inverts the specified matrix. /// </summary> /// <param name="matrix">The matrix.</param> /// <returns>The inverted matrix.</returns> /// <exception cref="System.ArgumentNullException">The matrix is null.</exception> /// <exception cref="System.ArgumentException">The matrix is not invertible.</exception> public static Matrix Invert(Matrix matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (matrix.All(value => value == 0)) { throw new ArgumentException(NumericsMessages.MatrixIsNotInvertible, nameof(matrix)); } LUDecomposition decomposition = new LUDecomposition(matrix); decomposition.Compute(); if (decomposition.Determinant == 0) { throw new ArgumentException(NumericsMessages.MatrixIsNotInvertible, nameof(matrix)); } Matrix inverse = new Matrix(matrix.NumberOfRows, matrix.NumberOfColumns); for (Int32 columnIndex = 0; columnIndex < matrix.NumberOfColumns; columnIndex++) { Vector b = VectorFactory.CreateUnitVector(matrix.NumberOfRows, columnIndex); Vector y = SolveEquation(decomposition, b); for (Int32 rowIndex = 0; rowIndex < inverse.NumberOfRows; ++rowIndex) { inverse[rowIndex, columnIndex] = y[rowIndex]; } } return(inverse); }