Newer
Older
/* sparse_matrix.h
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#include <cmath>
#define WANT_STREAM
#define WANT_MATH
#include "sparse_matrix.h"
#include "newmatio.h"
#include "newmat.h"
#include "miscmaths.h"
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
#include "utils/tracer_plus.h"
using namespace std;
using namespace Utilities;
using namespace NEWMAT;
using namespace MISCMATHS;
namespace MISCMATHS {
SparseMatrix::SparseMatrix(int pnrows, int pncols) :
nrows(pnrows),
ncols(pncols),
data(pnrows)
{
}
void SparseMatrix::ReSize(int pnrows, int pncols)
{
nrows = pnrows;
ncols = pncols;
data.clear();
data.resize(nrows);
}
const SparseMatrix& SparseMatrix::operator=(const Matrix& pmatin)
{
data.clear();
data.resize(pmatin.Nrows());
nrows = pmatin.Nrows();
ncols = pmatin.Ncols();
for(int r=1; r <= pmatin.Nrows(); r++)
{
for(int c=1; c <= pmatin.Ncols(); c++)
{
if(pmatin(r,c)!=0)
insert(r,c,pmatin(r,c));
}
}
return *this;
}
void SparseMatrix::transpose(SparseMatrix& ret)
{
Tracer_Plus tr("SparseMatrix::transpose");
ret.ReSize(ncols,nrows);
for(int r=1; r <= nrows; r++)
for(map<int,double>::const_iterator it=data[r-1].begin(); it!=data[r-1].end(); it++)
ret.insert((*it).first+1, r, (*it).second);
}
int SparseMatrix::maxnonzerosinrow() const
{
int mx = 0;
for(int r=1; r <= nrows; r++)
{
int si = data[r-1].size();
if(si > mx)
mx = si;
}
return mx;
}
void SparseMatrix::permute(const ColumnVector& p, SparseMatrix& pA)
{
Tracer_Plus tr("SparseMatrix::permute");
pA.ReSize(nrows,ncols);
ColumnVector ip(p.Nrows());
for(int r=1; r <= nrows; r++)
ip(int(p(r))) = r;
for(int r=1; r <= nrows; r++)
for(map<int,double>::const_iterator it=data[r-1].begin(); it!=data[r-1].end(); it++)
pA.insert(int(ip(r)), int(ip((*it).first+1)), (*it).second);
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
}
}
ReturnMatrix SparseMatrix::AsMatrix() const
{
Matrix ret(nrows,ncols);
ret = 0;
for(int r=1; r <= nrows; r++)
for(map<int,double>::const_iterator it=data[r-1].begin(); it!=data[r-1].end(); it++)
ret(r,(*it).first+1) = (*it).second;
ret.Release();
return ret;
}
float SparseMatrix::trace() const
{
float tr = 0.0;
for(int k = 1; k<=Nrows(); k++)
{
tr += (*this)(k,k);
}
return tr;
}
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
void SparseMatrix::vertconcatbelowme(const SparseMatrix& B)
{
Tracer_Plus tr("SparseMatrix::vertconcatbelowme");
if (Ncols() != B.Ncols()) {throw Exception("Cols don't match in SparseMatrix::vertconcatbelowme");}
data.resize(Nrows()+B.Nrows());
for (int i=1; i<=B.Nrows(); i++) {
this->row(Nrows()+i) = B.row(i);
}
nrows += B.Nrows();
}
void SparseMatrix::vertconcataboveme(const SparseMatrix& A)
{
Tracer_Plus tr("SparseMatrix::vertconcataboveme");
if (Ncols() != A.Ncols()) {throw Exception("Cols don't match in SparseMatrix::vertconcataboveme");}
data.resize(Nrows()+A.Nrows());
for (int i=Nrows(); i>=1; i--) {
this->row(i+A.Nrows()) = this->row(i);
}
for (int i=1; i<=A.Nrows(); i++) {
this->row(i) = A.row(i);
}
nrows += A.Nrows();
}
void SparseMatrix::horconcat2myright(const SparseMatrix& B)
{
Tracer_Plus tr("SparseMatrix::horconcat2myright");
if (Nrows() != B.Nrows()) {throw Exception("Rows don't match in SparseMatrix::vertconcat2myright");}
for (int i=1; i<=Nrows(); i++) {
const SparseMatrix::Row& tmpRow = B.row(i);
for (SparseMatrix::Row::const_iterator it=tmpRow.begin(); it!=tmpRow.end(); it++) {
this->insert(i,Ncols()+int(it->first)+1,double(it->second));
}
}
ncols += B.Ncols();
}
void SparseMatrix::horconcat2myleft(const SparseMatrix& A)
{
Tracer_Plus tr("SparseMatrix::horconcat2myright");
if (Nrows() != A.Nrows()) {throw Exception("Rows don't match in SparseMatrix::vertconcat2myleft");}
for (int i=1; i<=Nrows(); i++) {
SparseMatrix::Row oldRow = this->row(i);
this->row(i) = SparseMatrix::Row(); // Empty row.
const SparseMatrix::Row& tmpRow = A.row(i);
for (SparseMatrix::Row::const_iterator it=tmpRow.begin(); it!=tmpRow.end(); it++) {
this->insert(i,int(it->first)+1,double(it->second));
}
for (SparseMatrix::Row::const_iterator it=oldRow.begin(); it!=oldRow.end(); it++) {
this->insert(i,A.Ncols()+int(it->first)+1,double(it->second));
}
}
ncols += A.Ncols();
}
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
ReturnMatrix SparseMatrix::RowAsColumn(int r) const
{
Tracer_Plus tr("SparseMatrix::RowAsColumn");
ColumnVector ret;
ret.ReSize(ncols);
ret = 0;
const SparseMatrix::Row& rowtmp = row(r);
for(SparseMatrix::Row::const_iterator it=rowtmp.begin();it!=rowtmp.end();it++)
{
int c = (*it).first+1;
double val = (*it).second;
ret(c) = val;
}
ret.Release();
return ret;
}
void colvectosparserow(const ColumnVector& col, SparseMatrix::Row& row)
{
Tracer_Plus tr("SparseMatrix::colvectosparserow");
for(int j = 1; j<=col.Nrows(); j++)
{
if(std::abs(col(j))>1e-4)
row[j-1] = col(j);
}
}
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
{
Tracer_Plus tr("SparseMatrix::multiplyby");
for(int j = 1; j<=Nrows(); j++)
{
SparseMatrix::Row& row = (*this).row(j);
for(SparseMatrix::Row::iterator it=row.begin();it!=row.end();it++)
{
(*it).second *= S;
}
}
}
void multiply(const SparseMatrix& lm, const SparseMatrix& rm, SparseMatrix& ret)
{
Tracer_Plus tr("SparseMatrix::multiply");
int nrows = lm.Nrows();
int ncols = rm.Ncols();
if(lm.Ncols() != rm.Nrows()) throw Exception("Rows and cols don't match in SparseMatrix::multiply");
ret.ReSize(nrows,ncols);
for(int j = 1; j<=nrows; j++)
{
const SparseMatrix::Row& row = lm.row(j);
for(SparseMatrix::Row::const_iterator it=row.begin();it!=row.end();it++)
{
int c = (*it).first+1;
double val = (*it).second;
for(int k = 1; k<=ncols; k++)
{
ret.addto(j,k,val*rm(c,k));
}
}
}
}
void multiply(const SparseMatrix& lm, const ColumnVector& rm, ColumnVector& ret)
{
Tracer_Plus tr("SparseMatrix::multiply2");
int nrows = lm.Nrows();
if(lm.Ncols() != rm.Nrows()) throw Exception("Rows and cols don't match in SparseMatrix::multiply");
ret.ReSize(nrows);
for(int j = 1; j<=nrows; j++)
{
float sum = 0.0;
const SparseMatrix::Row& row = lm.row(j);
for(SparseMatrix::Row::const_iterator it=row.begin();it!=row.end();it++)
{
int c = (*it).first+1;
double val = (*it).second;
sum += val*rm(c);
}
ret(j) = sum;
}
}
void multiply(const DiagonalMatrix& lm, const SparseMatrix& rm, SparseMatrix& ret)
{
Tracer_Plus tr("SparseMatrix::multiply");
int nrows = lm.Nrows();
int ncols = rm.Ncols();
if(lm.Ncols() != rm.Nrows()) throw Exception("Rows and cols don't match in SparseMatrix::multiply");
ret.ReSize(nrows,ncols);
for(int j = 1; j<=nrows; j++)
{
const SparseMatrix::Row& row = rm.row(j);
for(SparseMatrix::Row::const_iterator it=row.begin();it!=row.end();it++)
{
int c = (*it).first+1;
double val = (*it).second;
ret.insert(j,c,val*lm(j,j));
}
}
}
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
void multiply(const SparseMatrix& lm, const SparseMatrix::Row& rm, ColumnVector& ret)
{
Tracer_Plus tr("SparseMatrix::multiply3");
int nrows = lm.Nrows();
ret.ReSize(nrows);
for(int j = 1; j<=nrows; j++)
{
float sum = 0.0;
const SparseMatrix::Row& row = lm.row(j);
SparseMatrix::Row::const_iterator it=row.begin();
SparseMatrix::Row::const_iterator itrm=rm.begin();
while(it!=row.end() && itrm!=rm.end())
{
int crm = (*itrm).first;
int c = (*it).first;
if(c==crm)
{
sum += ((*itrm).second)*((*it).second);
it++;
itrm++;
}
else if(c < crm)
{
it++;
}
else
{
itrm++;
}
}
ret(j) = sum;
}
}
void add(const SparseMatrix& lm, const SparseMatrix& rm, SparseMatrix& ret)
{
Tracer_Plus tr("SparseMatrix::add");
int nrows = lm.Nrows();
int ncols = lm.Ncols();
if(lm.Ncols() != rm.Ncols() || lm.Nrows() != rm.Nrows()) throw Exception("Rows and cols don't match in SparseMatrix::add");
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
ret.ReSize(nrows,ncols);
for(int j = 1; j<=nrows; j++)
{
const SparseMatrix::Row& lmrow = lm.row(j);
const SparseMatrix::Row& rmrow = rm.row(j);
SparseMatrix::Row::const_iterator lmit = lmrow.begin();
SparseMatrix::Row::const_iterator rmit = rmrow.begin();
int lmc = (*lmit).first+1;
int rmc = (*rmit).first+1;
while(lmit!=lmrow.end() || rmit!=rmrow.end())
{
if((lmc<rmc && lmit!=lmrow.end()) || rmit==rmrow.end())
{
ret.insert(j,lmc,(*lmit).second+rm(j,lmc));
lmit++;
lmc = (*lmit).first+1;
}
else if((rmc<lmc && rmit!=rmrow.end()) || lmit==lmrow.end())
{
ret.insert(j,rmc,(*rmit).second+lm(j,rmc));
rmit++;
rmc = (*rmit).first+1;
}
else
{
//lmc==rmc
ret.insert(j,rmc,(*lmit).second+(*rmit).second);
lmit++;
lmc = (*lmit).first+1;
rmit++;
rmc = (*rmit).first+1;
}
}
}
}
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
// Concatenation. Note that these work also for concatenating sparse and full (newmat)
// matrices by virtue of the Matrix->SparseMatrix constructor/converter.
// ret = [A; B]; % Matlab lingo
void vertconcat(const SparseMatrix& A, const SparseMatrix& B, SparseMatrix& ret)
{
if (A.Ncols() != B.Ncols()) {throw Exception("Cols don't match in SparseMatrix::vertconcat");}
ret.ReSize(A.Nrows()+B.Nrows(),A.Ncols());
for (int i=1; i<=A.Nrows(); i++) {ret.row(i) = A.row(i);}
for (int i=1; i<=B.Nrows(); i++) {ret.row(i+A.Nrows()) = B.row(i);}
}
// ret = [A B]; % Matlab lingo
void horconcat(const SparseMatrix& A, const SparseMatrix& B, SparseMatrix& ret)
{
if (A.Nrows() != B.Nrows()) {throw Exception("Rows don't match in SparseMatrix::horconcat");}
ret.ReSize(A.Nrows(),A.Ncols()+B.Ncols());
for (int i=1; i<=A.Nrows(); i++) {
ret.row(i) = A.row(i);
const SparseMatrix::Row& tmpRow = B.row(i);
for (SparseMatrix::Row::const_iterator it=tmpRow.begin(); it!=tmpRow.end(); it++) {
ret.insert(i,A.Ncols()+int(it->first)+1,double(it->second));
}
}
}