diff options
author | Chris Dyer <cdyer@cs.cmu.edu> | 2011-07-12 21:39:44 -0400 |
---|---|---|
committer | Chris Dyer <cdyer@cs.cmu.edu> | 2011-07-12 21:39:44 -0400 |
commit | 79899fe98e2a7e4d250a8518b8a4cd8f2bb93522 (patch) | |
tree | dec1ccbaab44df921ed07fdd23316d7755cf61ac /utils/fast_sparse_vector.h | |
parent | a8a8aeba08d5c0f6841394087bb4ec0b6ade0694 (diff) |
nasty bug in operator- in sparse vector
Diffstat (limited to 'utils/fast_sparse_vector.h')
-rw-r--r-- | utils/fast_sparse_vector.h | 19 |
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> |