如何计算矩阵的逆矩阵?

发布于 2024-07-09 20:26:43 字数 37 浏览 14 评论 0原文

有人可以告诉我如何计算矩阵的逆吗? 我用的是VC++6.0

Can somebody show me how to calculate the inverse of a matrix? I'm using VC++ 6.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-07-16 20:26:43

MFC 不是数值方法的工具。


计算 nxn 矩阵的逆矩阵很简单。 我给你算法:

/*  I took this from my implementation of CMatrix
 *  It works, but I'm not sure if it's the most efficient algorithm.
 *
 *  1. Start with Q = Identity, whose inverse is R = Identity.
 *  2. Set i = 0
 *  3. Replace the i-th column (zero-based count) vector of Q with the i-th
 *     column of the input matrix. This is an update of rank 1, so...
 *  4. Using the Sherman-Morrison formula, update R (the inverse of Q).
 *  5. The Sherman-Morrison formula also updates the determinant of the matrix.
 *     If it's zero, then the original matrix was not invertible.
 *  6. Increment i
 *  7. If i = n, stop
 *
 *  NOTES:
 *
 *  This algorithm has the advantage of calculating the determinant of the original
 *  matrix in the process.
 *
 *  My CMatrix class allows for general m*n matrices, it has these members:
 *  ldouble** x;    // there's a typedef long double ldouble; in the header
 *  UINT row, col;  // row count, column count
 *
 *  My CMatTmp class is similar to CMatrix (it has the same members),
 *  but it represents a temporal matrix used in internal calculations
 *
 *  My CVector class allows for n-dimensional vectors, it has these members:
 *  ldouble* x;
 *  UINT dim;
 */

CMatTmp CMatrix::DetInv(ldouble& det) const
{
    // The matrix must be square.
    if (row != col) throw 0;

    CMatTmp Rc(row), Rn(row);  // Start with identity row*row matrices
    CVector cc(row), lf(row);
    det = 1;

    for (UINT j = 0; j < col; ++j)
    {
        // Get the j-th column vector and subtract one from its j-th component.
        for (UINT i = 0; i < row; ++i)
            cc.x[i] = x[i][j];
        cc.x[j] -= 1;

        // Test whether the Sherman-Morrison corrector can be applied.
        lf = Rc * cc;
        ldouble den = 1 + lf.x[j];
        if (!abs(den))
        {
            det = 0;
            return CMatTmp(row,col);
        }

        // Update the determinant.
        det *= den;

        // Apply the Sherman-Morrison corrector.
        for (UINT i = 0; i < row; ++i)
            for (UINT k = 0; k <= j; ++k)
                Rn.x[i][k] -= lf.x[i] * Rc.x[j][k] / den;

        // Copy all relevant data from Rn to Rc.
        for (UINT i = 0; i < row; ++i)
            for (UINT k = 0; k <= j; ++k)
                Rc.x[i][k] = Rn.x[i][k];
    }

    return Rc;
}

MFC is not a tool for numerical methods.


Calculating the inverse of an nxn matrix is simple. I'll give you the algorithm:

/*  I took this from my implementation of CMatrix
 *  It works, but I'm not sure if it's the most efficient algorithm.
 *
 *  1. Start with Q = Identity, whose inverse is R = Identity.
 *  2. Set i = 0
 *  3. Replace the i-th column (zero-based count) vector of Q with the i-th
 *     column of the input matrix. This is an update of rank 1, so...
 *  4. Using the Sherman-Morrison formula, update R (the inverse of Q).
 *  5. The Sherman-Morrison formula also updates the determinant of the matrix.
 *     If it's zero, then the original matrix was not invertible.
 *  6. Increment i
 *  7. If i = n, stop
 *
 *  NOTES:
 *
 *  This algorithm has the advantage of calculating the determinant of the original
 *  matrix in the process.
 *
 *  My CMatrix class allows for general m*n matrices, it has these members:
 *  ldouble** x;    // there's a typedef long double ldouble; in the header
 *  UINT row, col;  // row count, column count
 *
 *  My CMatTmp class is similar to CMatrix (it has the same members),
 *  but it represents a temporal matrix used in internal calculations
 *
 *  My CVector class allows for n-dimensional vectors, it has these members:
 *  ldouble* x;
 *  UINT dim;
 */

CMatTmp CMatrix::DetInv(ldouble& det) const
{
    // The matrix must be square.
    if (row != col) throw 0;

    CMatTmp Rc(row), Rn(row);  // Start with identity row*row matrices
    CVector cc(row), lf(row);
    det = 1;

    for (UINT j = 0; j < col; ++j)
    {
        // Get the j-th column vector and subtract one from its j-th component.
        for (UINT i = 0; i < row; ++i)
            cc.x[i] = x[i][j];
        cc.x[j] -= 1;

        // Test whether the Sherman-Morrison corrector can be applied.
        lf = Rc * cc;
        ldouble den = 1 + lf.x[j];
        if (!abs(den))
        {
            det = 0;
            return CMatTmp(row,col);
        }

        // Update the determinant.
        det *= den;

        // Apply the Sherman-Morrison corrector.
        for (UINT i = 0; i < row; ++i)
            for (UINT k = 0; k <= j; ++k)
                Rn.x[i][k] -= lf.x[i] * Rc.x[j][k] / den;

        // Copy all relevant data from Rn to Rc.
        for (UINT i = 0; i < row; ++i)
            for (UINT k = 0; k <= j; ++k)
                Rc.x[i][k] = Rn.x[i][k];
    }

    return Rc;
}
饮湿 2024-07-16 20:26:43

另一个很好的来源是Numerical Recipes,尽管这可能有点矫枉过正。

Another good source is Numerical Recipes, although that might be a little overkill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文