-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparseMv.cpp
340 lines (259 loc) · 13.2 KB
/
sparseMv.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "sparseMv.h"
#include <iostream>
#include <fstream> // ifstream
#include <fast_matrix_market/app/Eigen.hpp>
// -----------------------------------------------------------------------
// Na5
Eigen::SparseMatrix<double> sparseA_Na5;
void init_SparseA_Na5(){
std::ifstream f("../../../Na5.mtx");
if(!f.is_open()) {std::cerr << "failed to open file" << std::endl; return;}
std::cout <<"building sparseA_Na5, ";
fast_matrix_market::read_matrix_market_eigen(f, sparseA_Na5);
std::cout <<"size = " << sparseA_Na5.rows() << ", " << sparseA_Na5.cols() << std::endl;
std::cout << "sparseA_Na5 size = " << sparseA_Na5.rows() << ", " << sparseA_Na5.cols() << std::endl;
}
void sparse_avec_Na5(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& avecs){
if(sparseA_Na5.rows() == 0) init_SparseA_Na5();
if(sparseA_Na5.rows() != n || sparseA_Na5.cols() != n){std::cerr << "input sparse matrix must be of size (n,n) = "<<"("<<n<<", "<<n<<")"; return;}
if(vecs.rows() != n || vecs.cols() != m){std::cerr << "vecs must be of size (n,m)"; return;}
if(avecs.rows() != n || avecs.cols() != m){std::cerr << "avecs must be of size (n,m)"; return;}
avecs = sparseA_Na5 * vecs;
}
void sparse_precnd_Na5(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(sparseA_Na5.rows() == 0) init_SparseA_Na5();
for (int icol = 0; icol < m; ++icol) {
for (int i = 0; i < n; ++i) {
// 检查分母是否为0,避免除以0的错误
// if (abs(a(i, i) + fac) > 1.0e-5) {
if (abs(sparseA_Na5.coeff(i, i)+shift) > 1.0e-5) {
tvecs(i, icol) = vecs(i, icol) / (sparseA_Na5.coeff(i, i)+shift);
}
}
}
}
Eigen::SparseMatrix<double> tridiagA_Na5;
void init_tridiagA_Na5(double shift){
if(sparseA_Na5.rows() == 0) init_SparseA_Na5();
int n = sparseA_Na5.rows();
tridiagA_Na5.resize(n, n);
tridiagA_Na5.reserve(3*n); // reserve space for tridiagonal elements
for(int i = 0; i < n; i++){
tridiagA_Na5.insert(i, i) = sparseA_Na5.coeff(i, i)+shift;
if(i > 0)
tridiagA_Na5.insert(i, i-1) = sparseA_Na5.coeff(i, i-1);
if(i < n-1)
tridiagA_Na5.insert(i, i+1) = sparseA_Na5.coeff(i, i+1);
}
tridiagA_Na5.makeCompressed();
}
void tridiagA_precnd_Na5(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(tridiagA_Na5.rows() == 0) init_tridiagA_Na5(shift);
for(int i = 0; i < n; i++){
tridiagA_Na5.coeffRef(i, i) = sparseA_Na5.coeff(i, i)+shift;
}
// solve tridiagA_Na5 * tvecs = vecs // 计算LU分解
Eigen::SparseLU<Eigen::SparseMatrix<double>> solver;
solver.analyzePattern(tridiagA_Na5);
solver.factorize(tridiagA_Na5);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Decomposition failed when doing tridiagonal precnd" << std::endl;}
// 使用LU分解来解线性系统
tvecs = solver.solve(vecs);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Solving failed when doing tridiagonal precnd" << std::endl;}
// 输出解矩阵
// std::cout << "The solution matrix tvecs is:\n" << tvecs << std::endl;
}
// -----------------------------------------------------------------------
// Si2
Eigen::SparseMatrix<double> sparseA_Si2;
void init_SparseA_Si2(){
std::ifstream f("../../../Si2.mtx");
if(!f.is_open()) {std::cerr << "failed to open file" << std::endl; return;}
std::cout <<"building sparseA_Si2, ";
fast_matrix_market::read_matrix_market_eigen(f, sparseA_Si2);
std::cout <<"size = " << sparseA_Si2.rows() << ", " << sparseA_Si2.cols() << std::endl;
std::cout << "sparseA_Si2 size = " << sparseA_Si2.rows() << ", " << sparseA_Si2.cols() << std::endl;
}
void sparse_avec_Si2(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& avecs){
if(sparseA_Si2.rows() == 0) init_SparseA_Si2();
if(sparseA_Si2.rows() != n || sparseA_Si2.cols() != n){std::cerr << "input sparse matrix must be of size (n,n) = "<<"("<<n<<", "<<n<<")"; return;}
if(vecs.rows() != n || vecs.cols() != m){std::cerr << "vecs must be of size (n,m)"; return;}
if(avecs.rows() != n || avecs.cols() != m){std::cerr << "avecs must be of size (n,m)"; return;}
avecs = sparseA_Si2 * vecs;
}
void sparse_precnd_Si2(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(sparseA_Si2.rows() == 0) init_SparseA_Si2();
for (int icol = 0; icol < m; ++icol) {
for (int i = 0; i < n; ++i) {
// 检查分母是否为0,避免除以0的错误
// if (abs(a(i, i) + fac) > 1.0e-5) {
if (abs(sparseA_Si2.coeff(i, i)+shift) > 1.0e-5) {
tvecs(i, icol) = vecs(i, icol) / (sparseA_Si2.coeff(i, i)+shift);
}
}
}
}
Eigen::SparseMatrix<double> tridiagA_Si2;
void init_tridiagA_Si2(double shift){
if(sparseA_Si2.rows() == 0) init_SparseA_Si2();
int n = sparseA_Si2.rows();
tridiagA_Si2.resize(n, n);
tridiagA_Si2.reserve(3*n); // reserve space for tridiagonal elements
for(int i = 0; i < n; i++){
tridiagA_Si2.insert(i, i) = sparseA_Si2.coeff(i, i)+shift;
if(i > 0)
tridiagA_Si2.insert(i, i-1) = sparseA_Si2.coeff(i, i-1);
if(i < n-1)
tridiagA_Si2.insert(i, i+1) = sparseA_Si2.coeff(i, i+1);
}
tridiagA_Si2.makeCompressed();
}
void tridiagA_precnd_Si2(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(tridiagA_Si2.rows() == 0) init_tridiagA_Si2(shift);
for(int i = 0; i < n; i++){
tridiagA_Si2.coeffRef(i, i) = sparseA_Si2.coeff(i, i)+shift;
}
// solve tridiagA_Si2 * tvecs = vecs // 计算LU分解
Eigen::SparseLU<Eigen::SparseMatrix<double>> solver;
solver.analyzePattern(tridiagA_Si2);
solver.factorize(tridiagA_Si2);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Decomposition failed when doing tridiagonal precnd" << std::endl;}
// 使用LU分解来解线性系统
tvecs = solver.solve(vecs);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Solving failed when doing tridiagonal precnd" << std::endl;}
// 输出解矩阵
// std::cout << "The solution matrix tvecs is:\n" << tvecs << std::endl;
}
// Eigen::SparseMatrix<double> sparseA_Si5H12;
// void init_SparseA_Si2(){
// std::ifstream f("../../../Si2.mtx");
// if(!f.is_open()) {std::cerr << "failed to open file" << std::endl; return;}
// std::cout <<"building sparseA_Si2, ";
// fast_matrix_market::read_matrix_market_eigen(f, sparseA_Si2);
// std::cout <<"size = " << sparseA_Si2.rows() << ", " << sparseA_Si2.cols() << std::endl;
// std::cout << "sparseA_Si2 size = " << sparseA_Si2.rows() << ", " << sparseA_Si2.cols() << std::endl;
// }
// void sparse_avec_Si5H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& avecs);
// void sparse_precnd_Si5H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift);
Eigen::SparseMatrix<double> sparseA_Si5H12;
void init_SparseA_Si5H12(){
std::ifstream f("../../../Si5H12.mtx");
if(!f.is_open()) {std::cerr << "failed to open file" << std::endl; return;}
std::cout <<"building sparseA_Si5H12, ";
fast_matrix_market::read_matrix_market_eigen(f, sparseA_Si5H12);
std::cout <<"size = " << sparseA_Si5H12.rows() << ", " << sparseA_Si5H12.cols() << std::endl;
std::cout << "sparseA_Si5H12 size = " << sparseA_Si5H12.rows() << ", " << sparseA_Si5H12.cols() << std::endl;
}
void sparse_avec_Si5H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& avecs){
if(sparseA_Si5H12.rows() == 0) init_SparseA_Si5H12();
if(sparseA_Si5H12.rows() != n || sparseA_Si5H12.cols() != n){std::cerr << "input sparse matrix must be of size (n,n) = "<<"("<<n<<", "<<n<<")"; return;}
if(vecs.rows() != n || vecs.cols() != m){std::cerr << "vecs must be of size (n,m)"; return;}
if(avecs.rows() != n || avecs.cols() != m){std::cerr << "avecs must be of size (n,m)"; return;}
avecs = sparseA_Si5H12 * vecs;
}
void sparse_precnd_Si5H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(sparseA_Si5H12.rows() == 0) init_SparseA_Si5H12();
for (int icol = 0; icol < m; ++icol) {
for (int i = 0; i < n; ++i) {
if (abs(sparseA_Si5H12.coeff(i, i)+shift) > 1.0e-5) {
tvecs(i, icol) = vecs(i, icol) / (sparseA_Si5H12.coeff(i, i)+shift);
}
}
}
}
Eigen::SparseMatrix<double> tridiagA_Si5H12;
void init_tridiagA_Si5H12(double shift){
if(sparseA_Si5H12.rows() == 0) init_SparseA_Si5H12();
int n = sparseA_Si5H12.rows();
tridiagA_Si5H12.resize(n, n);
tridiagA_Si5H12.reserve(3*n); // reserve space for tridiagonal elements
for(int i = 0; i < n; i++){
tridiagA_Si5H12.insert(i, i) = sparseA_Si5H12.coeff(i, i)+shift;
if(i > 0)
tridiagA_Si5H12.insert(i, i-1) = sparseA_Si5H12.coeff(i, i-1);
if(i < n-1)
tridiagA_Si5H12.insert(i, i+1) = sparseA_Si5H12.coeff(i, i+1);
}
tridiagA_Si5H12.makeCompressed();
}
void tridiagA_precnd_Si5H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(tridiagA_Si5H12.rows() == 0) init_tridiagA_Si5H12(shift);
for(int i = 0; i < n; i++){
tridiagA_Si5H12.coeffRef(i, i) = sparseA_Si5H12.coeff(i, i)+shift;
}
// solve tridiagA_Si5H12 * tvecs = vecs // 计算LU分解
Eigen::SparseLU<Eigen::SparseMatrix<double>> solver;
solver.analyzePattern(tridiagA_Si5H12);
solver.factorize(tridiagA_Si5H12);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Decomposition failed when doing tridiagonal precnd" << std::endl;}
// 使用LU分解来解线性系统
tvecs = solver.solve(vecs);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Solving failed when doing tridiagonal precnd" << std::endl;}
// 输出解矩阵
// std::cout << "The solution matrix tvecs is:\n" << tvecs << std::endl;
}
// Ga3As3H12
Eigen::SparseMatrix<double> sparseA_Ga3As3H12;
void init_SparseA_Ga3As3H12(){
std::ifstream f("../../../Ga3As3H12.mtx");
if(!f.is_open()) {std::cerr << "failed to open file" << std::endl; return;}
std::cout <<"building sparseA_Ga3As3H12, ";
fast_matrix_market::read_matrix_market_eigen(f, sparseA_Ga3As3H12);
std::cout <<"size = " << sparseA_Ga3As3H12.rows() << ", " << sparseA_Ga3As3H12.cols() << std::endl;
std::cout << "sparseA_Ga3As3H12 size = " << sparseA_Ga3As3H12.rows() << ", " << sparseA_Ga3As3H12.cols() << std::endl;
}
void sparse_avec_Ga3As3H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& avecs){
if(sparseA_Ga3As3H12.rows() == 0) init_SparseA_Ga3As3H12();
if(sparseA_Ga3As3H12.rows() != n || sparseA_Ga3As3H12.cols() != n){std::cerr << "input sparse matrix must be of size (n,n) = "<<"("<<n<<", "<<n<<")"; return;}
if(vecs.rows() != n || vecs.cols() != m){std::cerr << "vecs must be of size (n,m)"; return;}
if(avecs.rows() != n || avecs.cols() != m){std::cerr << "avecs must be of size (n,m)"; return;}
avecs = sparseA_Ga3As3H12 * vecs;
}
void sparse_precnd_Ga3As3H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(sparseA_Ga3As3H12.rows() == 0) init_SparseA_Ga3As3H12();
for (int icol = 0; icol < m; ++icol) {
for (int i = 0; i < n; ++i) {
if (abs(sparseA_Ga3As3H12.coeff(i, i)+shift) > 1.0e-5) {
tvecs(i, icol) = vecs(i, icol) / (sparseA_Ga3As3H12.coeff(i, i)+shift);
}
}
}
}
Eigen::SparseMatrix<double> tridiagA_Ga3As3H12;
void init_tridiagA_Ga3As3H12(double shift){
if(sparseA_Ga3As3H12.rows() == 0) init_SparseA_Ga3As3H12();
int n = sparseA_Ga3As3H12.rows();
tridiagA_Ga3As3H12.resize(n, n);
tridiagA_Ga3As3H12.reserve(3*n); // reserve space for tridiagonal elements
for(int i = 0; i < n; i++){
tridiagA_Ga3As3H12.insert(i, i) = sparseA_Ga3As3H12.coeff(i, i)+shift;
if(i > 0)
tridiagA_Ga3As3H12.insert(i, i-1) = sparseA_Ga3As3H12.coeff(i, i-1);
if(i < n-1)
tridiagA_Ga3As3H12.insert(i, i+1) = sparseA_Ga3As3H12.coeff(i, i+1);
}
tridiagA_Ga3As3H12.makeCompressed();
}
void tridiagA_precnd_Ga3As3H12(int n, int m, const Eigen::MatrixXd& vecs, Eigen::MatrixXd& tvecs, double shift){
if(tridiagA_Ga3As3H12.rows() == 0) init_tridiagA_Ga3As3H12(shift);
for(int i = 0; i < n; i++){
tridiagA_Ga3As3H12.coeffRef(i, i) = sparseA_Ga3As3H12.coeff(i, i)+shift;
}
// solve tridiagA_Ga3As3H12 * tvecs = vecs // 计算LU分解
Eigen::SparseLU<Eigen::SparseMatrix<double>> solver;
solver.analyzePattern(tridiagA_Ga3As3H12);
solver.factorize(tridiagA_Ga3As3H12);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Decomposition failed when doing tridiagonal precnd" << std::endl;}
// 使用LU分解来解线性系统
tvecs = solver.solve(vecs);
if (solver.info() != Eigen::Success) {std::cerr << "SparseLU Solving failed when doing tridiagonal precnd" << std::endl;}
// 输出解矩阵
// std::cout << "The solution matrix tvecs is:\n" << tvecs << std::endl;
}
// init all
// void init_all_sparseA(){
// init_SparseA_Na5();
// init_SparseA_Si2();
// init_SparseA_Si5H12();
// init_SparseA_Ga3As3H12();
// }