void invert(Matrix& A) { if (determinant(A) == 0 || (A.m_rows!=A.m_columns)) //Actually need to check too if max(Coffactors[])* 1/det ->OVERFLOW { } else { Matrix Cofactors(A.m_rows, A.m_columns); //Compute the Coffactor elementes for (int i = 0; i < Cofactors.m_columns; i++) { for (int j = 0; j < Cofactors.m_rows; j++) { Matrix temp = A; double coefficient = pow(-1.0, i + j) ; temp.DeleteColumn(i); temp.DeleteRow(j); Cofactors.mat[j*A.m_columns + i] = coefficient*determinant(temp); } } Matrix Inverse = Cofactors.traspose() / determinant(A); A = Inverse; } }
Matrix Matrix::inverse() { if (determinant(*this) == 0 || (m_rows != m_columns)) //Actually need to check too if max(Coffactors[])* 1/det ->OVERFLOW { return *this;//do nothing } else { Matrix Cofactors(m_rows, m_columns); //Compute the Coffactor elementes for (int i = 0; i < Cofactors.m_columns; i++) { for (int j = 0; j < Cofactors.m_rows; j++) { Matrix temp = *this; double coefficient = pow(-1.0, i + j) /* *A.mat[j*A.m_columns + i]*/; temp.DeleteColumn(i); temp.DeleteRow(j); Cofactors.mat[j*m_columns + i] = coefficient*determinant(temp); } } Matrix Inverse = Cofactors.traspose() / determinant(*this); return Inverse; } }
Matrix Matrix::Inverse() { //transpose matrix Matrix Tran = Transpose(); //cofactors float *A = GetMatrix(); Matrix Cofactors((A[4]*A[8] - A[5]*A[7]), -(A[3]*A[8] - A[5]*A[6]), (A[3]*A[7] - A[4]*A[6]), -(A[1]*A[8] - A[2]*A[7]), (A[0]*A[8] - A[2]*A[6]), -(A[0]*A[7] - A[1]*A[6]), (A[1]*A[5] - A[2]*A[4]), -(A[0]*A[5] - A[2]*A[3]), (A[0]*A[4] - A[1]*A[3])); //find determinant float det1 = GetDeterminant(); //Scale by determinant to find inverse return Cofactors/det1; }