七度&光 2018-10-29 04:18 采纳率: 22.2%
浏览 455

C 中向量的 QR分解

I first wrote a function to do a QR decomposition using the Householder algorithm and it worked for random matrices. But now I'm trying to get my function for computing Q* b to work. The algorithm is essentially the same, but now we multiply by vector b at the end rather than by matrix A. Yet, after 8 hours of work, I couldn't figure out what is wrong in my code. I'm just a beginner in C and would really appreciate some clues.

void qrhh2(int m, int n, double **A, double *b) { // Replace input vector b with Q* b, Q in A = QR
double **V; // Initialize outer product matrix V and W for W = VA
double *e; // Init basis vector

for (int k=0; k<m; k++) { // Create operable column vector
    double *x = calloc(m-k, sizeof(double));
    for (int l=k; l<m; l++) {
        x[l-k] = A[l][k];
    }

    double *v = calloc(m-k, sizeof(double)); // Fill v with 0s
    e = calloc(m-k, sizeof(double *)); // Fill basis vector e with 0s
    e[0] = 1; // Complete making basis vector e
    for (int j=k; j<m; j++) {
        v[j-k] = sign(x[0]) * norm(m-k, x)*e[j-k] + x[j-k];
    }

    normalize(m-k, v);

    /* Fill V, W with 0s, initialize matrices V, W */
    V = calloc(m-k, sizeof(double *));
    for (int i=k; i<m; i++) V[i-k] = malloc((m-k) * sizeof(double));
    for (int i=k; i<m; i++) { // Create matrix V by outer product v*v^*
            for (int j=k; j<m; j++) {
            V[i-k][j-k] = v[i-k] * v[j-k];
        }

    }

    double *c = malloc((m-k)*sizeof(double));
    for (int i=0; i<m-k; i++) {
        c[i] = b[k+i];
    }
    double *r = mat_vec_mult(m-k, m-k, V, c);


    for (int i=k; i<m; i++) { // Create R from A=QR
            b[i] = b[i] -  2*r[i-k];
        }
    free(V);
}

}

Please let me know if I should also post the functions used in this code, such as normalize, as well as the code I am using to test it. I get the first entry of the new vector b correct, but not the rest of the entries.

转载于:https://stackoverflow.com/questions/53038689/qr-decomposition-for-vector-in-c

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!