TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
qr_decomposition.cpp File Reference

Program to compute the QR decomposition of a given matrix. More...

#include <array>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "./qr_decompose.h"
Include dependency graph for qr_decomposition.cpp:

Go to the source code of this file.

Functions

int main (void)
 

Detailed Description

Program to compute the QR decomposition of a given matrix.

Author
Krishna Vedala

Definition in file qr_decomposition.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Definition at line 23 of file qr_decomposition.cpp.

23 {
24 unsigned int ROWS, COLUMNS;
25
26 std::cout << "Enter the number of rows and columns: ";
27 std::cin >> ROWS >> COLUMNS;
28
29 std::cout << "Enter matrix elements row-wise:\n";
30
31 std::valarray<std::valarray<double>> A(ROWS);
32 std::valarray<std::valarray<double>> Q(ROWS);
33 std::valarray<std::valarray<double>> R(COLUMNS);
34 for (int i = 0; i < std::max(ROWS, COLUMNS); i++) {
35 if (i < ROWS) {
36 A[i] = std::valarray<double>(COLUMNS);
37 Q[i] = std::valarray<double>(COLUMNS);
38 }
39 if (i < COLUMNS) {
40 R[i] = std::valarray<double>(COLUMNS);
41 }
42 }
43
44 for (int i = 0; i < ROWS; i++)
45 for (int j = 0; j < COLUMNS; j++) std::cin >> A[i][j];
46
47 std::cout << A << "\n";
48
49 clock_t t1 = clock();
50 qr_decompose(A, &Q, &R);
51 double dtime = static_cast<double>(clock() - t1) / CLOCKS_PER_SEC;
52
53 std::cout << Q << "\n";
54 std::cout << R << "\n";
55 std::cout << "Time taken to compute: " << dtime << " sec\n ";
56
57 return 0;
58}