public static void InverseDeterminant(Matrix matrix, out Matrix inverse, out double determinant) { int n = matrix.Rows; if (matrix.Columns != n) { throw new ArgumentException("The matrix isn't a square matrix."); } double[,] a = matrix.ToArray(); if (!trfac.spdmatrixcholesky(ref a, n, false)) { throw new ArithmeticException(); } determinant = matdet.spdmatrixcholeskydet(ref a, n); int info = 0; matinv.matinvreport rep = new matinv.matinvreport(); matinv.spdmatrixcholeskyinverse(ref a, n, false, ref info, ref rep); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { a[i, j] = a[j, i]; } } inverse = new Matrix(a); }
/// <summary> /// Matrix inversion using LU decomposition. /// </summary> public bool TryInverse(out Matrix inverse) { if (n == 0 && m == 0) { // Handle degenerate case. inverse = Matrix.Zero(0, 0); return true; } if (n == m) { double[,] x = (double[,])a.Clone(); int info = 0; matinv.matinvreport rep = new matinv.matinvreport(); // Compute the inverse. matinv.rmatrixluinverse(ref x, ref pivots, n, ref info, ref rep); if (info == 1) { inverse = new Matrix(x); return true; } } inverse = null; return false; }