Program to compute the QR decomposition of a given matrix.
More...
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "qr_decompose.h"
Program to compute the QR decomposition of a given matrix.
- Author
- Krishna Vedala
◆ main()
main function
21 unsigned int ROWS, COLUMNS;
23 printf(
"Enter the number of rows and columns: ");
24 scanf(
"%u %u", &ROWS, &COLUMNS);
28 "Number of rows must be greater than or equal to "
29 "number of columns.\n");
33 printf(
"Enter matrix elements row-wise:\n");
35 A = (
double **)
malloc(ROWS *
sizeof(
double *));
36 for (
int i = 0; i < ROWS; i++)
37 A[i] = (
double *)
malloc(COLUMNS *
sizeof(
double));
39 for (
int i = 0; i < ROWS; i++)
40 for (
int j = 0; j < COLUMNS; j++) scanf(
"%lf", &A[i][j]);
44 double **R = (
double **)
malloc(
sizeof(
double *) * ROWS);
45 double **Q = (
double **)
malloc(
sizeof(
double *) * ROWS);
48 perror(
"Unable to allocate memory for Q & R!");
51 for (
int i = 0; i < ROWS; i++)
53 R[i] = (
double *)
malloc(
sizeof(
double) * COLUMNS);
54 Q[i] = (
double *)
malloc(
sizeof(
double) * ROWS);
57 perror(
"Unable to allocate memory for Q & R.");
64 double dtime = (double)(clock() - t1) / CLOCKS_PER_SEC;
68 printf(
"Time taken to compute: %.4g sec\n", dtime);
70 for (
int i = 0; i < ROWS; i++)
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
void qr_decompose(double **A, double **Q, double **R, int M, int N)
Decompose matrix using Gram-Schmidt process.
Definition: qr_decompose.h:142
void print_matrix(double **A, int M, int N)
function to display matrix on stdout
Definition: qr_decompose.h:22