-
Notifications
You must be signed in to change notification settings - Fork 9
/
BandColMat.cpp
113 lines (93 loc) · 2.26 KB
/
BandColMat.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
#include "BandColMat.h"
namespace Daetk
{
using std::ostream;
using std::min;
using std::max;
using std::endl;
BandColMat::BandColMat():LinearOperator(0,0),M(),pivots(0),ku(0),kl(0),neq(0),rowOffSet(0){}
BandColMat::~BandColMat(){delete [] pivots;}
BandColMat& BandColMat::operator=(const BandColMat& C) { M=C.M; return *this;}
void BandColMat::beginAssembly(){}
void BandColMat::endAssembly(){}
BandColMat& BandColMat::operator=(const real& C)
{
for(unsigned int i=0;i<M.dim(0);i++)
{
for(unsigned int j=0;j<M.dim(1);j++)
{
M(i,j) = C;
}
}
return *this;
}
void BandColMat::zeroRow(int i)
{
for (int j=std::max(i-kl,0);j<=std::min(neq-1,i+ku);j++)
(*this)(i,j) = 0.0;
}
void BandColMat::zeroAll()
{
for(unsigned int i=0;i<M.dim(0);i++)
{
for(unsigned int j=0;j<M.dim(1);j++)
{
M(i,j) = 0.0;
}
}
}
bool BandColMat::apply(const Vec& x, Vec& Ax){ matVec(x,Ax); return false;}
real* BandColMat::castToArray(){return M.castToArray();}
const real* BandColMat::castToConstArray() const
{return M.castToConstArray();}
BandColMat::BandColMat(int upperDiagonals,int lowerDiagonals,int Neq):
LinearOperator(Neq,Neq),
M(2*lowerDiagonals+upperDiagonals+1,Neq,0.0),
ku(upperDiagonals),
kl(lowerDiagonals),
neq(Neq),
rowOffSet(kl-1)
{
pivots = new int[Neq];
}
void BandColMat::newsize(int lowerDiagonals,int upperDiagonals,int Neq)
{
ku=upperDiagonals;
kl=lowerDiagonals;
rowOffSet=kl-1;
neq=Neq;
M.newsize(2*kl+ku+1,neq);
delete [] pivots;
pivots = new int[neq];
}
ostream& operator<<(ostream& str,BandColMat& A)
{
for (int i=0;i<A.neq;i++)
{
str<<"row "<<i<<": ";
int bl(max(0,i-A.kl)),bu(min(A.neq-1,i+A.ku));
for (int j=bl;j<=bu;j++)
{
if (fabs(A.M(A.rowOffSet+A.ku+1+i-j,j)) !=0.0)
{
str<<j<<" "<<A.M(A.rowOffSet+A.ku+1+i-j,j)<<" ";
}
}
str<<endl;
}
return str;
}
void BandColMat::matVec(const Vec& x, Vec& Ax)
{
Ax=0.0;
for (int i=0;i<neq;i++)
{
int bl(max(0,i-kl)),bu(min(neq-1,i+ku));
for (int j=bl;j<=bu;j++)
{
if (M(rowOffSet+ku+1+i-j,j) !=0)
Ax(i)+=M(rowOffSet+ku+1+i-j,j)*x(j);
}
}
}
}//Daetk