summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2011-07-12 21:39:44 -0400
committerChris Dyer <cdyer@cs.cmu.edu>2011-07-12 21:39:44 -0400
commit9ab32f74dd821f08cb5863faf88d40ca60301688 (patch)
tree7861cd5a3d160a6ba3b9df746b0609dd8b273423
parentbde4a34bab96052570c248f7d9ccc299a9a3f097 (diff)
nasty bug in operator- in sparse vector
-rw-r--r--utils/fast_sparse_vector.h19
1 files changed, 10 insertions, 9 deletions
diff --git a/utils/fast_sparse_vector.h b/utils/fast_sparse_vector.h
index 4aae2039..9d72cb87 100644
--- a/utils/fast_sparse_vector.h
+++ b/utils/fast_sparse_vector.h
@@ -235,6 +235,13 @@ class FastSparseVector {
}
return *this;
}
+ FastSparseVector<T> erase_zeros(const T& EPSILON = 1e-4) const {
+ FastSparseVector<T> o;
+ for (const_iterator it = begin(); it != end(); ++it) {
+ if (fabs(it->second) > EPSILON) o.set_value(it->first, it->second);
+ }
+ return o;
+ }
const_iterator begin() const {
return const_iterator(*this, false);
}
@@ -344,15 +351,9 @@ const FastSparseVector<T> operator+(const FastSparseVector<T>& x, const FastSpar
template <typename T>
const FastSparseVector<T> operator-(const FastSparseVector<T>& x, const FastSparseVector<T>& y) {
- if (x.size() > y.size()) {
- FastSparseVector<T> res(x);
- res -= y;
- return res;
- } else {
- FastSparseVector<T> res(y);
- res -= x;
- return res;
- }
+ FastSparseVector<T> res(x);
+ res -= y;
+ return res;
}
template <class T>