LU decomposition of a square matrix
More...
#include <cassert>
#include <ctime>
#include <iomanip>
#include <iostream>
#include "./lu_decomposition.h"
Go to the source code of this file.
|
template<typename T > |
std::ostream & | operator<< (std::ostream &out, matrix< T > const &v) |
|
void | test1 () |
|
void | test2 () |
|
int | main (int argc, char **argv) |
|
LU decomposition of a square matrix
- Author
- Krishna Vedala
Definition in file lu_decompose.cpp.
◆ main()
int main |
( |
int | argc, |
|
|
char ** | argv ) |
Main function
Definition at line 84 of file lu_decompose.cpp.
84 {
85 std::srand(std::time(NULL));
86
89 return 0;
90}
◆ operator<<()
template<typename T >
std::ostream & operator<< |
( |
std::ostream & | out, |
|
|
matrix< T > const & | v ) |
operator to print a matrix
Definition at line 18 of file lu_decompose.cpp.
18 {
19 const int width = 10;
20 const char separator = ' ';
21
22 for (size_t row = 0; row < v.size(); row++) {
23 for (size_t col = 0; col < v[row].size(); col++)
24 out << std::left << std::setw(width) << std::setfill(separator)
25 << v[row][col];
26 out << std::endl;
27 }
28
29 return out;
30}
◆ test1()
Test LU decomposition
- Todo
- better ways to self-check a matrix output?
Definition at line 36 of file lu_decompose.cpp.
36 {
38 const int range = 50;
39 const int range2 = range >> 1;
40
41
46
48
49 A[i][j] = static_cast<double>(std::rand() % range - range2);
50 }
51
52 std::clock_t start_t = std::clock();
54 std::clock_t end_t = std::clock();
55 std::cout << "Time taken: "
56 << static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC << "\n";
57
58 std::cout << "A = \n" << A << "\n";
59 std::cout << "L = \n" << L << "\n";
60 std::cout << "U = \n" << U << "\n";
61}
int lu_decomposition(const matrix< T > &A, matrix< double > *L, matrix< double > *U)
std::vector< std::valarray< T > > matrix
◆ test2()
Test determinant computation using LU decomposition
Definition at line 66 of file lu_decompose.cpp.
66 {
67 std::cout << "Determinant test 1...";
70 std::cout << "passed\n";
71
72 std::cout << "Determinant test 2...";
75 std::cout << "passed\n";
76
77 std::cout << "Determinant test 3...";
78 matrix<float> A3({{1.2, 2.3, 3.4}, {4.5, 5.6, 6.7}, {7.8, 8.9, 9.0}});
80 std::cout << "passed\n";
81}
double determinant_lu(const matrix< T > &A)