SqMatrix SqMatrix::inverse() const { /* Reference: https://chi3x10.wordpress.com/2008/05/28/calculate-matrix-inversion-in-c/ */ SqMatrix inv(order); double det = determinant(); for (int i=0; i<order; i++) { for (int j=0; j<order; j++) { inv.arr[j][i] = minorMat(i,j).determinant() * ((i+j)%2 == 1 ? -1.0 : 1.0) / det; } } return inv; }
//A bad revursive implementation of diagnol minor, a better way could be using permutations/combinations to select the //rows/columns cplxnum CplxMatrix::diagonalMinor(int level) const{ cplxnum result; if (r != c) { std::cout<<"incompatible shapes"<<std::endl; return cplxnum(); } if (level <= 0) { std::cout<<"invalid level specification"<<std::endl; return result; } if (level == 1) { for (int i = 0; i < r; i++) { result = result + minor(i, i); } return result; } for (int i = 0; i < r; i++) { result = result + (1/double(level))*minorMat(i, i).diagonalMinor(level - 1); } return result; }
cplxnum CplxMatrix::minor(int row, int col) const{ return minorMat(row, col).det(); }
//recursive call for permenant, ignoring the signs for det cplxnum CplxMatrix::subperm(int row, int col) const{ return minorMat(row, col).perm(); }
double Matrix::minor(int row, int col) const{ return minorMat(row, col).det(); }