summaryrefslogtreecommitdiff
path: root/decoder/sparse_vector.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-13 02:01:07 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-13 02:01:07 +0000
commit2d6a5896df99904b0c492e77168489636d545869 (patch)
treebebf9b97cddbee40a95c026d1608c0cccf6bb49f /decoder/sparse_vector.h
parent77c25d9f30f95ccb7843f9dce71a4f4e018cc727 (diff)
vest: combine over-similar search directions, exclude primary directions, skeleton for oracle directions
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@227 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/sparse_vector.h')
-rw-r--r--decoder/sparse_vector.h32
1 files changed, 24 insertions, 8 deletions
diff --git a/decoder/sparse_vector.h b/decoder/sparse_vector.h
index be91f324..bfdeebcc 100644
--- a/decoder/sparse_vector.h
+++ b/decoder/sparse_vector.h
@@ -58,7 +58,7 @@ public:
}
int max_index() const {
- if (values_.empty()) return 0;
+ if (empty()) return 0;
typename MapType::const_iterator found =values_.end();
--found;
return found->first;
@@ -75,6 +75,18 @@ public:
}
template<typename S>
+ S cosine_sim(const SparseVector<S> &vec) const {
+ return dot(vec)/(l2norm()*vec.l2norm());
+ }
+
+ // if values are binary, gives |A intersect B|/|A union B|
+ template<typename S>
+ S tanimoto_coef(const SparseVector<S> &vec) const {
+ S dp=dot(vec);
+ return dp/(l2norm_sq()*vec.l2norm_sq()-dp);
+ }
+
+ template<typename S>
S dot(const SparseVector<S> &vec) const {
S sum = 0;
for (typename MapType::const_iterator
@@ -119,12 +131,16 @@ public:
return sum;
}
- T l2norm() const {
+ T l2norm_sq() const {
T sum = 0;
for (typename MapType::const_iterator
it = values_.begin(); it != values_.end(); ++it)
sum += it->second * it->second;
- return sqrt(sum);
+ return sum;
+ }
+
+ T l2norm() const {
+ return sqrt(l2norm_sq());
}
SparseVector<T> &operator+=(const SparseVector<T> &other) {
@@ -149,14 +165,14 @@ public:
return *this;
}
- SparseVector<T> &operator-=(const double &x) {
+ SparseVector<T> &operator-=(T const& x) {
for (typename MapType::iterator
it = values_.begin(); it != values_.end(); ++it)
it->second -= x;
return *this;
}
- SparseVector<T> &operator+=(const double &x) {
+ SparseVector<T> &operator+=(T const& x) {
for (typename MapType::iterator
it = values_.begin(); it != values_.end(); ++it)
it->second += x;
@@ -177,17 +193,17 @@ public:
return *this;
}
- SparseVector<T> operator+(const double &x) const {
+ SparseVector<T> operator+(T const& x) const {
SparseVector<T> result = *this;
return result += x;
}
- SparseVector<T> operator-(const double &x) const {
+ SparseVector<T> operator-(T const& x) const {
SparseVector<T> result = *this;
return result -= x;
}
- SparseVector<T> operator/(const double &x) const {
+ SparseVector<T> operator/(T const& x) const {
SparseVector<T> result = *this;
return result /= x;
}