-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearSolver.hpp
73 lines (63 loc) · 2.55 KB
/
linearSolver.hpp
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
// Karen's CGD
#define MAX_STEPS 100000
/*****************************************************************************
* Matrix class the solver will accept
*****************************************************************************/
class ImplicitMatrix
{
public:
virtual ~ImplicitMatrix()
{
}
virtual void MatVecMult(const double x[], double r[]) const = 0;
};
/*****************************************************************************
* This implicit matrix represents a discrete Laplacian operator.
*****************************************************************************/
class ImplicitMatrixLap : public ImplicitMatrix
{
public:
explicit ImplicitMatrixLap(int gridSize) : m_GridSize(gridSize)
{
}
/*************************************************************************
* Calculates the result of an implicit matrix multiply with the vector x
* and stores it in r.
* This implicit matrix multiply calculates the Laplacian of x
* assuming x is a grid of m_NumCells by m_NumCells. In general,
* the Laplacian at x[i] will be -4*x[i] plus each of its neighboring
* cells in the grid. This would then be divided by the size of a cell, but
* I assume that the cell size is unit length.
*
* @param x The grid to take the Laplacian of.
* @param r Stores the calculated Laplacian.
*************************************************************************/
virtual void MatVecMult(const double x[], double r[]) const override;
private:
// The width and length of grid of cells.
int m_GridSize;
int Idx2DTo1D(int x, int y) const
{
return x + (m_GridSize * y);
}
};
/*****************************************************************************
* Solves Ax = b for a symmetric, positive definite matrix A.
* A is represented implicitly by the function MatVecMult()
* which performs a matrix vector multiply, Av, and places the result in r.
*
* @param n The length of the vectors x and b.
* @param A Symmetric, positive definite matrix.
* @param epsilon The error tolerance.
* @param steps[in,out] When passed in, steps is the maximum number of
* iterations to take to solve. If not set, or set to
* less than 1, MAX_STEPS is used instead.
* Upon completion, steps contains the actual number of
* iterations taken.
*****************************************************************************/
double ConjGrad(int n,
const ImplicitMatrix *A,
double x[],
const double b[],
double epsilon,
int *steps);