使用b ^ nth幂找到矩阵的逆矩阵

时间:2016-12-02 22:06:15

标签: c matrix

我已经搜索了几个小时,并花了很多时间试图找出如何解决这个问题。我需要使用找到预定义矩阵的逆 A^-1 = I + (B + B^2 + ... + B^20)其中B = I-A

void invA(double a[][3], double id[][3], double z[][3])
{
    int i, j, n, k;
    double pb[3][3]   = {1.,0.,0.,0.,1.,0.,0.,0.,1.};
    double temp[3][3] = {1.,0.,0.,0.,1.,0.,0.,0.,1.};
    double b[3][3];

    temp[i][j] = 0;
    b[i][j]    = 0;

    for(i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            b[i][j] = id[i][j] - a[i][j];

    for (n = 0; n < 20; n++)                        //run loop n times
    {
        for (i = 0; i < 3; i++)                     //find b to the power 20
            for (j = 0; j < 3; j++)
                for (k = 0; k < 3; k++)
                    temp[i][j] += pb[i][k] * b[k][j];

        for (i = 0; i < 3; i++)                      //allocate pb from temp
            for (j = 0; j < 3; j++) 
                pb[i][j] = temp[i][j];

        for (i = 0; i < 3; i++)                       //summing b n time
            for (j = 0; j < 3; j++)                   //to find inverse
                z[i][j] = z[i][j] + pb[i][j];
    }
}

矩阵a是定义的矩阵,id是标识,z是反转(结果)。我似乎无法弄清楚我哪里出错了。

1 个答案:

答案 0 :(得分:2)

你几乎没有问题。

首先,函数开头的bcm2835_peripheralgpio使用未初始化的变量GPIO_BASEGPIO_BASE。行为未定义,谁知道select [Date], sum(Amount) as Amount from yourTable group by [Date] 实际上是如何初始化的。

然后,SELECT avg(bigtable.gizmo.attendee_nps) FROM ( SELECT DISTINCT attendee_survey_results.swoop_event_id AS "Swoop ID", attendee_survey_results.startup_weekend_city AS "SW City", swooptable.start_date AS "Date", gizmo.attendee_nps AS "Attendee NPS" FROM attendee_survey_results JOIN ( SELECT swoop_event_id, ( 100 * count(CASE WHEN attendee_nps >= 9 THEN 1 END) / count(attendee_nps) - 100 * count(CASE WHEN attendee_nps <= 6 THEN 1 END) / count(attendee_nps) ) AS "attendee_nps" FROM attendee_survey_results GROUP BY swoop_event_id ) AS "gizmo" ON gizmo.swoop_event_id = attendee_survey_results.swoop_event_id JOIN ( SELECT eid,start_date,manager_email FROM events ) AS "swooptable" ON gizmo.swoop_event_id = swooptable.eid ) AS bigtable 必须在每次迭代时重新初始化为零矩阵 。我不知道你的代码到底在做什么,但它肯定不是一种强大的力量。

最后,(除非temp[i][j] = 0;初始化为b[i][j] = 0;),否则您缺少初始条款。

所有这一切,我强烈建议将大多数循环分解为函数:ij。一旦他们进行了单元测试,剩下的就更简单了。