-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.h
More file actions
executable file
·486 lines (432 loc) · 10.9 KB
/
Matrix.h
File metadata and controls
executable file
·486 lines (432 loc) · 10.9 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#pragma once
#include "stdio.h"
#include "Vector.h"
#include "project.h"
#include <iostream>
using namespace std;
template <class T=double>
class Matrix
{
private:
int nRow,nCol;
double* pData;
static bool IsDispInfo;
public:
Matrix(void);
Matrix(int _nrow,int _ncol,double* data=NULL);
Matrix(const Matrix<T>& matrix);
~Matrix(void);
void releaseData();
void copyData(const Matrix<T>& matrix);
void allocate(const Matrix<T>& matrix);
void allocate(int _nrow,int _ncol);
void reset();
bool dimMatch(const Matrix<T>& matrix) const;
bool dimcheck(const Matrix<T>& matrix) const;
void loadData(int _nrow,int _ncol,T* data);
static void enableDispInfo(bool dispInfo=false){IsDispInfo=dispInfo;};
// display the matrix
void printMatrix();
void identity(int ndim);
// function to access the member variables
inline int nrow() const{return nRow;};
inline int ncol() const{return nCol;};
inline double* data() {return pData;};
inline const double* data() const {return (const double*)pData;};
inline double operator [](int index) const{return pData[index];};
inline double& operator[](int index) {return pData[index];};
inline double data(int row,int col)const {return pData[row*nCol+col];};
inline double& data(int row,int col) {return pData[row*nCol+col];};
bool matchDimension(int _nrow,int _ncol) const {if(nRow==_nrow && nCol==_ncol) return true; else return false;};
bool matchDimension(const Matrix<T>& matrix) const {return matchDimension(matrix.nrow(),matrix.ncol());};
// functions to check dimensions
bool checkDimRight(const Vector<T>& vector) const;
bool checkDimRight(const Matrix<T>& matrix) const;
bool checkDimLeft(const Vector<T>& vector) const;
bool checkDimLeft(const Matrix<T>& matrix) const;
// functions for matrix computation
void Multiply(Vector<T>& result,const Vector<T>& vect) const;
void Multiply(Matrix<T>& result,const Matrix<T>& matrix) const;
void transpose(Matrix& result) const;
void fromVector(const Vector<T>& vect);
double norm2() const;
double sum() const
{
double total = 0;
for(int i = 0;i<nCol*nRom;i++)
total += pData[i];
return total;
}
// operators
Matrix& operator=(const Matrix<T>& matrix);
Matrix& operator+=(double val);
Matrix& operator-=(double val);
Matrix& operator*=(double val);
Matrix& operator/=(double val);
Matrix& operator+=(const Matrix<T>& matrix);
Matrix& operator-=(const Matrix<T>& matrix);
Matrix& operator*=(const Matrix<T>& matrix);
Matrix& operator/=(const Matrix<T>& matrix);
friend Vector<T> operator*(const Matrix<T>& matrix,const Vector<T>& vect);
friend Matrix<T> operator*(const Matrix<T>& matrix1,const Matrix<T>& matrix2);
// solve linear systems
void SolveLinearSystem(Vector<T>& result,const Vector<T>& b) const;
void ConjugateGradient(Vector<T>& result,const Vector<T>& b) const;
template<class T>
bool Matrix<T>::IsDispInfo=false;
template<class T>
Matrix<T>::Matrix(void)
{
nRow=nCol=0;
pData=NULL;
}
template<class T>
Matrix<T>::Matrix(int nrow,int ncol,double* data)
{
nRow=nrow;
nCol=ncol;
pData=new T[nRow*nCol];
if(data==NULL)
memset(pData,0,sizeof(T)*nRow*nCol);
else
memcpy(pData,data,sizeof(T)*nRow*nCol);
}
template<class T>
Matrix<T>::Matrix(const Matrix<T>& matrix)
{
nRow=nCol=0;
pData=NULL;
copyData(matrix);
}
template<class T>
Matrix<T>::~Matrix(void)
{
releaseData();
}
template<class T>
void Matrix<T>::releaseData()
{
if(pData!=NULL)
delete pData;
pData=NULL;
nRow=nCol=0;
}
template<class T>
void Matrix<T>::copyData(const Matrix<T> &matrix)
{
if(!dimMatch(matrix))
allocate(matrix);
memcpy(pData,matrix.pData,sizeof(T)*nRow*nCol);
}
template<class T>
bool Matrix<T>::dimMatch(const Matrix<T>& matrix) const
{
if(nCol==matrix.nCol && nRow==matrix.nRow)
return true;
else
return false;
}
template<class T>
bool Matrix<T>::dimcheck(const Matrix<T>& matrix) const
{
if(!dimMatch(matrix))
{
cout<<"The dimensions of the matrices don't match!"<<endl;
return false;
}
return true;
}
template<class T>
void Matrix<T>::reset()
{
if(pData!=NULL)
memset(pData,0,sizeof(T)*nRow*nCol);
}
template<class T>
void Matrix<T>::allocate(int nrow,int ncol)
{
releaseData();
nRow=nrow;
nCol=ncol;
if(nRow*nCol>0)
{
pData=new T[nRow*nCol];
memset(pData,0,sizeof(T)*nRow*nCol);
}
}
template<class T>
void Matrix<T>::allocate(const Matrix<T>& matrix)
{
allocate(matrix.nRow,matrix.nCol);
}
template<class T>
void Matrix<T>::loadData(int _nrow, int _ncol, T *data)
{
if(!matchDimension(_nrow,_ncol))
allocate(_nrow,_ncol);
memcpy(pData,data,sizeof(T)*nRow*nCol);
}
template<class T>
void Matrix<T>::printMatrix()
{
for(int i=0;i<nRow;i++)
{
for(int j=0;j<nCol;j++)
cout<<pData[i*nCol+j]<<" ";
cout<<endl;
}
}
template<class T>
void Matrix<T>::identity(int ndim)
{
allocate(ndim,ndim);
reset();
for(int i=0;i<ndim;i++)
pData[i*ndim+i]=1;
}
//--------------------------------------------------------------------------------------------------
// functions to check dimensionalities
//--------------------------------------------------------------------------------------------------
template<class T>
bool Matrix<T>::checkDimRight(const Vector<T>& vect) const
{
if(nCol==vect.dim())
return true;
else
{
cout<<"The matrix and vector don't match in multiplication!"<<endl;
return false;
}
}
template<class T>
bool Matrix<T>::checkDimRight(const Matrix<T> &matrix) const
{
if(nCol==matrix.nrow())
return true;
else
{
cout<<"The matrix and matrix don't match in multiplication!"<<endl;
return false;
}
}
template<class T>
bool Matrix<T>::checkDimLeft(const Vector<T>& vect) const
{
if(nRow==vect.dim())
return true;
else
{
cout<<"The vector and matrix don't match in multiplication!"<<endl;
return false;
}
}
template<class T>
bool Matrix<T>::checkDimLeft(const Matrix<T> &matrix) const
{
if(nRow==matrix.ncol())
return true;
else
{
cout<<"The matrix and matrix don't match in multiplication!"<<endl;
return false;
}
}
//--------------------------------------------------------------------------------------------------
// functions for numerical computation
//--------------------------------------------------------------------------------------------------
template<class T>
void Matrix<T>::Multiply(Vector<T> &result, const Vector<T>&vect) const
{
checkDimRight(vect);
if(result.dim()!=nRow)
result.allocate(nRow);
for(int i=0;i<nRow;i++)
{
double temp=0;
for(int j=0;j<nCol;j++)
temp+=pData[i*nCol+j]*vect.data()[j];
result.data()[i]=temp;
}
}
template<class T>
void Matrix<T>::Multiply(Matrix<T> &result, const Matrix<T> &matrix) const
{
checkDimRight(matrix);
if(!result.matchDimension(nRow,matrix.nCol))
result.allocate(nRow,matrix.nCol);
for(int i=0;i<nRow;i++)
for(int j=0;j<matrix.nCol;j++)
{
double temp=0;
for(int k=0;k<nCol;k++)
temp+=pData[i*nCol+k]*matrix.pData[k*matrix.nCol+j];
result.pData[i*matrix.nCol+j]=temp;
}
}
template<class T>
void Matrix<T>::transpose(Matrix<T> &result) const
{
if(!result.matchDimension(nCol,nRow))
result.allocate(nCol,nRow);
for(int i=0;i<nCol;i++)
for(int j=0;j<nRow;j++)
result.pData[i*nRow+j]=pData[j*nCol+i];
}
template<class T>
void Matrix<T>::fromVector(const Vector<T>&vect)
{
if(!matchDimension(vect.dim(),1))
allocate(vect.dim(),1);
memcpy(pData,vect.data(),sizeof(double)*vect.dim());
}
template<class T>
double Matrix<T>::norm2() const
{
if(pData==NULL)
return 0;
double temp=0;
for(int i=0;i<nCol*nRow;i++)
temp+=pData[i]*pData[i];
return temp;
}
//--------------------------------------------------------------------------------------------------
// operators
//--------------------------------------------------------------------------------------------------
template<class T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& matrix)
{
copyData(matrix);
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator +=(double val)
{
for(int i=0;i<nCol*nRow;i++)
pData[i]+=val;
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator -=(double val)
{
for(int i=0;i<nCol*nRow;i++)
pData[i]-=val;
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator *=(double val)
{
for(int i=0;i<nCol*nRow;i++)
pData[i]*=val;
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator /=(double val)
{
for(int i=0;i<nCol*nRow;i++)
pData[i]/=val;
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator +=(const Matrix<T> &matrix)
{
dimcheck(matrix);
for(int i=0;i<nCol*nRow;i++)
pData[i]+=matrix.pData[i];
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator -=(const Matrix<T> &matrix)
{
dimcheck(matrix);
for(int i=0;i<nCol*nRow;i++)
pData[i]-=matrix.pData[i];
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator *=(const Matrix<T> &matrix)
{
dimcheck(matrix);
for(int i=0;i<nCol*nRow;i++)
pData[i]*=matrix.pData[i];
return *this;
}
template<class T>
Matrix<T>& Matrix<T>::operator /=(const Matrix<T> &matrix)
{
dimcheck(matrix);
for(int i=0;i<nCol*nRow;i++)
pData[i]/=matrix.pData[i];
return *this;
}
template<class T>
Vector<T> operator*(const Matrix<T>& matrix,const Vector<T>& vect)
{
Vector<T> result;
matrix.Multiply(result,vect);
return result;
}
template<class T>
Matrix<T> operator*(const Matrix<T>& matrix1,const Matrix<T>& matrix2)
{
Matrix<T> result;
matrix1.Multiply(result,matrix2);
return result;
}
//--------------------------------------------------------------------------------------------------
// function for conjugate gradient method
//--------------------------------------------------------------------------------------------------
template<class T>
void Matrix<T>::ConjugateGradient(Vector<T> &result, const Vector<T>&b) const
{
if(nCol!=nRow)
{
cout<<"Error: when solving Ax=b, A is not square!"<<endl;
return;
}
checkDimRight(b);
if(!result.matchDimension(b))
result.allocate(b);
Vector<T> r(b),p,q;
result.reset();
int nIterations=nRow*5;
Vector<T> rou(nIterations);
for(int k=0;k<nIterations;k++)
{
rou[k]=r.norm2();
if(IsDispInfo)
cout<<rou[k]<<endl;
if(rou[k]<1E-20)
break;
if(k==0)
p=r;
else
{
double ratio=rou[k]/rou[k-1];
p=r+p*ratio;
}
Multiply(q,p);
double alpha=rou[k]/innerproduct(p,q);
result+=p*alpha;
r-=q*alpha;
}
}
template<class T>
void Matrix<T>::SolveLinearSystem(Vector<T> &result, const Vector<T>&b) const
{
if(nCol==nRow)
{
ConjugateGradient(result,b);
return;
}
if(nRow<nCol)
{
cout<<"Not enough observations for parameter estimation!"<<endl;
return;
}
Matrix<T> AT,ATA;
transpose(AT);
AT.Multiply(ATA,*this);
Vector<T> ATb;
AT.Multiply(ATb,b);
ATA.ConjugateGradient(result,ATb);
}