Example #1
0
//Inverse is implemented using adjoint method
CplxMatrix CplxMatrix::inverse() const{
    if (r != c) {
        std::cout<<"incompatible shapes"<<std::endl;
        CplxMatrix result;
        return result;
    }
    if (det().re == 0&&det().im==0) {
        std::cout<<"singular matrix encountered"<<std::endl;
        CplxMatrix result;
        return result;
    }
    return cofactorMatrix().transpose().scalarMul(1.0/det());
}
Example #2
0
MatrixMxN<Scalar> MatrixMxN<Scalar>::inverse() const
{
    unsigned int rows = (*this).rows();
    unsigned int cols = (*this).cols();
    if(rows!=cols)
    {
        std::cerr<<"Matrix not square matrix, it's not invertible!\n";
        std::exit(EXIT_FAILURE);
    }
    Scalar det = determinant();
    if(det==0)
    {
        std::cerr<<"Matrix not invertible!\n";
        std::exit(EXIT_FAILURE);
    }
    MatrixMxN<Scalar> result = cofactorMatrix();
    result = result.transpose();
    result /= det;
    return result;
}
Example #3
0
void inverseMatrix(int n, double **matrix, double **matRes) {
    double det, value;
    double **minor;
    int i, j;
    det = determinantMatrix(matrix, n);
    det = 1 / det;
    createMatrix(&minor, n-1, n-1);

    for (j = 0; j < n; j++) {
        for (i = 0; i < n; i++) {
            // get the co-factor (matrix) of A(j,i)
            cofactorMatrix(n, j, i, matrix, minor);
            //GetMinor(A,minor,j,i,n);
            value = determinantMatrix(minor, n-1);
            matRes[i][j] = det * value;
            //Y[i][j] = det*CalcDeterminant(minor,order-1);
            if ((i + j) % 2 == 1)
                matRes[i][j] = -matRes[i][j];
        }
    }

    destroyMatrix(&minor, n-1, n-1);
}
Matrix<double> Matrix<double>::GetCofactorMatrix(size_t row, size_t col) const
{
    const size_t rowCount = GetRowLength();
    const size_t colCount = GetColumnLength();
    Matrix<double> cofactorMatrix(rowCount-1, colCount-1);

    for (size_t r = 0, ri = 0; ri < rowCount; ++ri)
    {
        if (ri == row)
        {
            continue;
        }
        for (size_t c = 0, ci = 0; ci < colCount; ++ci)
        {
            if (ci != col)
            {
                cofactorMatrix[r][c] = (*this)[ri][ci];
                ++c;
            }
        }
        ++r;
    }
    return cofactorMatrix;
}