-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSparseVector.cpp
297 lines (256 loc) · 5.84 KB
/
SparseVector.cpp
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
#include "stdafx.h"
#include "SparseVector.h"
#include "Vector.h"
int SparseVector::Length() const { return length_; }
double& SparseVector::operator[](int index)
{
std::list<SparseNode>::iterator it = nodes_.begin();
while (it != nodes_.end() && it->index < index)
{
it++;
}
if (it == nodes_.end() || it->index != index)
{
it = nodes_.insert(it, SparseNode(index, 0));
return it->value;
}
return it->value;
}
double SparseVector::operator[](int index) const
{
std::list<SparseNode>::const_iterator it = nodes_.begin();
while (it != nodes_.end() && it->index < index)
{
it++;
}
if (it == nodes_.end() || it->index != index)
return 0;
return it->value;
}
//todo - make this faster
bool SparseVector::operator==(const SparseVector& rhs) const
{
for (int i = 0; i<rhs.length_; i++)
{
if (rhs[i] != (*this)[i])
return false;
}
return true;
}
bool SparseVector::operator != (const SparseVector& rhs) const
{
return !((*this) == rhs);
}
double SparseVector::at(int index) const
{
std::list<SparseNode>::const_iterator it = nodes_.begin();
while (it != nodes_.end() && it->index < index)
{
it++;
}
if (it == nodes_.end() || it->index != index)
return 0;
return it->value;
}
SparseVector SparseVector::operator *(const SparseVector& rhs) const
{
// note: assuming two vectors have same length
SparseVector product(length_);
std::list<SparseVector::SparseNode>::const_iterator ita = nodes_.begin();
std::list<SparseVector::SparseNode>::const_iterator itb = rhs.nodes_.begin();
while (ita != nodes_.end() && itb != rhs.nodes_.end())
{
if (itb->index == ita->index)
{
product[itb->index] = ita->value * itb->value;
++ita;
++itb;
}
else if (itb->index < ita->index)
{
++itb;
}
else
{
++ita;
}
}
return product;
}
SparseVector SparseVector::operator /(const SparseVector& rhs) const
{
// note: assuming two vectors have same length
SparseVector quotient(length_);
std::list<SparseVector::SparseNode>::const_iterator ita = nodes_.begin();
std::list<SparseVector::SparseNode>::const_iterator itb = rhs.nodes_.begin();
while (ita != nodes_.end() && itb != rhs.nodes_.end())
{
if (itb->index == ita->index)
{
quotient[itb->index] = ita->value / itb->value;
++ita;
++itb;
}
else if (itb->index < ita->index)
{
++itb;
}
else
{
++ita;
}
}
return quotient;
}
SparseVector& SparseVector::operator+=(const SparseVector& rhs)
{
std::list<SparseVector::SparseNode>::iterator ita = nodes_.begin();
std::list<SparseVector::SparseNode>::const_iterator itb = rhs.nodes_.begin();
while (ita != nodes_.end() && itb != rhs.nodes_.end())
{
if (itb->index == ita->index)
{
ita->value += itb->value;
++ita;
++itb;
}
else if (itb->index < ita->index)
{
nodes_.insert(ita, SparseVector::SparseNode(itb->index, itb->value));
++itb;
}
else
{
++ita;
}
}
std::copy(itb, rhs.nodes_.end(), std::inserter(nodes_, ita));
return *this;
}
SparseVector& SparseVector::operator-=(const SparseVector& rhs)
{
std::list<SparseVector::SparseNode>::iterator ita = nodes_.begin();
std::list<SparseVector::SparseNode>::const_iterator itb = rhs.nodes_.begin();
while (ita != nodes_.end() && itb != rhs.nodes_.end())
{
if (itb->index == ita->index)
{
ita->value -= itb->value;
++ita;
++itb;
}
else if (itb->index < ita->index)
{
nodes_.insert(ita, SparseVector::SparseNode(itb->index, -itb->value));
++itb;
}
else
{
++ita;
}
}
std::transform(itb, rhs.nodes_.end(), inserter(nodes_, ita),
SparseNode::negate());
return *this;
}
SparseVector& SparseVector::operator*=(double rhs)
{
std::transform(nodes_.begin(), nodes_.end(), nodes_.begin(),
SparseNode::multiply(rhs));
return *this;
}
SparseVector& SparseVector::operator/=(double rhs)
{
transform(nodes_.begin(), nodes_.end(), nodes_.begin(),
SparseNode::divide(rhs));
return *this;
}
//this zeroes out this vector
void SparseVector::clear()
{
nodes_.clear();
}
void SparseVector::MakeSparse()
{
nodes_.erase(std::remove_if(nodes_.begin(), nodes_.end(), NodeIsZero()), nodes_.end());
}
double SparseVector::Sparsity() const
{
return double(nodes_.size()) / double(length_);
}
void SparseVector::Resize(int size)
{
if (size < length_)
{
nodes_.erase(std::remove_if(nodes_.begin(), nodes_.end(), IndexGreaterThan(size - 1)), nodes_.end());
}
length_ = size;
}
double InnerProduct(const SparseVector& lhs, const SparseVector& rhs)
{
double product = 0;
std::list<SparseVector::SparseNode>::const_iterator ita = lhs.nodes_.begin();
std::list<SparseVector::SparseNode>::const_iterator itb = rhs.nodes_.begin();
while (ita != lhs.nodes_.end() && itb != rhs.nodes_.end())
{
if (itb->index == ita->index)
{
product += ita->value * itb->value;
++ita;
++itb;
}
else if (itb->index < ita->index)
{
++itb;
}
else
{
++ita;
}
}
return product;
}
double InnerProduct(const SparseVector& lhs, const Vector& rhs)
{
if (lhs.Length() != rhs.Length()) throw WrongDimensionException();
double product = 0;
SparseVector::const_iterator it(lhs);
while (it)
{
product += it.getVal() * rhs[it.getIndex()];
++it;
}
return product;
}
double InnerProduct(const Vector& lhs, const SparseVector& rhs)
{
if (lhs.Length() != rhs.Length()) throw WrongDimensionException();
double product = 0;
SparseVector::const_iterator it(rhs);
while (it)
{
product += it.getVal() * lhs[it.getIndex()];
++it;
}
return product;
}
SparseVector operator+(const SparseVector& a, const SparseVector& b)
{
return SparseVector(a) += b;
}
SparseVector operator-(const SparseVector& a, const SparseVector& b)
{
return SparseVector(a) -= b;
}
SparseVector operator*(const SparseVector& a, double b)
{
return SparseVector(a) *= b;
}
SparseVector operator*(double a, const SparseVector& b)
{
return SparseVector(b) *= a;
}
SparseVector operator/(const SparseVector& a, double b)
{
return SparseVector(a) /= b;
}