Commit bf43a30c authored by Davis King's avatar Davis King

Renamed assign_dense_to_sparse() to assign() and made it capable of also

assigning from sparse to sparse.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404179
parent f1d98591
......@@ -147,17 +147,18 @@ namespace dlib
return std::sqrt(distance_squared(a_scale,a,b_scale,b));
}
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
template <typename T, typename EXP>
typename enable_if<is_matrix<T> >::type assign_dense_to_sparse (
typename enable_if<is_matrix<T> >::type assign (
T& dest,
const matrix_exp<EXP>& src
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_vector(src),
"\t void assign_dense_to_sparse(dest,src)"
"\t void assign(dest,src)"
<< "\n\t the src matrix must be a row or column vector"
);
......@@ -165,14 +166,14 @@ namespace dlib
}
template <typename T, typename EXP>
typename disable_if<is_matrix<T> >::type assign_dense_to_sparse (
typename disable_if<is_matrix<T> >::type assign (
T& dest,
const matrix_exp<EXP>& src
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_vector(src),
"\t void assign_dense_to_sparse(dest,src)"
"\t void assign(dest,src)"
<< "\n\t the src matrix must be a row or column vector"
);
......@@ -185,6 +186,26 @@ namespace dlib
}
}
template <typename T, typename U>
typename disable_if_c<is_matrix<T>::value || is_matrix<U>::value>::type assign (
T& dest, // sparse
const U& src // sparse
)
{
dest.assign(src.begin(), src.end());
}
template <typename T, typename U, typename Comp, typename Alloc, typename S>
typename disable_if<is_matrix<S> >::type assign (
std::map<T,U,Comp,Alloc>& dest, // sparse
const S& src // sparse
)
{
dest.clear();
dest.insert(src.begin(), src.end());
}
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
template <typename T>
......
......@@ -115,27 +115,25 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <typename T, typename EXP>
void assign_dense_to_sparse (
template <typename T, typename U>
void assign (
T& dest,
const matrix_exp<EXP>& src
const U& src
);
/*!
requires
- is_vector(src) == true
- the key type in T is capable of holding integers or T is a dlib::matrix
capable of storing src
- dest == a sparse vector or a dense vector
- src == a sparse vector or a dense vector
- dest is not dense when src is sparse
(i.e. you can't assign a sparse vector to a dense vector. This is
because we don't know what the proper dimensionality should be for the
dense vector)
ensures
- if (T is a dlib::matrix) then
- #dest == src
(if dest is just a normal matrix then this function just does a normal copy)
- else
- dest is a sparse vector and this function copies src into it. The
assignment is performed such that the following is true:
for all i: if (src(i) != 0) then make_pair(i, src(i)) is an element of #dest
- #dest will be properly sorted
- #src represents the same vector as dest.
(conversion between sparse/dense formats is done automatically)
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
......
......@@ -531,7 +531,7 @@ namespace dlib
// As an aside, the reason for using max_index_plus_one() and not just w.size()-1 is because
// doing it this way avoids an inane warning from gcc that can occur in some cases.
const long out_size = sparse_vector::max_index_plus_one(x);
sparse_vector::assign_dense_to_sparse(df.basis_vectors(0), matrix_cast<scalar_type>(colm(w, 0, out_size)));
sparse_vector::assign(df.basis_vectors(0), matrix_cast<scalar_type>(colm(w, 0, out_size)));
df.alpha.set_size(1);
df.alpha(0) = 1;
......
......@@ -163,23 +163,6 @@ namespace
DLIB_TEST(m(1) == 1 - 2*samples[3][1].second);
DLIB_TEST(m(2) == 1);
// test mixed sparse and dense dot products
{
std::map<unsigned int, double> sv;
matrix<double,0,1> dv(4);
dv = 1,2,3,4;
sv[0] = 1;
sv[3] = 1;
using namespace sparse_vector;
DLIB_TEST(dot(sv,dv) == 5);
DLIB_TEST(dot(dv,sv) == 5);
DLIB_TEST(dot(dv,dv) == 30);
DLIB_TEST(dot(sv,sv) == 2);
}
}
// ----------------------------------------------------------------------------------------
......@@ -233,6 +216,94 @@ namespace
{
test_dense();
test_sparse();
// test mixed sparse and dense dot products
{
std::map<unsigned int, double> sv;
matrix<double,0,1> dv(4);
dv = 1,2,3,4;
sv[0] = 1;
sv[3] = 1;
using namespace sparse_vector;
DLIB_TEST(dot(sv,dv) == 5);
DLIB_TEST(dot(dv,sv) == 5);
DLIB_TEST(dot(dv,dv) == 30);
DLIB_TEST(dot(sv,sv) == 2);
}
// test mixed sparse dense assignments
{
std::map<unsigned int, double> sv, sv2;
std::vector<std::pair<unsigned int, double> > sv3;
matrix<double,0,1> dv(4), dv2;
dv = 1,2,3,4;
sv[0] = 1;
sv[3] = 1;
using namespace sparse_vector;
assign(dv2, dv);
DLIB_TEST(dv2.size() == 4);
DLIB_TEST(dv2(0) == 1);
DLIB_TEST(dv2(1) == 2);
DLIB_TEST(dv2(2) == 3);
DLIB_TEST(dv2(3) == 4);
assign(sv2, dv);
DLIB_TEST(sv2.size() == 4);
DLIB_TEST(sv2[0] == 1);
DLIB_TEST(sv2[1] == 2);
DLIB_TEST(sv2[2] == 3);
DLIB_TEST(sv2[3] == 4);
assign(sv2, sv);
DLIB_TEST(sv2.size() == 2);
DLIB_TEST(sv2[0] == 1);
DLIB_TEST(sv2[1] == 0);
DLIB_TEST(sv2[2] == 0);
DLIB_TEST(sv2[3] == 1);
assign(sv3, sv);
DLIB_TEST(sv3.size() == 2);
DLIB_TEST(sv3[0].second == 1);
DLIB_TEST(sv3[1].second == 1);
DLIB_TEST(sv3[0].first == 0);
DLIB_TEST(sv3[1].first == 3);
assign(sv3, dv);
DLIB_TEST(sv3.size() == 4);
DLIB_TEST(sv3[0].second == 1);
DLIB_TEST(sv3[1].second == 2);
DLIB_TEST(sv3[2].second == 3);
DLIB_TEST(sv3[3].second == 4);
DLIB_TEST(sv3[0].first == 0);
DLIB_TEST(sv3[1].first == 1);
DLIB_TEST(sv3[2].first == 2);
DLIB_TEST(sv3[3].first == 3);
assign(sv3, sv);
DLIB_TEST(sv3.size() == 2);
DLIB_TEST(sv3[0].second == 1);
DLIB_TEST(sv3[1].second == 1);
DLIB_TEST(sv3[0].first == 0);
DLIB_TEST(sv3[1].first == 3);
sv.clear();
assign(sv, sv3);
DLIB_TEST(sv.size() == 2);
DLIB_TEST(sv[0] == 1);
DLIB_TEST(sv[1] == 0);
DLIB_TEST(sv[2] == 0);
DLIB_TEST(sv[3] == 1);
}
}
} a;
......
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