mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-06-18 01:36:52 +08:00
added QR decomposition algorithm
Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
58
numerical_methods/qr_decomposition.cpp
Normal file
58
numerical_methods/qr_decomposition.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @file
|
||||
* \brief Program to compute the [QR
|
||||
* decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
|
||||
* matrix.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
|
||||
#include "./qr_decompose.h"
|
||||
|
||||
using qr_algorithm::qr_decompose;
|
||||
using qr_algorithm::operator<<;
|
||||
|
||||
/**
|
||||
* main function
|
||||
*/
|
||||
int main(void) {
|
||||
unsigned int ROWS, COLUMNS;
|
||||
|
||||
std::cout << "Enter the number of rows and columns: ";
|
||||
std::cin >> ROWS >> COLUMNS;
|
||||
|
||||
std::cout << "Enter matrix elements row-wise:\n";
|
||||
|
||||
std::valarray<std::valarray<double>> A(ROWS);
|
||||
std::valarray<std::valarray<double>> Q(ROWS);
|
||||
std::valarray<std::valarray<double>> R(COLUMNS);
|
||||
for (int i = 0; i < std::max(ROWS, COLUMNS); i++) {
|
||||
if (i < ROWS) {
|
||||
A[i] = std::valarray<double>(COLUMNS);
|
||||
Q[i] = std::valarray<double>(COLUMNS);
|
||||
}
|
||||
if (i < COLUMNS) {
|
||||
R[i] = std::valarray<double>(COLUMNS);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < ROWS; i++)
|
||||
for (int j = 0; j < COLUMNS; j++) std::cin >> A[i][j];
|
||||
|
||||
std::cout << A << "\n";
|
||||
|
||||
clock_t t1 = clock();
|
||||
qr_decompose(A, &Q, &R);
|
||||
double dtime = static_cast<double>(clock() - t1) / CLOCKS_PER_SEC;
|
||||
|
||||
std::cout << Q << "\n";
|
||||
std::cout << R << "\n";
|
||||
std::cout << "Time taken to compute: " << dtime << " sec\n ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user