-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.h
More file actions
240 lines (206 loc) · 8.59 KB
/
Solver.h
File metadata and controls
240 lines (206 loc) · 8.59 KB
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
#include "Matrix.h"
#include "CSRMatrix.h"
#include<vector>
/**
A solver class that has functions that correspond to various linear solver algorithms.
*/
using namespace std;
#ifndef GROUP_ASSIGNMENT_LSG_SOLVER_H
#define GROUP_ASSIGNMENT_LSG_SOLVER_H
class Solver {
public:
// --------------auxiliary function-----------------
/**
* @tparam U
* @param A The input square matrix on upper triangular form
* @param b The vector b from upper_triangle(Matrix<U> & A, U* b)
* @param output The output array to store the vector x in linear system Ax=b
*/
template <class U>
void back_substitution(Matrix<U>& A, U* b, U* output);
/**
* @tparam U
* @param A The input square matrix on upper triangular form
* @param b The vector b from upper_triangle(Matrix<U> & A, U* b)
* @param output The output array to store the vector x in linear system Ax=b
*/
template <class U>
void back_substitution_CSRMatrix(CSRMatrix<U>& A, U* b, U* output);
/**
* @tparam U
* @param A The input square matrix on upper triangular form
* @param b The vector b from upper_triangle(Matrix<U> & A, U* b)
* @param output The output array to store the vector x in linear system Ax=b
*/
template <class U>
void forward_substitution(Matrix<U>& A, U* b, U* output);
/**
* @tparam U
* @param A The input square matrix A that will be converted into upper triangular form through row operations
* @param b The same row operations are performed on the vector b
*/
template<class U>
void upper_triangle(Matrix<U>& A, U* b);
/**
* @tparam U
* @param A The input square matrix A that will be converted into upper triangular form through row operations
* @param b The same row operations are performed on the vector b
*/
template<class U>
void upper_triangle_CSRMatrix(CSRMatrix<U>& A, U* b);
/**
* This version takes a matrix and an array (vector)
* Used in Gaussian Jordan solver
* @tparam U
* @param mat Input matrix A
* @param b Input vector b
* @param cur_row Swap rows cur_row and max_row
* @param max_row Swap rows cur_row and max_row
*/
template<class U>
void swap_row(Matrix<U>& mat, U* b, int cur_row, int max_row);
/**
This version takes three parameters, which is used in LU decomposition pp
* @tparam U
* @param matrix Input matrix
* @param current_row Swap rows cur_row and max_row
* @param max_row Swap cur_row and max_row
*/
template<class U>
void swap_row(Matrix<U>& matrix, int current_row, int max_row);
template<class U>
void swap_row_CSRMatrix(CSRMatrix<U>& matrix, int current_row, int max_row);
/** LU decomposition
* @tparam U
* @param matA Input matrix A which we would apply operations to change it into upper triangular matrix U in place
* @param matL Input matrix L is initially zero matrix and will be changed into lower triangular matrix L in place
*/
template <class U>
void LU_decomposition(Matrix<U> & matA, Matrix<U> & matL);
/**
* This version uses partial pivoting
* @tparam U
* @param A The input square matrix A that will be converted into upper triangular form through row operations
* @param b The same row operations are performed on the vector b
*/
template<class U>
void upper_triangle_pp(Matrix<U> & A, U* b);
template<class U>
void upper_triangle_pp_CSRMatrix(CSRMatrix<U> & A, U* b);
/**
* @tparam U
* @param matA
* @param matP
* @param matL
* @param matU
*/
template <class U>
void LU_decomposition_partial_pivoting(Matrix<U>& matA, Matrix<U>& matP, Matrix<U>& matL, Matrix<U>& matU);
template <class U>
U gauss_seidel_addition(Matrix<U>& A, U *b, U *x, int i);
template <class U>
U gauss_seidel_addition_CSRMatrix(CSRMatrix<U> &A, U *b, U *x, int i);
template <class U>
void LU_decomposition_CSRMatrix(CSRMatrix<U> & matA, CSRMatrix<U> & matL);
template <class U>
void forward_substitution_CSRMatrix(CSRMatrix<U>& A, U* b, U* output);
// ---------------------Dense linear solvers---------------------
/** Gaussian Elimination Solver
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template<class U>
void Gaussian_Elimination(const Matrix<U>& matA, const U* vecb, U* output);
/** Gauss Jordan Solver
* This version uses partial pivoting
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void Gauss_Jordan(const Matrix<U>& matA, const U* vecb, U* output);
/** LU Solver
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void LU_solver( Matrix<U>& matA, U* vecb, U* output);
/** LU Solver with partial pivoting
* This version uses partial pivoting
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void LU_solver_partial_pivoting(Matrix<U>& matA, U* vecb, U* output);
/** Gauss Seidel Solver
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void gauss_seidel(Matrix<U>& matA, U* vecb, U* output);
/** Jacobi Method Solver
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param it_max Maximum number of iterations
* @param it_tol Maxium tolerance for residual (Once residual < it_tol, end iteration)
*/
template <class U>
void Jacobi_Method(Matrix<U> & matA, const U* vecb, int it_max, double it_tol, U* output);
// ---------------------Sparse linear solvers---------------------
/** Gaussian Elimination Solver for CSRMatrix
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void Gaussian_Elimination_CSRMatrix(CSRMatrix<U>& matA, const U* vecb, U* output);
/** Gauss Jordan Solver for CSRMatrix
* This version uses partial pivoting
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void Gauss_Jordan_CSRMatrix(CSRMatrix<U>& matA, const U* vecb, U* output);
/** Gauss Seidel Solver for CSRMatrix
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void Gauss_Seidel_CSRMatrix(CSRMatrix<U>& matA, U *vecb, U * output);
/** Jacobi Method Solver for CSRMatrix
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param it_max Maximum number of iterations
* @param it_tol Maxium tolerance for residual (Once residual < it_tol, end iteration)
*/
template <class U>
void Jacobi_Method_CSRMatrix(CSRMatrix<U> & matA, const U* vecb, int it_max, double it_tol, U* output);
/** LU Solver for CSRMatrix
* @tparam U
* @param matA The input square matrix A in the linear system Ax=b
* @param vecb The vector b in the linear system Ax=b
* @param output The vector x to be solved in the linear system Ax=b
*/
template <class U>
void LU_solver_CSRMatrix( CSRMatrix<U>& matA, U* vecb, U* output);
};
//function to check convergence, needed for Gauss-Seidel method
template <class T>
bool check_error(Matrix<T>& mat, Matrix<T>& vect, Matrix<T>& vect_output);
#endif //GROUP_ASSIGNMENT_LSG_SOLVER_H