///<summary>Calculates the inverse of the matrix.</summary> ///<returns>the inverse of the matrix.</returns> ///<exception cref="NotPositiveDefiniteException">A is not positive definite.</exception> public FloatMatrix GetInverse() { Compute(); if (!ispd) { throw new NotPositiveDefiniteException(); } else { #if MANAGED FloatMatrix ret = FloatMatrix.CreateIdentity(order); ret = Solve(ret); return(ret); #else float[] inverse = new float[l.data.Length]; Array.Copy(l.data, inverse, l.data.Length); Lapack.Potri.Compute(Lapack.UpLo.Lower, order, inverse, order); FloatMatrix ret = new FloatMatrix(order, order); ret.data = inverse; for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { if (j > i) { ret.data[j * order + i] = ret.data[i * order + j]; } } } return(ret); #endif } }
///<summary>Calculates the inverse of the matrix.</summary> ///<returns>the inverse of the matrix.</returns> ///<exception cref="SingularMatrixException">matrix is singular.</exception> public FloatMatrix GetInverse() { Compute(); if (singular) { throw new SingularMatrixException(); } else { #if MANAGED FloatMatrix ret = FloatMatrix.CreateIdentity(order); ret = Solve(ret); return(ret); #else float[] inverse = new float[factor.Length]; Array.Copy(factor, inverse, factor.Length); Lapack.Getri.Compute(order, inverse, order, pivots); FloatMatrix ret = new FloatMatrix(order, order); ret.data = inverse; return(ret); #endif } }
/// <summary>Performs the QR factorization.</summary> protected override void InternalCompute() { int m = matrix.RowLength; int n = matrix.ColumnLength; #if MANAGED int minmn = m < n ? m : n; r_ = new FloatMatrix(matrix); // create a copy var u = new FloatVector[minmn]; for (int i = 0; i < minmn; i++) { u[i] = Householder.GenerateColumn(r_, i, m - 1, i); Householder.UA(u[i], r_, i, m - 1, i + 1, n - 1); } q_ = FloatMatrix.CreateIdentity(m); for (int i = minmn - 1; i >= 0; i--) { Householder.UA(u[i], q_, i, m - 1, i, m - 1); } #else qr = new float[matrix.data.Length]; Array.Copy(matrix.data, qr, matrix.data.Length); jpvt = new int[n]; jpvt[0] = 1; Lapack.Geqp3.Compute(m, n, qr, m, jpvt, out tau); r_ = new FloatMatrix(m, n); // Populate R for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i <= j) { r_.data[j * m + i] = qr[(jpvt[j] - 1) * m + i]; } else { r_.data[j * m + i] = 0.0f; } } } q_ = new FloatMatrix(m, m); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (j < n) { q_.data[j * m + i] = qr[j * m + i]; } else { q_.data[j * m + i] = 0.0f; } } } if (m < n) { Lapack.Orgqr.Compute(m, m, m, q_.data, m, tau); } else { Lapack.Orgqr.Compute(m, m, n, q_.data, m, tau); } #endif for (int i = 0; i < m; i++) { if (q_[i, i] == 0) { isFullRank = false; } } }