public static DualMatrix Inverse(DualMatrix matrix) { int m = matrix.Rows; if (matrix.Columns != m) { throw new ArgumentException("The matrix isn't a square matrix."); } if (m <= 3) { DualNumber determinant = DeterminantDirect(m, matrix); return InverseDirect(m, matrix, determinant); } int n = GradientLength(m, matrix); return Inverse(matrix, m, n, LUDecomposition.Inverse(matrix.GetValues())); }
/// <summary> /// This method saves some time if both the inverse and the determinant is needed by only having to compute /// the inverse once. /// </summary> public static void InverseDeterminant(DualMatrix matrix, out DualMatrix inverse, out DualNumber determinant) { int m = matrix.Rows; if (matrix.Columns != m) { throw new ArgumentException("The matrix isn't a square matrix."); } if (m <= 3) { // The naive implementation seems to be faster (for all values of n). Maybe it's // faster to perform the LU decomposition directly on the DualNumbers? determinant = DeterminantDirect(m, matrix); inverse = InverseDirect(m, matrix, determinant); return; } int n = GradientLength(m, matrix); LUDecomposition lu = LUDecomposition.Decompose(matrix.GetValues()); Matrix inverse0 = lu.Inverse(); double determinant0 = lu.Determinant(); inverse = Inverse(matrix, m, n, inverse0); determinant = Determinant(matrix, m, n, inverse0, determinant0); }