Newer
Older
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
#include <iostream>
#include <iomanip>
#include <strstream>
#include <cmath>
#define WANT_STREAM
#define WANT_MATH
#include "sparse_matrix.h"
#include "newmatio.h"
#include "newmat.h"
#include "miscmaths/miscmaths.h"
#include "newimage/newimageall.h"
#include "utils/tracer_plus.h"
using namespace std;
using namespace Utilities;
using namespace NEWMAT;
using namespace MISCMATHS;
using namespace NEWIMAGE;
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);
}
}
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;
}
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);
}
}
void SparseMatrix::multiplyby(float S)
{
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 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;
}
}
}