INLINE Vector<max_n> solve_using_qr( Int m, Int n, Matrix<max_m, max_n> a, Vector<max_m> b) { auto qr = factorize_qr_householder(m, n, a); auto qtb = implicit_q_trans_b(m, n, qr.v, b); auto x = solve_upper_triangular(n, qr.r, qtb); return x; }
int main(){ char *filename = "data_F.dat"; int n_col=2; int n_row = 884; float *file_data = load_matrix(filename, n_row); float *data = malloc(n_row*n_col*sizeof(float)); float *b = malloc(n_row*sizeof(float)); float *theta = malloc(n_row*sizeof(float)); make_data(file_data, data, b,theta, n_row); float *data_traspose = traspose(data,n_row,n_col); float *matrix = multiply(data_traspose,data, n_col,n_row,n_row,n_col); float *new_b = multiply(data_traspose,b, n_col, n_row, n_row, 1); float *U = malloc(n_col*n_col*sizeof(float)); float *L = malloc(n_col*n_col*sizeof(float)); lu_decomposition(matrix,new_b,U,L,n_col); float a1,a2; solve_upper_triangular(U,new_b,&a1,&a2); print_in_file(a1,a2); return 0; }
/* Solve from the qr decomposition itself. Useful if multiple equations with the same left hand side need to be solved. */ struct vector* linsolve_from_qr(struct qr_decomp* qr, struct vector* v) { struct vector* rhs = matrix_vector_multiply_Mtv(qr->q, v); struct vector* solution = solve_upper_triangular(qr->r, rhs); vector_free(rhs); return solution; }