Skip to content

Commit 35a20fc

Browse files
committedSep 26, 2024
Move tests to seperate repo.
0 parents  commit 35a20fc

6 files changed

+196
-0
lines changed
 

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "linear_algebra"]
2+
path = linear_algebra
3+
url = https://github.com/water-chika/linear_algebra

‎CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(linear_algebra_test)
4+
5+
add_subdirectory(linear_algebra)
6+
7+
add_executable(linear_algebra_test
8+
linear_algebra_test.cpp
9+
matrix_test.hpp
10+
vector_test.hpp
11+
)
12+
13+
set_target_properties(linear_algebra_test PROPERTIES CXX_STANDARD 23)
14+
target_link_libraries(linear_algebra_test linear_algebra)

‎linear_algebra

Submodule linear_algebra added at fa89438

‎linear_algebra_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "matrix_test.hpp"
2+
#include "vector_test.hpp"
3+
4+
int main() {
5+
std::cout << "test gram schmidt: " << (test_gram_schmidt()?"PASSED":"FAILED") << std::endl;
6+
test_vector();
7+
return 0;
8+
}

‎matrix_test.hpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
};

‎vector_test.hpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
#include <vector.hpp>
3+
#include <fixsized_vector.hpp>
4+
#include <vector>
5+
#include <functional>
6+
7+
bool test0() {
8+
linear_algebra::fixsized_vector<float, 2> a{1.0f, 0.0f};
9+
a *= 2.0f;
10+
return a == linear_algebra::fixsized_vector<float, 2>{2.0f, 0.0f};
11+
}
12+
13+
bool test1() {
14+
linear_algebra::fixsized_vector<double, 3> a{ 0.5, 0.5, 0.5 };
15+
return 0.75 == dot_product(a, a);
16+
}
17+
18+
bool test_add() {
19+
linear_algebra::fixsized_vector<double, 2> a{ 0.5, -0.5 };
20+
linear_algebra::fixsized_vector<double, 2> b{ -1.0, 1.5 };
21+
return a + b == linear_algebra::fixsized_vector<double, 2>{-0.5, 1.0};
22+
}
23+
bool test_sub() {
24+
linear_algebra::fixsized_vector<double, 2> a{ 0.5, -0.5 };
25+
linear_algebra::fixsized_vector<double, 2> b{ -1.0, 1.5 };
26+
return a - b == linear_algebra::fixsized_vector<double, 2>{1.5, -2.0};
27+
}
28+
bool test_element_multi() {
29+
linear_algebra::fixsized_vector<double, 2> a{ 0.5, -0.5 };
30+
linear_algebra::fixsized_vector<double, 2> b{ -1.0, 1.5 };
31+
return element_multi(a, b) == linear_algebra::fixsized_vector<double, 2>{-0.5, -0.5*1.5};
32+
}
33+
bool test_ranged_for() {
34+
linear_algebra::fixsized_vector<double, 128> a{};
35+
for (auto& e : a) {
36+
e = 1.0;
37+
}
38+
bool success = true;
39+
for (auto e : a) {
40+
success = success && e == 1.0;
41+
}
42+
return success;
43+
}
44+
bool test_negative() {
45+
linear_algebra::fixsized_vector<double, 2> a{1.0, -1.0};
46+
return -a == linear_algebra::fixsized_vector<double, 2>{-1.0, 1.0};
47+
}
48+
49+
bool test_vector() {
50+
std::vector<std::function<bool()>> tests{
51+
test0,
52+
test1,
53+
test_add,
54+
test_sub,
55+
test_element_multi,
56+
test_ranged_for,
57+
test_negative,
58+
};
59+
for (auto& test : tests) {
60+
if (test()) {
61+
std::cout << "passed" << std::endl;
62+
}
63+
else {
64+
std::cout << "failed" << std::endl;
65+
}
66+
}
67+
return true;
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.