QR 分解中的正交化输出稍微不准确的正交化矩阵

Orthogonalization in QR Factorization outputting slightly innaccurate orthogonalized matrix

我正在为 QR 分解编写代码,但出于某种原因,我的正交方法无法按预期工作。基本上,我的 proj() 方法输出随机投影。这是代码:

apmatrix<double> proj(apmatrix<double> v, apmatrix<double> u)   
//Projection of u onto v
{
//proj(v,u) = [(u dot v)/(v dot v)]*v
    double a = mult(transpose(u,u),v)[0][0], b = mult(transpose(v,v),v)[0][0], c = (a/b);
    apmatrix<double>k;
    k.resize(v.numrows(),v.numcols());
    for(int i = 0; i<v.numrows(); i++)
    {
        for(int j = 0; j<v.numcols(); j++)
        {
            k[i][j]=v[i][j]*c;
        }
    }
    return k;
}

我用手动矩阵输入自己测试了这个方法,它似乎工作正常。这是我的正交方法:

apmatrix<double> orthogonal(apmatrix<double> A)   //Orthogonal
{
    /*
    n = (number of columns of A)-1
    x = columns of A
    v0 = x0
    v1 = x1 - proj(v0,x1)
    vn = xn - proj(v0,xn) - proj(v1,xn) - ... - proj(v(n-1),xn)
    V = {v1, v2, ..., vn} or [v0 v1 ... vn]
    */
    apmatrix<double> V, x, v;
    int n = A.numcols();
    V.resize(A.numrows(),n);
    x.resize(A.numrows(), 1);
    v.resize(A.numrows(),1);
    for(int i = 0; i<A.numrows(); i++)
    {
        x[i][0]=A[i][1];
        v[i][0]=A[i][0];
        V[i][0]=A[i][0];
    }
    for (int c = 1; c<n; c++)   //Iterates through each col of A as if each was its own matrix
    {
        apmatrix<double>vn,vc; //vn = Orthogonalized v (avoiding matrix overwriting of v); vc = previously orthogonalized v
            vn=x;
        vc.resize(v.numrows(), 1);
        for(int i=0; i<c; i++)   //Vn = an-(sigma(t=1, n-1, proj(vt, xn))
        {
            for(int k = 0; k<V.numrows(); k++)
                vc[k][0] = V[k][i]; //Sets vc to designated v matrix
            apmatrix<double>temp = proj(vc, x);
            for(int j = 0; j<A.numrows(); j++)
            {
                vn[j][0]-=temp[j][0]; //orthogonalize matrix
            }
        }
        for(int k = 0; k<V.numrows(); k++)
        {
            V[k][c]=vn[k][0]; //Subtracts orthogonalized col to V
            v[k][0]=V[k][c]; //v is redundant. more of a placeholder
        }
        if((c+1)<A.numcols())  //Matrix Out of Bounds Checker
        {
            for(int k = 0; k<A.numrows(); k++)
            {
                vn[k][0]=0;
                vc[k][0]=0;
                x[k][0]=A[k][c+1]; //Moves x onto next v
            }
        }
    }
    system("PAUSE");
    return V;
}

出于测试目的,我一直在使用二维数组:[[1,1,4],[1,4,2],[1,4,2],[1,1,0]] .每列都是它自己的 4x1 矩阵。矩阵应分别输出为:[1,1,1,1]T、[-1.5,1.5,1.5,-1.5]T 和 [2,0,0,-2]T。现在发生的情况是第一列正确输出(它是同一个矩阵),但第二列和第三列输出可能相似但不等于它们的预期值。

同样,每次我调用正交方法时,它都会输出不同的东西。我认为这是由于在 proj() 方法中输入的数字,但我不完全确定。

apmatrix 来自 AP 大学董事会,当时他们教授 cpp。它类似于 Java 中的向量或 ArrayLists。

Here 是 link 到 apmatrix.cpp 和文档或条件(可能更有用),apmatrix.h。 Here 是完整代码的 link(我添加了视觉标记以查看计算机在做什么)。

可以公平地假设所有自定义方法都按预期工作(也许矩阵回归除外,但那是无关紧要的)。并确保在尝试因式分解之前使用 enter 方法输入矩阵。代码可能效率低下,部分原因是我不久前自学了 cpp,并且我一直在尝试不同的方法来修复我的代码。感谢您的帮助!

如评论所述:

@AhmedFasih After doing more tests today, I have found that it is in-fact some >memory issue. I found that for some reason, if a variable or an apmatrix object >is declared within a loop, initialized, then that loop is reiterated, the >memory does not entirely wipe the value stored in that variable or object. This >is noted in two places in my code. For whatever reason, I had to set the >doubles a,b, and c to 0 in the proj method and apmatrixdh to 0 in the >mult method or they would store some value in the next iteration. Thank you so >much for you help!