diff --git a/dlib/svm/sparse_vector.h b/dlib/svm/sparse_vector.h index f1146a62b..582b11459 100644 --- a/dlib/svm/sparse_vector.h +++ b/dlib/svm/sparse_vector.h @@ -7,6 +7,8 @@ #include #include #include "../algs.h" +#include +#include namespace dlib @@ -216,37 +218,67 @@ namespace dlib // ------------------------------------------------------------------------------------ + namespace impl + { + template + typename T::value_type::second_type dot ( + const T& a, + const U& b + ) + { + typedef typename T::value_type::second_type scalar_type; + + typename T::const_iterator ai = a.begin(); + typename U::const_iterator bi = b.begin(); + + scalar_type sum = 0; + while (ai != a.end() && bi != b.end()) + { + if (ai->first == bi->first) + { + sum += ai->second * bi->second; + ++ai; + ++bi; + } + else if (ai->first < bi->first) + { + ++ai; + } + else + { + ++bi; + } + } + + return sum; + } + } + template - typename T::value_type::second_type dot ( + inline typename T::value_type::second_type dot ( const T& a, const T& b ) { - typedef typename T::value_type::second_type scalar_type; + return dlib::sparse_vector::impl::dot(a,b); + } - typename T::const_iterator ai = a.begin(); - typename T::const_iterator bi = b.begin(); + template + inline T4 dot ( + const std::vector& a, + const std::map& b + ) + { + return dlib::sparse_vector::impl::dot(a,b); + } - scalar_type sum = 0; - while (ai != a.end() && bi != b.end()) - { - if (ai->first == bi->first) - { - sum += ai->second * bi->second; - ++ai; - ++bi; - } - else if (ai->first < bi->first) - { - ++ai; - } - else - { - ++bi; - } - } - - return sum; + template + inline T4 dot ( + const std::map& a, + const std::vector& b + ) + { + return dlib::sparse_vector::impl::dot(a,b); } // ------------------------------------------------------------------------------------ diff --git a/dlib/svm/sparse_vector_abstract.h b/dlib/svm/sparse_vector_abstract.h index d0f4a7865..3ec65734c 100644 --- a/dlib/svm/sparse_vector_abstract.h +++ b/dlib/svm/sparse_vector_abstract.h @@ -6,6 +6,8 @@ #include #include "../algs.h" #include "../serialize.h" +#include +#include namespace dlib { @@ -164,6 +166,30 @@ namespace dlib - returns the dot product between the vectors a and b !*/ + template + T4 dot ( + const std::vector& a, + const std::map& b + ); + /*! + requires + - a and b are sparse vectors + ensures + - returns the dot product between the vectors a and b + !*/ + + template + T4 dot ( + const std::map& a, + const std::vector& b + ); + /*! + requires + - a and b are sparse vectors + ensures + - returns the dot product between the vectors a and b + !*/ + // ---------------------------------------------------------------------------------------- template