简体   繁体   English

PLU 分解的逆矩阵

[英]Matrix inverse from PLU decomposition

I am trying to compute the matrix inverse from plu decomposition, but the results I get are wrong.我试图从 plu 分解计算逆矩阵,但我得到的结果是错误的。 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)函数Matrix<T>::gen_col从其输入生成一个列向量, solve_climb / solve_descent分别计算求解 AX=B 的列向量,其中 A 是三角矩阵(根据情况不同或优于)

FYI, The code throws the logic errors 'WHYY' when trying to check the calculations are right for each vector.仅供参考,当尝试检查每个向量的计算是否正确时,代码会抛出逻辑错误“WHYY”。

Any clue of where the code could be wrong ?代码可能出错的任何线索?

Thank you谢谢

EDIT : The full code is here (matrix_def.h) and here (matrix.h)编辑:完整代码在这里 (matrix_def.h)这里 (matrix.h)

As L is triangular inferior, you should use solve_descent , and not solve_climb .由于 L 是三角形劣势,您应该使用solve_descent ,而不是solve_climb The same thing goes for U (triangular superior), you need to use solve_climb . U(三角上级)也是如此,您需要使用solve_climb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM