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];


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.


Csdn user default icon