-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_linalg.cpp
221 lines (182 loc) · 6.35 KB
/
test_linalg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
MIT License
Copyright (c) 2024 xingxing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "linalg.hpp"
#include <iostream>
#include <cassert>
#include <cmath>
// 用于浮点数比较的辅助函数
template<typename T>
bool is_close(const T& a, const T& b, T eps = 1e-10) {
return std::abs(a - b) < eps;
}
// 用于矩阵比较的辅助函数
template<typename T>
bool matrix_is_close(const linalg::Matrix<T>& a, const linalg::Matrix<T>& b, T eps = 1e-10) {
if (a.get_rows() != b.get_rows() || a.get_cols() != b.get_cols()) {
return false;
}
for (size_t i = 0; i < a.get_rows(); ++i) {
for (size_t j = 0; j < a.get_cols(); ++j) {
if (!is_close(a(i, j), b(i, j), eps)) {
return false;
}
}
}
return true;
}
void test_vector_operations() {
std::cout << "测试向量运算..." << std::endl;
// 构造函数测试
linalg::Vector<double> v1 = {1.0, 2.0, 3.0};
linalg::Vector<double> v2 = {4.0, 5.0, 6.0};
// 加法测试
auto v3 = v1 + v2;
assert(is_close(v3[0], 5.0));
assert(is_close(v3[1], 7.0));
assert(is_close(v3[2], 9.0));
// 减法测试
auto v4 = v2 - v1;
assert(is_close(v4[0], 3.0));
assert(is_close(v4[1], 3.0));
assert(is_close(v4[2], 3.0));
// 范数测试
double norm = v1.norm();
assert(is_close(norm, std::sqrt(14.0))); // sqrt(1^2 + 2^2 + 3^2)
// 打印测试
std::cout << "向量v1:\n" << v1 << std::endl;
std::cout << "向量v2:\n" << v2 << std::endl;
std::cout << "向量运算测试通过!" << std::endl;
}
void test_matrix_operations() {
std::cout << "测试矩阵运算..." << std::endl;
// 构造函数测试
linalg::Matrix<double> m1 = {
{1.0, 2.0},
{3.0, 4.0}
};
linalg::Matrix<double> m2 = {
{5.0, 6.0},
{7.0, 8.0}
};
// 加法测试
auto m3 = m1 + m2;
assert(is_close(m3(0, 0), 6.0));
assert(is_close(m3(0, 1), 8.0));
assert(is_close(m3(1, 0), 10.0));
assert(is_close(m3(1, 1), 12.0));
// 减法测试
auto m4 = m2 - m1;
assert(is_close(m4(0, 0), 4.0));
assert(is_close(m4(0, 1), 4.0));
assert(is_close(m4(1, 0), 4.0));
assert(is_close(m4(1, 1), 4.0));
// 矩阵乘法测试
auto m5 = m1 * m2;
assert(is_close(m5(0, 0), 19.0)); // 1*5 + 2*7 = 19
assert(is_close(m5(0, 1), 22.0)); // 1*6 + 2*8 = 22
assert(is_close(m5(1, 0), 43.0)); // 3*5 + 4*7 = 43
assert(is_close(m5(1, 1), 50.0)); // 3*6 + 4*8 = 50
// 转置测试
auto m6 = m1.transpose();
assert(is_close(m6(0, 0), 1.0));
assert(is_close(m6(0, 1), 3.0));
assert(is_close(m6(1, 0), 2.0));
assert(is_close(m6(1, 1), 4.0));
// 打印测试
std::cout << "矩阵m1:\n" << m1 << std::endl;
std::cout << "矩阵m2:\n" << m2 << std::endl;
std::cout << "矩阵运算测试通过!" << std::endl;
}
void test_matrix_advanced() {
std::cout << "测试高级矩阵运算..." << std::endl;
// 测试矩阵求逆
linalg::Matrix<double> m = {
{4.0, 7.0},
{2.0, 6.0}
};
auto inv = m.inverse();
std::cout << "原矩阵:\n" << m << std::endl;
std::cout << "逆矩阵:\n" << inv << std::endl;
// 验证求逆结果:m * m^(-1) 应该等于单位矩阵
auto identity = m * inv;
assert(is_close(identity(0, 0), 1.0));
assert(is_close(identity(0, 1), 0.0));
assert(is_close(identity(1, 0), 0.0));
assert(is_close(identity(1, 1), 1.0));
// 测试行列式
double det = m.determinant();
assert(is_close(det, 10.0)); // 4*6 - 7*2 = 24 - 14 = 10
// 测试最大值和绝对值
linalg::Matrix<double> test_mat = {
{-1.0, 2.0},
{-3.0, 4.0}
};
auto max_vals = test_mat.max();
auto abs_mat = test_mat.abs();
std::cout << "测试矩阵:\n" << test_mat << std::endl;
std::cout << "每列最大值:\n" << max_vals << std::endl;
std::cout << "绝对值矩阵:\n" << abs_mat << std::endl;
std::cout << "高级矩阵运算测试通过!" << std::endl;
}
void test_error_handling() {
std::cout << "测试错误处理..." << std::endl;
try {
// 测试不匹配维度的矩阵加法
linalg::Matrix<double> m1(2, 2);
linalg::Matrix<double> m2(2, 3);
auto m3 = m1 + m2;
assert(false); // 不应该到达这里
} catch (const std::runtime_error&) {
// 预期的异常
}
try {
// 测试非方阵求逆
linalg::Matrix<double> m(2, 3);
auto inv = m.inverse();
assert(false); // 不应该到达这里
} catch (const std::runtime_error&) {
// 预期的异常
}
try {
// 测试不可逆矩阵
linalg::Matrix<double> m = {
{1.0, 2.0},
{2.0, 4.0}
};
auto inv = m.inverse();
assert(false); // 不应该到达这里
} catch (const std::runtime_error&) {
// 预期的异常
}
std::cout << "错误处理测试通过!" << std::endl;
}
int main() {
try {
test_vector_operations();
test_matrix_operations();
test_matrix_advanced();
test_error_handling();
std::cout << "\n所有测试通过!" << std::endl;
} catch (const std::exception& e) {
std::cerr << "测试失败:" << e.what() << std::endl;
return 1;
}
return 0;
}