Commit f35d683f authored by Davis King's avatar Davis King

Added some more stuff to the sparse vector functions.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402955
parent 41f2f3e8
......@@ -68,6 +68,61 @@ namespace dlib
return sum;
}
// ------------------------------------------------------------------------------------
template <typename T, typename U, typename V, typename W>
typename T::value_type::second_type distance_squared (
const V& a_scale,
const T& a,
const W& b_scale,
const U& b
)
{
typedef typename T::value_type::second_type scalar_type;
typedef typename U::value_type::second_type scalar_typeU;
// Both T and U must contain the same kinds of elements
COMPILE_TIME_ASSERT((is_same_type<scalar_type, scalar_typeU>::value));
typename T::const_iterator ai = a.begin();
typename U::const_iterator bi = b.begin();
scalar_type sum = 0, temp = 0;
while (ai != a.end() && bi != b.end())
{
if (ai->first == bi->first)
{
temp = a_scale*ai->second - b_scale*bi->second;
++ai;
++bi;
}
else if (ai->first < bi->first)
{
temp = a_scale*ai->second;
++ai;
}
else
{
temp = b_scale*bi->second;
++bi;
}
sum += temp*temp;
}
while (ai != a.end())
{
sum += a_scale*a_scale*ai->second*ai->second;
++ai;
}
while (bi != b.end())
{
sum += b_scale*b_scale*bi->second*bi->second;
++bi;
}
return sum;
}
// ------------------------------------------------------------------------------------
template <typename T, typename U>
......@@ -79,6 +134,19 @@ namespace dlib
return std::sqrt(distance_squared(a,b));
}
// ------------------------------------------------------------------------------------
template <typename T, typename U, typename V, typename W>
typename T::value_type::second_type distance (
const V& a_scale,
const T& a,
const W& b_scale,
const U& b
)
{
return std::sqrt(distance_squared(a_scale,a,b_scale,b));
}
// ------------------------------------------------------------------------------------
template <typename T, typename U>
......@@ -148,6 +216,20 @@ namespace dlib
return std::sqrt(length_squared(a));
}
// ------------------------------------------------------------------------------------
template <typename T, typename U>
typename T::value_type::second_type scale_by (
T& a,
const U& value
)
{
for (typename T::iterator i = a.begin(); i != a.end(); ++i)
{
i->second *= value;
}
}
}
// ----------------------------------------------------------------------------------------
......
......@@ -47,6 +47,24 @@ namespace dlib
a and b
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename V, typename W>
typename T::value_type::second_type distance_squared (
const V& a_scale,
const T& a,
const W& b_scale,
const U& b
);
/*!
requires
- a is a sorted range of std::pair objects
- b is a sorted range of std::pair objects
ensures
- returns the squared distance between the vectors
a_scale*a and b_scale*b
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
......@@ -63,6 +81,24 @@ namespace dlib
a and b. (i.e. std::sqrt(distance_squared(a,b)))
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename V, typename W>
typename T::value_type::second_type distance (
const V& a_scale,
const T& a,
const W& b_scale,
const U& b
);
/*!
requires
- a is a sorted range of std::pair objects
- b is a sorted range of std::pair objects
ensures
- returns the distance between the vectors
a_scale*a and b_scale*b. (i.e. std::sqrt(distance_squared(a_scale,a,b_scale,b)))
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
......@@ -106,6 +142,21 @@ namespace dlib
- returns std::sqrt(length_squared(a,a))
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
typename T::value_type::second_type scale_by (
T& a,
const U& value
);
/*!
requires
- a is a sorted range of std::pair objects
ensures
- #a == a*value
(i.e. multiplies every element of the vector a by value)
!*/
}
// ----------------------------------------------------------------------------------------
......
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