使用高斯消元计算矩阵的逆时非常高的不准确度

时间:2017-09-25 02:11:42

标签: c++ math matrix floating-point matrix-inverse

我正在研究一个c ++代码库,它使用矩阵库来计算各种事物。其中之一是计算矩阵的逆。它使用高斯elimation来实现这一点。但结果非常不准确。因此,将逆矩阵与原始矩阵相乘甚至不会接近单位矩阵。

以下是用于计算逆的代码,矩阵是在数值类型以及行和列上模板化的:

/// \brief Take the inverse of the matrix.
/// \return A new matrix which is the inverse of the current one.
matrix<T, M, M> inverse() const
{
    static_assert(M == N, "Inverse matrix is only defined for square matrices.");

    // augmented the current matrix with the identiy matrix.
    auto augmented = this->augment(matrix<T, M, M>::get_identity());

    for (std::size_t i = 0; i < M; i++)
    {
        // divide the current row by the diagonal element.
        auto divisor = augmented[i][i];
        for (std::size_t j = 0; j < 2 * M; j++)
        {
            augmented[i][j] /= divisor;
        }

        // For each element in the column of the diagonal element that is currently selected
        // set all element in that column to 0 except the diagonal element by using the currently selected row diagonal element.

        for (std::size_t j = 0; j < M; j++)
        {
            if (i == j)
            {
                continue;
            }

            auto multiplier = augmented[j][i];
            for (std::size_t k = 0; k < 2 * M; k++)
            {
                augmented[j][k] -= multiplier * augmented[i][k];
            }
        }
    }

    // Slice of the the new identity matrix on the left side.
    return augmented.template slice<0, M, M, M>();
}

现在我已经进行了单元测试,使用预先计算的值测试反正是否正确。我尝试两个矩阵一个3x3和一个4x4。我使用这个网站来计算逆:https://matrix.reshish.com/,它们确实匹配到一定程度。因为单元测试确实成功了。但是,一旦我计算出原始矩阵*,就可以实现甚至类似于单位矩阵的逆矩阵。请参阅下面代码中的注释。

BOOST_AUTO_TEST_CASE(matrix_inverse)
{
    auto m1 = matrix<double, 3, 3>({
        {7, 8, 9},
        {10, 11, 12},
        {13, 14, 15}
    });

    auto inverse_result1 = matrix<double,3, 3>({
        {264917625139441.28, -529835250278885.3, 264917625139443.47},
        {-529835250278883.75, 1059670500557768, -529835250278884.1},
        {264917625139442.4, -529835250278882.94, 264917625139440.94}
    });

    auto m2 = matrix<double, 4, 4>({
        {7, 8, 9, 23},
        {10, 11, 12, 81},
        {13, 14, 15, 11},
        {1, 73, 42, 65}
    });

    auto inverse_result2 = matrix<double, 4, 4>({
        {-0.928094660194201, 0.21541262135922956, 0.4117111650485529, -0.009708737864078209},
        {-0.9641231796116679, 0.20979975728155775, 0.3562651699029188, 0.019417475728154842},
        {1.7099261731391882, -0.39396237864078376, -0.6169346682848 , -0.009708737864076772 },
        {-0.007812499999999244, 0.01562499999999983, -0.007812500000000278, 0}
    });


    // std::cout << (m1.inverse() * m1) << std::endl;
    // results in
    // 0.500000000     1.000000000     -0.500000000
    // 1.000000000     0.000000000     0.500000000
    // 0.500000000     -1.000000000    1.000000000
    // std::cout << (m2.inverse() * m2) << std::endl; 
    // results in
    // 0.396541262     -0.646237864    -0.689016990    -2.162317961
    // 1.206917476     2.292475728     1.378033981     3.324635922
    // -0.884708738    -0.958737864    -0.032766990    -3.756067961
    // -0.000000000    -0.000000000    -0.000000000    1.000000000

    BOOST_REQUIRE_MESSAGE(
        m1.inverse().fuzzy_equal(inverse_result1, 0.1) == true,
        "3x3 inverse is not the expected result."
    );

    BOOST_REQUIRE_MESSAGE(
        m2.inverse().fuzzy_equal(inverse_result2, 0.1) == true,
        "4x4 inverse is not the expected result."
    );
}

我在我的智慧结束。我绝不是矩阵数学方面的专家,因为我必须在工作中学到这一切,但这真的让我感到难过。

完整的代码矩阵类可在以下位置获得: https://codeshare.io/johnsmith

404行是反函数所在的位置。

感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

正如评论中已经确定的那样,感兴趣的矩阵是单数的,因此没有逆。

很好,您的测试已经发现代码中的第一个问题 - 这种情况没有得到妥善处理,也没有出现错误。

更大的问题是,这不容易被发现:如果由于舍入错误而没有错误,那将是一块蛋糕 - 只是测试除数不是0!但浮点运算中存在舍入误差,因此除数将是非常小的非零数。

并且无法判断由于舍入误差导致的非零值还是矩阵接近奇异(但不是单数)的事实。但是,如果矩阵接近奇异,则条件很差,因此无论如何都不能信任结果。

理想情况下,算法不仅应该计算逆矩阵,还应该(估计)原始矩阵的条件,这样调用者就可以对不良条件做出反应。

使用知名且经过良好测试的库进行此类计算可能是明智之举 - 需要考虑很多并且可以做错的事情。

相关问题