1
+ #pragma once
2
+
3
+ #include < matrix.hpp>
4
+ #include < vector.hpp>
5
+ #include < fixsized_matrix.hpp>
6
+
7
+ #include " matrix.hpp"
8
+ #include " combined_reference_matrix.hpp"
9
+ #include < iostream>
10
+ #include < concepts>
11
+
12
+ bool test_gram_schmidt () {
13
+ auto A = linear_algebra::matrix<float , 3 , 3 >{
14
+ {1 , 2 , 3 },
15
+ {-1 , 0 , -3 },
16
+ {0 , -2 , 3 }
17
+ };
18
+ auto B = linear_algebra::matrix<float , 3 , 3 >{
19
+ { 1 , 1 , 1 ,},
20
+ { -1 , 1 , 1 ,},
21
+ { 0 , -2 , 1 ,},
22
+ };
23
+ return B == gram_schmidt (A);
24
+ }
25
+
26
+ namespace water {
27
+ namespace concept_helper {
28
+ template <class T >
29
+ concept number = std::integral<T> || std::floating_point<T>;
30
+ }
31
+ }
32
+
33
+ class linear_algebra_test {
34
+ public:
35
+ template <water::concept_helper::number Number>
36
+ class set_number {
37
+ public:
38
+ template <std::integral Integral>
39
+ class set_index {
40
+ public:
41
+ static void run () {
42
+ linear_algebra::fixsized_vector<Number, 2 > v{ 4 .0f , 1 .0f }, u{ 2 .0f , -1 .0f };
43
+ auto combination = Number{ 2.0 } *v + Number{ -3 .0f }*u;
44
+ std::cout << combination << std::endl;
45
+ std::cout << dot_product (Number{ 0 .5f } *v * Number{ 2 .0f }, Number{ 2 .0f } *u * Number{ 0 .5f }) << std::endl;
46
+ linear_algebra::matrix<Number, 2 , 2 > A{ linear_algebra::column_vector{v}, linear_algebra::column_vector{u} };
47
+ std::cout << " A is below: " << A << std::endl;
48
+ std::cout << A.column (0 ) << std::endl;
49
+ std::cout << A.column (1 ) << std::endl;
50
+ std::cout << A.row (0 ) << std::endl;
51
+ std::cout << A.row (1 ) << std::endl;
52
+ A.row (1 ) -= A.row (0 ) * 2 ;
53
+ std::cout << " After subtract 2 * row 0 from row 1, A is below: " << A << std::endl;
54
+
55
+ auto U = linear_algebra::eliminate (A);
56
+ std::cout << " The elimination U of A is below: " << U << std::endl;
57
+
58
+ {
59
+ using namespace linear_algebra ;
60
+ auto A_I = make_matrix_with_columns<double , 3 , 6 >({fixsized_vector<double , 3 >{2 ,1 ,1 }, fixsized_vector<double , 3 >{1 ,2 ,1 }, fixsized_vector<double , 3 >{1 ,1 ,2 },
61
+ fixsized_vector<double , 3 >{1 ,0 ,0 }, fixsized_vector<double , 3 >{0 ,1 ,0 }, fixsized_vector<double , 3 >{0 ,0 ,1 }});
62
+ std::cout << " set A_I = " << A_I << std::endl;
63
+ auto res = eliminate (A_I);
64
+ std::cout << " after eliminate, it =" << res << std::endl;
65
+ res = back_substitution (res);
66
+ std::cout << " after back substitution, it =" << res << std::endl;
67
+ }
68
+ {
69
+ auto A = linear_algebra::matrix<int , 3 , 3 >{
70
+ {1 , 0 , 1 },
71
+ {2 , 3 , 0 },
72
+ {5 , 4 , 7 }
73
+ };
74
+ std::cout << " test initializer, A = " << A << std::endl;
75
+ }
76
+ try {
77
+ using namespace linear_algebra ;
78
+ std::cout << " This is chapter 2, exercise 31." << std::endl;
79
+ auto A = matrix<Number, 3 , 3 >{
80
+ {2 , 1 , 1 },
81
+ {1 , 2 , 1 },
82
+ {1 , 1 , 2 }
83
+ };
84
+ std::cout << " A = " << A << std::endl;
85
+ std::cout << " A inverse = " << inverse (A) << std::endl;
86
+ auto B = matrix<Number, 3 , 3 >{
87
+ {2 , -1 , -1 },
88
+ {-1 , 2 , -1 },
89
+ {-1 , -1 , 2 }
90
+ };
91
+ std::cout << " B = " << B << std::endl;
92
+ auto B_I = concatenate_columns (B, identity_matrix<Number, 3 >());
93
+ std::cout << " B inverse = " <<
94
+ select_columns<3 , 4 , 5 >(back_substitution (eliminate (B_I))) << std::endl;
95
+ }
96
+ catch (std::exception & e) {
97
+ std::cout << e.what () << std::endl;
98
+ }
99
+ }
100
+ };
101
+ };
102
+ };
0 commit comments