My Project
dgbmatrix-dgbmatrix.hpp
1 //=============================================================================
4 inline dgbmatrix& dgbmatrix::operator=(const dgbmatrix& mat)
5 {
6 #ifdef CPPL_VERBOSE
7  std::cerr << "# [MARK] dgbmatrix::operator=(const dgbmatrix&)"
8  << std::endl;
9 #endif//CPPL_VERBOSE
10 
11  if(Array!=mat.Array){ // if it is NOT self substitution
12  copy(mat);
13  }
14  return *this;
15 }
16 
20 
21 //=============================================================================
24 inline dgbmatrix& dgbmatrix::operator+=(const dgbmatrix& mat)
25 {
26 #ifdef CPPL_VERBOSE
27  std::cerr << "# [MARK] dgbmatrix::operator+=(const dgbmatrix&)"
28  << std::endl;
29 #endif//CPPL_VERBOSE
30 
31 #ifdef CPPL_DEBUG
32  if(N!=mat.N || M!=mat.M){
33  std::cerr << "[ERROR] dgbmatrix::operator+=(dgbmatrix&)" << std::endl
34  << "These two matrises can not make a summation." << std::endl
35  << "Your input was"
36  << "(" << M <<"x"<< N <<","<< KL <<":"<< KU << ") "<< "+="
37  << "("<< mat.M <<"x"<< mat.N <<","<< mat.KL <<":"<< mat.KU <<") "
38  << std::endl;
39  exit(1);
40  }
41 #endif//CPPL_DEBUG
42 
43  if(KL>=mat.KL && KU>=mat.KU){
44  for(long i=0; i<M; i++){
45  for(long j=max(0,i-mat.KL); j<min(N,i+mat.KU+1); j++){
46  operator()(i,j)+=mat(i,j);
47  }
48  }
49 
50  return *this;
51  }
52 
53  else{
54  dgbmatrix newmat(M,N,max(KL,mat.KL),max(KU,mat.KU));
55  newmat.zero();
56  for(long i=0; i<M; i++){
57  for(long j=max(0,i-KL); j<min(N,i+KU+1); j++){
58  newmat(i,j)+=operator()(i,j);
59  }
60  for(long j=max(0,i-mat.KL); j<min(mat.N,i+mat.KU+1); j++){
61  newmat(i,j)+=mat(i,j);
62  }
63  }
64 
65  swap(*this,newmat);
66  return *this;
67  }
68 }
69 
70 //=============================================================================
73 inline dgbmatrix& dgbmatrix::operator-=(const dgbmatrix& mat)
74 {
75 #ifdef CPPL_VERBOSE
76  std::cerr << "# [MARK] dgbmatrix::operator-=(const dgbmatrix&)"
77  << std::endl;
78 #endif//CPPL_VERBOSE
79 
80 #ifdef CPPL_DEBUG
81  if(N!=mat.N || M!=mat.M){
82  std::cerr << "[ERROR] dgbmatrix::operator-=(dgbmatrix&)" << std::endl
83  << "These two matrises can not make a subtraction." << std::endl
84  << "Your input was"
85  << "(" << M <<"x"<< N <<","<< KL <<":"<< KU << ") "<< "-="
86  << "("<< mat.M <<"x"<< mat.N <<","<< mat.KL <<":"<< mat.KU <<") "
87  << std::endl;
88  exit(1);
89  }
90 #endif//CPPL_DEBUG
91 
92  if(KL>=mat.KL && KU>=mat.KU){
93  for(long i=0; i<M; i++){
94  for(long j=max(0,i-mat.KL); j<min(N,i+mat.KU+1); j++){
95  operator()(i,j)-=mat(i,j);
96  }
97  }
98 
99  return *this;
100  }
101 
102  else{
103  dgbmatrix newmat(M,N,max(KL,mat.KL),max(KU,mat.KU));
104  newmat.zero();
105  for(long i=0; i<M; i++){
106  for(long j=max(0,i-KL); j<min(N,i+KU+1); j++){
107  newmat(i,j)+=operator()(i,j);
108  }
109  for(long j=max(0,i-mat.KL); j<min(mat.N,i+mat.KU+1); j++){
110  newmat(i,j)-=mat(i,j);
111  }
112  }
113 
114  swap(*this,newmat);
115  return *this;
116  }
117 }
118 
119 //=============================================================================
121 inline dgbmatrix& dgbmatrix::operator*=(const dgbmatrix& mat)
122 {
123 #ifdef CPPL_VERBOSE
124  std::cerr << "# [MARK] dgbmatrix::operator-=(const dgbmatrix&)"
125  << std::endl;
126 #endif//CPPL_VERBOSE
127 
128 #ifdef CPPL_DEBUG
129  if(N!=mat.M){
130  std::cerr << "[ERROR] dgbmatrix::operator*=(dgbmatrix&)" << std::endl
131  << "These two matrises can not make a product." << std::endl
132  << "Your input was (" << M << "x" << N << ") * ("
133  << mat.M << "x" << mat.N << ")." << std::endl;
134  exit(1);
135  }
136 #endif//CPPL_DEBUG
137 
138  dgbmatrix newmat( M, mat.N, KU+mat.KL-1, KL+mat.KU+1 );
139  newmat.zero();
140 
141  for(long i=0; i<newmat.M; i++){
142  for(long j=max(0,i-newmat.KL); j<min(newmat.N,i+newmat.KU+1); j++){
143  for(long k=max( max(0,i-KL), max(0,j-mat.KU) );
144  k< min( min(N,i+KU+1), min(mat.M,j+mat.KL+1) ); k++){
145  newmat(i,j)+= operator()(i,k)*mat(k,j);
146  }
147  }
148  }
149 
150  swap(*this,newmat);
151  return *this;
152 }
153 
157 
158 //=============================================================================
160 inline _dgbmatrix operator+(const dgbmatrix& matA, const dgbmatrix& matB)
161 {
162 #ifdef CPPL_VERBOSE
163  std::cerr << "# [MARK] operator+(const dgbmatrix&, const dgbmatrix&)"
164  << std::endl;
165 #endif//CPPL_VERBOSE
166 
167 #ifdef CPPL_DEBUG
168  if(matA.N!=matB.N || matA.M!=matB.M){
169  std::cerr << "[ERROR] operator+(dgbmatrix&, dgbmatrix&)" << std::endl
170  << "These two matrises can not make a summation." << std::endl
171  << "Your input was (" << matA.M << "x" << matA.N << ") + ("
172  << matB.M << "x" << matB.N << ")." << std::endl;
173  exit(1);
174  }
175 #endif//CPPL_DEBUG
176 
177  dgbmatrix newmat(matA.M,matA.N,max(matA.KL,matB.KL),max(matA.KU,matB.KU));
178  newmat.zero();
179 
180  for(long i=0; i<matA.M; i++){
181  for(long j=max(0,i-matA.KL); j<min(matA.N,i+matA.KU+1); j++){
182  newmat(i,j)+=matA(i,j);
183  }
184  for(long j=max(0,i-matB.KL); j<min(matB.N,i+matB.KU+1); j++){
185  newmat(i,j)+=matB(i,j);
186  }
187  }
188 
189  return _(newmat);
190 }
191 
192 //=============================================================================
194 inline _dgbmatrix operator-(const dgbmatrix& matA, const dgbmatrix& matB)
195 {
196 #ifdef CPPL_VERBOSE
197  std::cerr << "# [MARK] operator-(const dgbmatrix&, const dgbmatrix&)"
198  << std::endl;
199 #endif//CPPL_VERBOSE
200 
201 #ifdef CPPL_DEBUG
202  if(matA.N!=matB.N || matA.M!=matB.M){
203  std::cerr << "[ERROR] operator-(dgbmatrix&, dgbmatrix&)" << std::endl
204  << "These two matrises can not make a subtraction." << std::endl
205  << "Your input was (" << matA.M << "x" << matA.N << ") - ("
206  << matB.M << "x" << matB.N << ")." << std::endl;
207  exit(1);
208  }
209 #endif//CPPL_DEBUG
210 
211  dgbmatrix newmat(matA.M,matA.N,max(matA.KL,matB.KL),max(matA.KU,matB.KU));
212  newmat.zero();
213 
214  for(long i=0; i<matA.M; i++){
215  for(long j=max(0,i-matA.KL); j<min(matA.N,i+matA.KU+1); j++){
216  newmat(i,j)+=matA(i,j);
217  }
218  for(long j=max(0,i-matB.KL); j<min(matB.N,i+matB.KU+1); j++){
219  newmat(i,j)-=matB(i,j);
220  }
221  }
222 
223  return _(newmat);
224 }
225 
226 //=============================================================================
228 inline _dgbmatrix operator*(const dgbmatrix& matA, const dgbmatrix& matB)
229 {
230 #ifdef CPPL_VERBOSE
231  std::cerr << "# [MARK] operator*(const dgbmatrix&, const dgbmatrix&)"
232  << std::endl;
233 #endif//CPPL_VERBOSE
234 
235 #ifdef CPPL_DEBUG
236  if(matA.N!=matB.M){
237  std::cerr << "[ERROR] operator*(dgbmatrix&, dgbmatrix&)" << std::endl
238  << "These two matrises can not make a product." << std::endl
239  << "Your input was (" << matA.M << "x" << matA.N << ") * ("
240  << matB.M << "x" << matB.N << ")." << std::endl;
241  exit(1);
242  }
243 #endif//CPPL_DEBUG
244 
245  dgbmatrix newmat( matA.M, matB.N, matA.KU+matB.KL-1, matA.KL+matB.KU+1 );
246  newmat.zero();
247 
248  long i, j, k;
249 #pragma omp parallel for private(j,k)
250  for(i=0; i<newmat.M; i++){
251  for(j=max(0,i-newmat.KL); j<min(newmat.N,i+newmat.KU+1); j++){
252  for(k=max( max(0,i-matA.KL), max(0,j-matB.KU) );
253  k< min( min(matA.N,i+matA.KU+1), min(matB.M,j+matB.KL+1) ); k++){
254  newmat(i,j)+= matA(i,k)*matB(k,j);
255  }
256  }
257  }
258 
259  return _(newmat);
260 }
dgbmatrix & operator=(const dgbmatrix &)
Definition: dgbmatrix-dgbmatrix.hpp:4
friend _dgematrix i(const dgbmatrix &)
Definition: dgbmatrix-calc.hpp:22
double * Array
1D Array to store vector data
Definition: _drovector.hpp:8
double & operator()(const long &, const long &)
Definition: dgbmatrix-io.hpp:3
dgbmatrix & operator+=(const dgbmatrix &)
Definition: dgbmatrix-dgbmatrix.hpp:24
friend _drovector operator-(const _drovector &)
Definition: _drovector-unary.hpp:15
Real Double-precision General Band Matrix Class.
Definition: dgbmatrix.hpp:3
dgbmatrix & operator*=(const dgbmatrix &)
Definition: dgbmatrix-dgbmatrix.hpp:121
void copy(const dgbmatrix &)
Definition: dgbmatrix-misc.hpp:74
friend _drovector operator*(const drovector &, const dgematrix &)
Definition: drovector-dgematrix.hpp:3
(DO NOT USE) Smart-temporary Real Double-precision General Band Matrix Class
Definition: _dgbmatrix.hpp:3
dgbmatrix & operator-=(const dgbmatrix &)
Definition: dgbmatrix-dgbmatrix.hpp:73
friend const _drovector & operator+(const _drovector &)
Definition: _drovector-unary.hpp:3
long M
matrix row size
Definition: _dgematrix.hpp:7
friend void swap(dgbmatrix &, dgbmatrix &)
Definition: dgbmatrix-misc.hpp:167