-
Notifications
You must be signed in to change notification settings - Fork 6
/
SparseMatrix.inl
502 lines (450 loc) · 13.2 KB
/
SparseMatrix.inl
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#include <float.h>
///////////////////
// SparseMatrix //
///////////////////
///////////////////////////////////////
// SparseMatrix Methods and Memebers //
///////////////////////////////////////
template<class T>
SparseMatrix<T>::SparseMatrix()
{
rows=0;
rowSizes=NULL;
m_ppElements=NULL;
}
template<class T>
SparseMatrix<T>::SparseMatrix( int rows ){this->rows=0;Resize(rows);}
template<class T>
SparseMatrix<T>::SparseMatrix( const SparseMatrix& M )
{
Resize(M.rows);
for (int i=0; i<rows; i++){
SetRowSize(i,M.rowSizes[i]);
for(int j=0;j<rowSizes[i];j++){m_ppElements[i][j]=M.m_ppElements[i][j];}
}
}
template<class T>
int SparseMatrix<T>::Entries(void){
int e=0;
for(int i=0;i<rows;i++){e+=int(rowSizes[i]);}
return e;
}
template<class T>
SparseMatrix<T>& SparseMatrix<T>::operator = (const SparseMatrix<T>& M)
{
Resize(M.rows);
for (int i=0; i<rows; i++){
SetRowSize(i,M.rowSizes[i]);
for (int j=0; j<rowSizes[i]; j++){m_ppElements[i][j]=M.m_ppElements[i][j];}
}
return *this;
}
template<class T>
SparseMatrix<T>::~SparseMatrix(){Resize(0);}
template<class T>
void SparseMatrix<T>::Resize( int r )
{
int i;
if(rows>0){
free(m_ppElements);
free(rowSizes);
}
rows=r;
if(r){
rowSizes=(int*)malloc(sizeof(int)*r);
memset(rowSizes,0,sizeof(int)*r);
m_ppElements=(MatrixEntry<T>**)malloc(sizeof(MatrixEntry<T>*)*r);
}
}
template<class T>
void SparseMatrix<T>::SetRowSize(int row,int count){
if(row>=0 && row<rows){
if(rowSizes[row]){free(m_ppElements[row]);}
if(count>0){m_ppElements[row]=(MatrixEntry<T>*)malloc(sizeof(MatrixEntry<T>)*count);}
rowSizes[row]=count;
}
}
template<class T>
void SparseMatrix<T>::SetZero()
{
Resize(rows);
}
template<class T>
void SparseMatrix<T>::SetIdentity()
{
SetZero();
// for(int ij=0; ij < Min( this->Rows(), this->Columns() ); ij++)
// (*this)(ij,ij) = T(1);
for(int ij=0; ij < rows; ij++){
SetRowSize(ij,1);
m_ppElements[ij][0] = MatrixEntry<T>(ij);
m_ppElements[ij][0].Value = T(1);
}
}
template<class T>
SparseMatrix<T> SparseMatrix<T>::operator * (const T& V) const
{
SparseMatrix<T> M(*this);
M *= V;
return M;
}
template<class T>
SparseMatrix<T>& SparseMatrix<T>::operator *= (const T& V)
{
for (int i=0; i<this->Rows(); i++)
{
for(int ii=0;ii<m_ppElements[i].size();ii++){m_ppElements[i][ii].Value*=V;}
}
return *this;
}
template<class T>
SparseMatrix<T> SparseMatrix<T>::Multiply( const SparseMatrix<T>& M ) const
{
SparseMatrix<T> R( this->Rows(), M.Columns() );
for(int i=0; i<R.Rows(); i++){
for(int ii=0;ii<m_ppElements[i].size();ii++){
int N=m_ppElements[i][ii].N;
T Value=m_ppElements[i][ii].Value;
for(int jj=0;jj<M.m_ppElements[N].size();jj++){
R(i,M.m_ppElements[N][jj].N) += Value * M.m_ppElements[N][jj].Value;
}
}
}
return R;
}
template<class T>
template<class T2>
Vector<T2> SparseMatrix<T>::Multiply( const Vector<T2>& V ) const
{
Vector<T2> R( rows );
for (int i=0; i<rows; i++)
{
T2 temp=T2();
for(int ii=0;ii<rowSizes[i];ii++){
temp+=m_ppElements[i][ii].Value * V.m_pV[m_ppElements[i][ii].N];
}
R(i)=temp;
}
return R;
}
template<class T>
template<class T2>
void SparseMatrix<T>::Multiply( const Vector<T2>& In,Vector<T2>& Out) const
{
for (int i=0; i<rows; i++){
T2 temp=T2();
for(int j=0;j<rowSizes[i];j++){temp+=m_ppElements[i][j].Value * In.m_pV[m_ppElements[i][j].N];}
Out.m_pV[i]=temp;
}
}
template<class T>
SparseMatrix<T> SparseMatrix<T>::operator * (const SparseMatrix<T>& M) const
{
return Multiply(M);
}
template<class T>
template<class T2>
Vector<T2> SparseMatrix<T>::operator * (const Vector<T2>& V) const
{
return Multiply(V);
}
template<class T>
SparseMatrix<T> SparseMatrix<T>::Transpose() const
{
SparseMatrix<T> M( rows );
for (int i=0; i<rows; i++)
{
for(int ii=0;ii<m_ppElements[i].size();ii++){
M(m_ppElements[i][ii].N,i) = m_ppElements[i][ii].Value;
}
}
return M;
}
template<class T>
template<class T2>
int SparseMatrix<T>::SolveSymmetric(const SparseMatrix<T>& M,const Vector<T2>& b,const int& iters,Vector<T2>& solution,const T2 eps,const int& reset){
Vector<T2> d,r,Md;
T2 alpha,beta,rDotR;
Md.Resize(b.Dimensions());
if(reset){
solution.Resize(b.Dimensions());
solution.SetZero();
}
d=r=b-M.Multiply(solution);
rDotR=r.Dot(r);
if(b.Dot(b)<=eps){
solution.SetZero();
return 0;
}
int i;
for(i=0;i<iters;i++){
T2 temp;
M.Multiply(d,Md);
temp=d.Dot(Md);
if(temp<=eps){break;}
alpha=rDotR/temp;
r.SubtractScaled(Md,alpha);
temp=r.Dot(r);
if(temp/b.Dot(b)<=eps){break;}
beta=temp/rDotR;
solution.AddScaled(d,alpha);
if(beta<=eps){break;}
rDotR=temp;
Vector<T2>::Add(d,beta,r,d);
}
return i;
}
// Solve for x s.t. M(x)=b by solving for x s.t. M^tM(x)=M^t(b)
template<class T>
int SparseMatrix<T>::Solve(const SparseMatrix<T>& M,const Vector<T>& b,const int& iters,Vector<T>& solution,const T eps){
SparseMatrix mTranspose=M.Transpose();
Vector<T> bb=mTranspose*b;
Vector<T> d,r,Md;
T alpha,beta,rDotR;
int i;
solution.Resize(M.Columns());
solution.SetZero();
d=r=bb;
rDotR=r.Dot(r);
for(i=0;i<iters && rDotR>eps;i++){
T temp;
Md=mTranspose*(M*d);
alpha=rDotR/d.Dot(Md);
solution+=d*alpha;
r-=Md*alpha;
temp=r.Dot(r);
beta=temp/rDotR;
rDotR=temp;
d=r+d*beta;
}
return i;
}
////////////////////
// SparseNMatrix //
////////////////////
////////////////////////////////////////
// SparseNMatrix Methods and Memebers //
////////////////////////////////////////
template<class T,int Dim>
SparseNMatrix<T,Dim>::SparseNMatrix()
{
rows=0;
rowSizes=NULL;
m_ppElements=NULL;
}
template<class T,int Dim>
SparseNMatrix<T,Dim>::SparseNMatrix( int rows ){Resize(rows);}
template<class T,int Dim>
SparseNMatrix<T,Dim>::SparseNMatrix( const SparseNMatrix& M )
{
Resize(M.rows);
for (int i=0; i<rows; i++){
SetRowSize(i,M.rowSizes[i]);
for(int j=0;j<rowSizes[i];j++){m_ppElements[i][j]=M.m_ppElements[i][j];}
}
}
template<class T,int Dim>
int SparseNMatrix<T,Dim>::Entries(void){
int e=0;
for(int i=0;i<rows;i++){e+=int(rowSizes[i]);}
return e;
}
template<class T,int Dim>
SparseNMatrix<T,Dim>& SparseNMatrix<T,Dim>::operator = (const SparseNMatrix<T,Dim>& M)
{
Resize(M.rows);
for (int i=0; i<rows; i++){
SetRowSize(i,M.rowSizes[i]);
for (int j=0; j<rowSizes[i]; j++){m_ppElements[i][j]=M.m_ppElements[i][j];}
}
return *this;
}
template<class T,int Dim>
SparseNMatrix<T,Dim>::~SparseNMatrix(){Resize(0);}
template<class T,int Dim>
void SparseNMatrix<T,Dim>::Resize( int r )
{
int i;
if(rows>0){
free(m_ppElements);
free(rowSizes);
}
rows=r;
if(r){
rowSizes=(int*)malloc(sizeof(int)*r);
memset(rowSizes,0,sizeof(int)*r);
m_ppElements=(NMatrixEntry<T,Dim>**)malloc(sizeof(NMatrixEntry<T,Dim>*)*r);
}
}
template<class T,int Dim>
void SparseNMatrix<T,Dim>::SetRowSize(int row,int count){
if(row>=0 && row<rows){
if(rowSizes[row]){free(m_ppElements[row]);}
if(count>0){m_ppElements[row]=(NMatrixEntry<T,Dim>*)malloc(sizeof(NMatrixEntry<T,Dim>)*count);}
rowSizes[row]=count;
}
}
template<class T,int Dim>
SparseNMatrix<T,Dim> SparseNMatrix<T,Dim>::operator * (const T& V) const
{
SparseNMatrix<T,Dim> M(*this);
M *= V;
return M;
}
template<class T,int Dim>
SparseNMatrix<T,Dim>& SparseNMatrix<T,Dim>::operator *= (const T& V)
{
for (int i=0; i<rows; i++)
{
for(int ii=0;ii<m_ppElements[i].size();ii++){
for(int jj=0;jj<Dim;jj++){
m_ppElements[i][ii].Value[jj]*=V;
}
}
}
return *this;
}
template<class T,int Dim>
template<class T2>
NVector<T2,Dim> SparseNMatrix<T,Dim>::operator * (const Vector<T2>& V) const
{
NVector<T2,Dim> R( rows );
for (int i=0; i<rows; i++)
{
T2 temp[Dim];
for(int ii=0;ii<Dim;ii++){temp[ii]=T2();}
for(int ii=0;ii<rowSizes[i];ii++){
for(int jj=0;jj<Dim;jj++){temp[jj]+=m_ppElements[i][ii].Value[jj]*V.m_pV[m_ppElements[i][jj].N];}
}
for(int ii=0;ii<Dim;ii++){R[i][ii]=temp[ii];}
}
return R;
}
template<class T,int Dim>
template<class T2>
Vector<T2> SparseNMatrix<T,Dim>::operator * (const NVector<T2,Dim>& V) const
{
Vector<T2> R( rows );
for (int i=0; i<rows; i++)
{
T2 temp(0);
for(int ii=0;ii<rowSizes[i];ii++){
for(int jj=0;jj<Dim;jj++){temp+=m_ppElements[i][ii].Value[jj]*V.m_pV[m_ppElements[i][ii].N][jj];}
}
R(i)=temp;
}
return R;
}
///////////////////////////
// SparseSymmetricMatrix //
///////////////////////////
template<class T>
template<class T2>
Vector<T2> SparseSymmetricMatrix<T>::operator * (const Vector<T2>& V) const {return Multiply(V);}
template<class T>
template<class T2>
Vector<T2> SparseSymmetricMatrix<T>::Multiply( const Vector<T2>& V ) const
{
Vector<T2> R( this->rows );
for (int i=0; i<this->rows; i++){
for(int ii=0;ii<this->rowSizes[i];ii++){
int j=this->m_ppElements[i][ii].N;
R(i)+=this->m_ppElements[i][ii].Value * V.m_pV[j];
R(j)+=this->m_ppElements[i][ii].Value * V.m_pV[i];
}
}
return R;
}
template<class T>
template<class T2>
void SparseSymmetricMatrix<T>::Multiply( const Vector<T2>& In,Vector<T2>& Out) const
{
Out.SetZero();
for (int i=0; i<this->rows; i++){
MatrixEntry<T>* temp=this->m_ppElements[i];
T2& in1=In.m_pV[i];
T2& out1=Out.m_pV[i];
int rs=this->rowSizes[i];
for(int ii=0;ii<rs;ii++){
MatrixEntry<T>& temp2=temp[ii];
int j=temp2.N;
T2 v=temp2.Value;
out1+=v * In.m_pV[j];
Out.m_pV[j]+=v * in1;
}
}
}
template<class T>
template<class T2>
int SparseSymmetricMatrix<T>::Solve(const SparseSymmetricMatrix<T>& M,const Vector<T2>& b,const int& iters,Vector<T2>& solution,const T2 eps,const int& reset){
Vector<T2> d,r,Md;
T2 alpha,beta,rDotR,bDotB;
Md.Resize(b.Dimensions());
if(reset){
solution.Resize(b.Dimensions());
solution.SetZero();
}
d=r=b-M.Multiply(solution); // error vector
rDotR=r.Dot(r); // L2 distance of error vector
bDotB=b.Dot(b); // L2 distance of b
if(b.Dot(b)<=eps){
solution.SetZero();
return 0;
}
int i;
for(i=0;i<iters;i++){
T2 temp;
M.Multiply(d,Md); // vec Md = matrix M * vec d
temp=d.Dot(Md);
if(fabs(temp)<=eps){break;}
alpha=rDotR/temp;
r.SubtractScaled(Md,alpha);
temp=r.Dot(r);
if(temp/bDotB<=eps){break;}
beta=temp/rDotR;
solution.AddScaled(d,alpha);
if(beta<=eps){break;}
rDotR=temp;
Vector<T2>::Add(d,beta,r,d);
}
return i;
}
template<class T>
template<class T2>
int SparseSymmetricMatrix<T>::Solve(const SparseSymmetricMatrix<T>& M,const Vector<T>& diagonal,const Vector<T2>& b,const int& iters,Vector<T2>& solution,const T2 eps,const int& reset){
Vector<T2> d,r,Md;
if(reset){
solution.Resize(b.Dimensions());
solution.SetZero();
}
Md.Resize(M.rows);
for(int i=0;i<iters;i++){
M.Multiply(solution,Md);
r=b-Md;
for(int j=0;j<int(M.rows);j++){solution[j]+=r[j]/diagonal[j];}
}
return iters;
}