TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
lu_decompose.cpp
Go to the documentation of this file.
1
7
#include <cassert>
8
#include <ctime>
9
#include <iomanip>
10
#include <iostream>
11
12
#include "
./lu_decomposition.h
"
13
17
template
<
typename
T>
18
std::ostream &
operator<<
(std::ostream &out,
matrix<T>
const
&v) {
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
}
31
36
void
test1
() {
37
int
mat_size
= 3;
// default matrix size
38
const
int
range = 50;
39
const
int
range2 = range >> 1;
40
41
/* Create a square matrix with random values */
42
matrix<double>
A(
mat_size
, std::valarray<double>(
mat_size
));
43
matrix<double>
L(
mat_size
, std::valarray<double>(
mat_size
));
// output
44
matrix<double>
U(
mat_size
, std::valarray<double>(
mat_size
));
// output
45
for
(
int
i = 0; i <
mat_size
; i++) {
46
// calloc so that all valeus are '0' by default
47
for
(
int
j = 0; j <
mat_size
; j++)
48
/* create random values in the limits [-range2, range-1] */
49
A[i][j] =
static_cast<
double
>
(std::rand() % range - range2);
50
}
51
52
std::clock_t start_t = std::clock();
53
lu_decomposition
(A, &L, &U);
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
}
62
66
void
test2
() {
67
std::cout <<
"Determinant test 1..."
;
68
matrix<int>
A1({{1, 2, 3}, {4, 9, 6}, {7, 8, 9}});
69
assert(
determinant_lu
(A1) == -48);
70
std::cout <<
"passed\n"
;
71
72
std::cout <<
"Determinant test 2..."
;
73
matrix<int>
A2({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
74
assert(
determinant_lu
(A2) == 0);
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}});
79
assert(
determinant_lu
(A3) == 3.63);
80
std::cout <<
"passed\n"
;
81
}
82
84
int
main
() {
85
std::srand(std::time(NULL));
// random number initializer
86
87
test1
();
88
test2
();
89
return
0;
90
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
test2
void test2(const std::string &text)
Self test 2 - using 8x8 randomly generated key.
Definition
hill_cipher.cpp:505
test1
void test1(const std::string &text)
Self test 1 - using 3x3 randomly generated key.
Definition
hill_cipher.cpp:470
operator<<
static std::ostream & operator<<(std::ostream &out, matrix< T > const &v)
Definition
hill_cipher.cpp:55
lu_decomposition.h
Functions associated with LU Decomposition of a square matrix.
determinant_lu
double determinant_lu(const matrix< T > &A)
Definition
lu_decomposition.h:90
lu_decomposition
int lu_decomposition(const matrix< T > &A, matrix< double > *L, matrix< double > *U)
Definition
lu_decomposition.h:29
matrix
std::vector< std::valarray< T > > matrix
Definition
lu_decomposition.h:19
mat_size
ll mat_size
Definition
matrix_exponentiation.cpp:45
numerical_methods
lu_decompose.cpp
Generated by
1.18.0