2

I am trying to compute the matrix inverse from plu decomposition, but the results I get are wrong. Using the debugger, I couldn't find a single step I could blame, so here is the code for the inversion function :

template<class T>
Matrix<T> Matrix<T>::inv() const {
    std::tuple<Matrix<T>, Matrix<T>, Matrix<T>> PLU(this->decomp_PLU());
    Matrix<T> P(std::get<0>(PLU));
    Matrix<T> L(std::get<1>(PLU));
    Matrix<T> U(std::get<2>(PLU));

    if (!this->lines() == this->cols()) { throw std::logic_error("Matrix must be square"); }
    unsigned N = this->lines();

    Matrix<T> Y(N, N);
    for (unsigned i = 0; i < N; i++) {
        Matrix<T> B(Matrix<T>::gen_col(P.transp().data[i])); // B is the ith column vector of P
        Y[i] = Matrix<T>::solve_climb(L, B).transp().data[0]; // Compute LY=P for each column vector

        Matrix<T> conf(L.dot(Matrix<T>::gen_col(Y[i])));
        if (!conf.allclose(B, 1e-9, 1e-9)) {
            throw std::logic_error("WHYY");
        }
    }
    Y = Y.transp();

    Matrix<T> X(N, N);
    for (unsigned i = 0; i < N; i++) {
        Matrix<T> B(Matrix<T>::gen_col(Y.transp().data[i])); // B is the ith column vector of Y
        X[i] = Matrix<T>::solve_descent(U, B).transp().data[0]; // Compute UX=Y for each column vector

        Matrix<T> conf(U.dot(Matrix<T>::gen_col(X[i])));
        if (!conf.allclose(B, 1e-9, 1e-9)) {
            throw std::logic_error("WHYY");
        }
    }
    X = X.transp();

    return X;
}

The function Matrix<T>::gen_col generates a column vector from its input, and solve_climb / solve_descent each compute the column vector that solves AX=B where A is a triangular matrix (inferior or superior depending on the case)

FYI, The code throws the logic errors 'WHYY' when trying to check the calculations are right for each vector.

Any clue of where the code could be wrong ?

Thank you

EDIT : The full code is here (matrix_def.h) and here (matrix.h)

3
  • 1
    Just wondering, how is someone, who may want to run and test and debug your code, be able to go about it, since the only thing provided here is the inv function and nothing else. No header files included! Where is the code for gen_col and solve_descent? What does Y.transp() do? What does Matrix<T> X(N,N); do? I know, I can assume you transpose a matrix and you create a matrix with those functions, but as it stands, this snippet of code is not an minimal reproducible example and therefore short of speculating on a solution, it is difficult, if not impossible to debug. Commented Apr 11, 2019 at 8:05
  • You are right. Edited my question with links to the full code ! Thanks
    – Magix
    Commented Apr 11, 2019 at 8:08
  • 1
    Those lines seem to compare two column vectors with absolute and relative admissible errors of 1e-9. Is it too small, maybe? Can you evaluate the distance between those two?
    – Bob__
    Commented Apr 11, 2019 at 9:43

1 Answer 1

2

As L is triangular inferior, you should use solve_descent, and not solve_climb. The same thing goes for U (triangular superior), you need to use solve_climb.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.