Commit 1e7659dc authored by Davis King's avatar Davis King

It occurred to me that in some cases it is very easy to end up with dense vectors

which are slightly smaller than their corresponding sparse vectors.  So I changed
the requires clause of the dot(sparse,dense) and dot(dense,sparse) functions to not
require that the dense vector's size > sparse vector's size.  Now any combination
of sizes is legal.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404181
parent 790f442b
...@@ -258,16 +258,18 @@ namespace dlib ...@@ -258,16 +258,18 @@ namespace dlib
) )
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(is_vector(b) && max_index_plus_one(a) <= (unsigned long)b.size(), DLIB_ASSERT(is_vector(b),
"\t scalar_type dot(sparse_vector a, dense_vector b)" "\t scalar_type dot(sparse_vector a, dense_vector b)"
<< "\n\t 'b' must be a vector to be used in a dot product and the sparse vector 'a'" << "\n\t 'b' must be a vector to be used in a dot product."
<< "\n\t can't be bigger that the dense vector 'b'."
); );
typedef typename T::value_type::second_type scalar_type; typedef typename T::value_type::second_type scalar_type;
typedef typename T::value_type::first_type first_type;
scalar_type sum = 0; scalar_type sum = 0;
for (typename T::const_iterator ai = a.begin(); ai != a.end(); ++ai) for (typename T::const_iterator ai = a.begin();
(ai != a.end()) && (ai->first < static_cast<first_type>(b.size()));
++ai)
{ {
sum += ai->second * b(ai->first); sum += ai->second * b(ai->first);
} }
......
...@@ -159,8 +159,6 @@ namespace dlib ...@@ -159,8 +159,6 @@ namespace dlib
requires requires
- a is a valid sparse vector (as defined at the top of this file). - a is a valid sparse vector (as defined at the top of this file).
- is_vector(b) == true - is_vector(b) == true
- max_index_plus_one(a) <= b.size()
(i.e. a can't be bigger than b)
ensures ensures
- returns the dot product between the vectors a and b - returns the dot product between the vectors a and b
!*/ !*/
...@@ -176,8 +174,6 @@ namespace dlib ...@@ -176,8 +174,6 @@ namespace dlib
requires requires
- b is a valid sparse vector (as defined at the top of this file). - b is a valid sparse vector (as defined at the top of this file).
- is_vector(a) == true - is_vector(a) == true
- max_index_plus_one(b) <= a.size()
(i.e. b can't be bigger than a)
ensures ensures
- returns the dot product between the vectors a and b - returns the dot product between the vectors a and b
!*/ !*/
......
...@@ -233,6 +233,9 @@ namespace ...@@ -233,6 +233,9 @@ namespace
DLIB_TEST(dot(dv,sv) == 5); DLIB_TEST(dot(dv,sv) == 5);
DLIB_TEST(dot(dv,dv) == 30); DLIB_TEST(dot(dv,dv) == 30);
DLIB_TEST(dot(sv,sv) == 2); DLIB_TEST(dot(sv,sv) == 2);
sv[10] = 9;
DLIB_TEST(dot(sv,dv) == 5);
} }
// test mixed sparse dense assignments // test mixed sparse dense assignments
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment