TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
qr_decomposition.cpp
Go to the documentation of this file.
1
9#include <array>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#include <iostream>
14
15#include "./qr_decompose.h"
16
18using qr_algorithm::operator<<;
19
23int main(void) {
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}
void qr_decompose(const std::valarray< std::valarray< T > > &A, std::valarray< std::valarray< T > > *Q, std::valarray< std::valarray< T > > *R)
Library functions to compute QR decomposition of a given matrix.
int main(void)