Commit c9a1aa4b authored by Davis King's avatar Davis King

Added more overloads of sparse_to_dense(). Now it works on single

vectors of both sparse and dense type.
parent 5730a7a2
......@@ -634,6 +634,104 @@ namespace dlib
return temp;
}
// ----------------------------------------------------------------------------------------
namespace impl
{
template <typename sparse_vector_type>
inline matrix<typename sparse_vector_type::value_type::second_type,0,1> sparse_to_dense (
const sparse_vector_type& vect,
long num_dimensions
)
{
// You must use unsigned integral key types in your sparse vectors
typedef typename sparse_vector_type::value_type::first_type idx_type;
typedef typename sparse_vector_type::value_type::second_type value_type;
COMPILE_TIME_ASSERT(is_unsigned_type<idx_type>::value);
matrix<value_type,0,1> result;
if (vect.size() == 0)
return result;
result.set_size(num_dimensions);
result = 0;
for (typename sparse_vector_type::const_iterator j = vect.begin(); j != vect.end(); ++j)
{
if (result.size() < (long)(j->first))
{
result(j->first) += j->second;
}
}
return result;
}
}
// ----------------------------------------------------------------------------------------
template <typename idx_type, typename value_type, typename alloc>
matrix<value_type,0,1> sparse_to_dense (
const std::vector<std::pair<idx_type,value_type>,alloc>& vect,
long num_dimensions
)
{
return impl::sparse_to_dense(vect,num_dimensions);
}
// ----------------------------------------------------------------------------------------
template <typename idx_type, typename value_type, typename alloc>
matrix<value_type,0,1> sparse_to_dense (
const std::vector<std::pair<idx_type,value_type>,alloc>& vect
)
{
return impl::sparse_to_dense(vect, max_index_plus_one(vect));
}
// ----------------------------------------------------------------------------------------
template <typename T1, typename T2, typename T3, typename T4>
matrix<T2,0,1> sparse_to_dense (
const std::map<T1,T2,T3,T4>& vect,
long num_dimensions
)
{
return impl::sparse_to_dense(vect,num_dimensions);
}
// ----------------------------------------------------------------------------------------
template <typename T1, typename T2, typename T3, typename T4>
matrix<T2,0,1> sparse_to_dense (
const std::map<T1,T2,T3,T4>& vect
)
{
return impl::sparse_to_dense(vect, max_index_plus_one(vect));
}
// ----------------------------------------------------------------------------------------
template <typename T>
typename enable_if<is_matrix<T>,T&>::type sparse_to_dense(
T& item
) { return item; }
template <typename EXP>
matrix<typename EXP::type,0,1> sparse_to_dense(
const matrix<EXP>& item,
long num
)
{
if (item.size() == num)
return item;
else if (item.size() < num)
return join_cols(item, zeros_matrix(num-item.size(),1));
else
return rowm(item,0,num);
}
// ----------------------------------------------------------------------------------------
template <typename sample_type, typename alloc>
......@@ -642,35 +740,19 @@ namespace dlib
)
{
typedef typename sample_type::value_type pair_type;
typedef typename basic_type<typename pair_type::first_type>::type key_type;
// You must use unsigned integral key types in your sparse vectors
COMPILE_TIME_ASSERT(is_unsigned_type<key_type>::value);
typedef typename pair_type::second_type value_type;
std::vector< matrix<value_type,0,1> > result;
// do nothing if there aren't any samples
if (samples.size() == 0)
return result;
// figure out how many elements we need in our dense vectors.
const unsigned long max_dim = max_index_plus_one(samples);
// now turn all the samples into dense samples
result.resize(samples.size());
for (unsigned long i = 0; i < samples.size(); ++i)
{
result[i].set_size(max_dim);
result[i] = 0;
for (typename sample_type::const_iterator j = samples[i].begin(); j != samples[i].end(); ++j)
{
result[i](j->first) = j->second;
}
result[i] = sparse_to_dense(samples[i],max_dim);
}
return result;
......
......@@ -361,6 +361,49 @@ namespace dlib
of 0 elements.
!*/
// ----------------------------------------------------------------------------------------
template <
typename sample_type
>
matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
const sample_type& vect
);
/*!
requires
- vect must be an unsorted sparse vector or a dense column vector.
ensures
- converts the single sparse or dense vector vect to a dense (column matrix form)
representation. That is, this function returns a vector V such that:
- V.size() == max_index_plus_one(vect)
- for all valid j:
- V(j) == The value of the j'th dimension of the vector vect. Note
that V(j) is zero if it is a sparse vector that doesn't contain an
entry for the j'th dimension.
!*/
// ----------------------------------------------------------------------------------------
template <
typename sample_type
>
matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
const sample_type& vect,
long num_dimensions
);
/*!
requires
- vect must be an unsorted sparse vector or a dense column vector.
ensures
- converts the single sparse or dense vector vect to a dense (column matrix form)
representation. That is, this function returns a vector V such that:
- V.size() == num_dimensions
- for all valid j:
- V(j) == The value of the j'th dimension of the vector vect. Note
that V(j) is zero if it is a sparse vector that doesn't contain an
entry for the j'th dimension.
!*/
// ----------------------------------------------------------------------------------------
template <
......@@ -372,23 +415,16 @@ namespace dlib
);
/*!
requires
- sample_type must be an STL container
- sample_type::value_type == std::pair<T,U> where T is some kind of
unsigned integral type
- all elements of samples must be unsorted sparse vectors or dense column vectors.
ensures
- converts from sparse sample vectors to dense (column matrix form)
- That is, this function returns a std::vector R such that:
- R contains column matrices
- R.size() == samples.size()
- for all valid i:
- R[i] == the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i]
- for all valid j:
- R[i](j) == the value of the element in samples[i] that has key
value j. That is, the key used for each element of a sparse
vector directly determines where that element gets put into a
dense vector. Note that elements not explicitly in the sparse
vector have a value of 0.
- R[i] == sparse_to_dense(samples[i], max_index_plus_one(samples))
(i.e. the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i].)
!*/
// ----------------------------------------------------------------------------------------
......
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