/** * @file * Program to compute the QR decomposition of a * given matrix. */ #include #include #include #include "qr_decompose.h" #include /** * main function */ int main(void) { double **A; unsigned int ROWS, COLUMNS; printf("Enter the number of rows and columns: "); scanf("%u %u", &ROWS, &COLUMNS); if (ROWS < COLUMNS) { fprintf(stderr, "Number of rows must be greater than or equal to number of columns.\n"); return -1; } printf("Enter matrix elements row-wise:\n"); A = (double **)malloc(ROWS * sizeof(double *)); for (int i = 0; i < ROWS; i++) A[i] = (double *)malloc(COLUMNS * sizeof(double)); for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) scanf("%lf", &A[i][j]); print_matrix(A, ROWS, COLUMNS); double **R = (double **)malloc(sizeof(double *) * ROWS); double **Q = (double **)malloc(sizeof(double *) * ROWS); if (!Q || !R) { perror("Unable to allocate memory for Q & R!"); return -1; } for (int i = 0; i < ROWS; i++) { R[i] = (double *)malloc(sizeof(double) * COLUMNS); Q[i] = (double *)malloc(sizeof(double) * ROWS); if (!Q[i] || !R[i]) { perror("Unable to allocate memory for Q & R."); return -1; } } function_timer *t1 = new_timer(); start_timer(t1); qr_decompose(A, Q, R, ROWS, COLUMNS); double dtime = end_timer_delete(t1); print_matrix(R, ROWS, COLUMNS); print_matrix(Q, ROWS, COLUMNS); printf("Time taken to compute: %.4g sec\n", dtime); for (int i = 0; i < ROWS; i++) { free(A[i]); free(R[i]); free(Q[i]); } free(A); free(R); free(Q); return 0; }