/******************************************************************************* * vector_t lin_system_solve_qr(matrix_t A, vector_t b) * * Gives a least-squares solution to the system AX=b for non-square A using QR. * * Ax=b * QRx=b * Rx=Q'b (because Q'Q=I) * then solve for x with gaussian elimination *******************************************************************************/ vector_t lin_system_solve_qr(matrix_t A, vector_t b){ vector_t xout = create_empty_vector(); vector_t temp; matrix_t Q,R; int i,k; if(!A.initialized || !b.initialized){ printf("ERROR: matrix or vector not initialized yet\n"); return xout; } // do QR decomposition if(QR_decomposition(A,&Q,&R)<0){ printf("failed to perform QR decomposition on A\n"); return xout; } // transpose Q matrix if(transpose_matrix(&Q)<0){ printf("ERROR: failed to transpose Q\n"); return xout; } // multiply through temp = matrix_times_col_vec(Q,b); destroy_matrix(&Q); // solve for x knowing R is upper triangular int nDim = R.cols; xout = create_vector(nDim); for(k=(nDim-1); k>=0; k--){ xout.data[k] = temp.data[k]; for(i=(k+1); i<nDim; i++){ xout.data[k] -= (R.data[k][i]*xout.data[i]); } xout.data[k] = xout.data[k] / R.data[k][k]; } destroy_matrix(&R); destroy_vector(&temp); return xout; }
/* ****************************************************************************** */ int main() { char file_name[100]; FILE *file; double **A, *b, *gamma, swap; int n, m, i, j, k, *permutation; printf("Nome do Arquivo: "); scanf("%s", file_name); file = fopen(file_name, "r"); if (file == NULL) { fprintf(stderr, "Não foi possível abrir o arquivo!\n"); return -1; } fscanf(file, "%d", &n); fscanf(file, "%d", &m); A = alloc_matrix(n, m); b = malloc(n * sizeof(double)); gamma = malloc(m * sizeof(double)); permutation = malloc (m * sizeof(int)); for(k = 0; k < m; k++) permutation[k] = k; for (k = 0; k < n*m; k ++) { fscanf(file, "%d %d", &i, &j); fscanf(file, "%lf", &A[i][j]); } for (k= 0; k < n; k ++) { fscanf(file, "%d", &i); fscanf(file, "%lf", &b[i]); } QR_decomposition(A, gamma, n, m, permutation); solve_QR_system(A, n, m, b, gamma); for (k = m - 1; k >= 0; k --) { swap = b[k]; b[k] = b[permutation[k]]; b[permutation[k]] = swap; } /*vetor de resposta em b*/ /*IMPORTANTE: so posso fazer isso pois o sistema e sobre-determinado*/ for (n = 0; n < m; n ++) printf("%f ", b[n]); printf("\n"); /* TESTE */ /*b[0] = 1; for (k = 0; k < n; k ++) printf("%f ", b[k]); printf("\n"); print_matrix(n, m, A); vector_times_matrix(b, A, n, m, 0); for (k = 0; k < m; k ++) printf("%f ", b[k]); printf("\n");*/ /* start = clock(); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; */ free(permutation); free(b); free(gamma); free_matrix(n, A); return 0; }
int main(){ printf("Let's test some linear algebra functions....\n\n"); // create a random nxn matrix for later use matrix_t A = create_random_matrix(DIM,DIM); printf("New Random Matrix A:\n"); print_matrix(A); // also create random vector vector_t b = create_random_vector(DIM); printf("\nNew Random Vector b:\n"); print_vector(b); // do an LUP decomposition on A matrix_t L,U,P; LUP_decomposition(A,&L,&U,&P); printf("\nL:\n"); print_matrix(L); printf("U:\n"); print_matrix(U); printf("P:\n"); print_matrix(P); // do a QR decomposition on A matrix_t Q,R; QR_decomposition(A,&Q,&R); printf("\nQR Decomposition of A\n"); printf("Q:\n"); print_matrix(Q); printf("R:\n"); print_matrix(R); // get determinant of A float det = matrix_determinant(A); printf("\nDeterminant of A : %8.4f\n", det); // get an inverse for A matrix_t Ainv = invert_matrix(A); if(A.initialized != 1) return -1; printf("\nAinverse\n"); print_matrix(Ainv); // multiply A times A inverse matrix_t AA = multiply_matrices(A,Ainv); if(AA.initialized!=1) return -1; printf("\nA * Ainverse:\n"); print_matrix(AA); // solve a square linear system vector_t x = lin_system_solve(A, b); printf("\nGaussian Elimination solution x to the equation Ax=b:\n"); print_vector(x); // now do again but with qr decomposition method vector_t xqr = lin_system_solve_qr(A, b); printf("\nQR solution x to the equation Ax=b:\n"); print_vector(xqr); // If b are the coefficients of a polynomial, get the coefficients of the // new polynomial b^2 vector_t bb = poly_power(b,2); printf("\nCoefficients of polynomial b times itself\n"); print_vector(bb); // clean up all the allocated memory. This isn't strictly necessary since // we are already at the end of the program, but good practice to do. destroy_matrix(&A); destroy_matrix(&AA); destroy_vector(&b); destroy_vector(&bb); destroy_vector(&x); destroy_vector(&xqr); destroy_matrix(&Q); destroy_matrix(&R); printf("DONE\n"); return 0; }