Commit 2e7e20f2 authored by Davis King's avatar Davis King

- Added make_sparse_vector()

 - Refined the sparse_to_dense() routines a little.
parent ad3ede2d
...@@ -641,7 +641,7 @@ namespace dlib ...@@ -641,7 +641,7 @@ namespace dlib
template <typename sparse_vector_type> template <typename sparse_vector_type>
inline matrix<typename sparse_vector_type::value_type::second_type,0,1> sparse_to_dense ( inline matrix<typename sparse_vector_type::value_type::second_type,0,1> sparse_to_dense (
const sparse_vector_type& vect, const sparse_vector_type& vect,
long num_dimensions unsigned long num_dimensions
) )
{ {
// You must use unsigned integral key types in your sparse vectors // You must use unsigned integral key types in your sparse vectors
...@@ -674,7 +674,7 @@ namespace dlib ...@@ -674,7 +674,7 @@ namespace dlib
template <typename idx_type, typename value_type, typename alloc> template <typename idx_type, typename value_type, typename alloc>
matrix<value_type,0,1> sparse_to_dense ( matrix<value_type,0,1> sparse_to_dense (
const std::vector<std::pair<idx_type,value_type>,alloc>& vect, const std::vector<std::pair<idx_type,value_type>,alloc>& vect,
long num_dimensions unsigned long num_dimensions
) )
{ {
return impl::sparse_to_dense(vect,num_dimensions); return impl::sparse_to_dense(vect,num_dimensions);
...@@ -695,7 +695,7 @@ namespace dlib ...@@ -695,7 +695,7 @@ namespace dlib
template <typename T1, typename T2, typename T3, typename T4> template <typename T1, typename T2, typename T3, typename T4>
matrix<T2,0,1> sparse_to_dense ( matrix<T2,0,1> sparse_to_dense (
const std::map<T1,T2,T3,T4>& vect, const std::map<T1,T2,T3,T4>& vect,
long num_dimensions unsigned long num_dimensions
) )
{ {
return impl::sparse_to_dense(vect,num_dimensions); return impl::sparse_to_dense(vect,num_dimensions);
...@@ -720,23 +720,25 @@ namespace dlib ...@@ -720,23 +720,25 @@ namespace dlib
template <typename EXP> template <typename EXP>
matrix<typename EXP::type,0,1> sparse_to_dense( matrix<typename EXP::type,0,1> sparse_to_dense(
const matrix<EXP>& item, const matrix_exp<EXP>& item,
long num unsigned long num
) )
{ {
if (item.size() == num) typedef typename EXP::type type;
if (item.size() == (long)num)
return item; return item;
else if (item.size() < num) else if (item.size() < (long)num)
return join_cols(item, zeros_matrix(num-item.size(),1)); return join_cols(item, zeros_matrix<type>((long)num-item.size(),1));
else else
return rowm(item,0,num); return colm(item,0,(long)num);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <typename sample_type, typename alloc> template <typename sample_type, typename alloc>
std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense ( std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
const std::vector<sample_type, alloc>& samples const std::vector<sample_type, alloc>& samples,
unsigned long num_dimensions
) )
{ {
typedef typename sample_type::value_type pair_type; typedef typename sample_type::value_type pair_type;
...@@ -744,20 +746,49 @@ namespace dlib ...@@ -744,20 +746,49 @@ namespace dlib
std::vector< matrix<value_type,0,1> > result; std::vector< matrix<value_type,0,1> > 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 // now turn all the samples into dense samples
result.resize(samples.size()); result.resize(samples.size());
for (unsigned long i = 0; i < samples.size(); ++i) for (unsigned long i = 0; i < samples.size(); ++i)
{ {
result[i] = sparse_to_dense(samples[i],max_dim); result[i] = sparse_to_dense(samples[i],num_dimensions);
} }
return result; return result;
} }
// ----------------------------------------------------------------------------------------
template <typename sample_type, typename alloc>
std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
const std::vector<sample_type, alloc>& samples
)
{
return sparse_to_dense(samples, max_index_plus_one(samples));
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
T make_sparse_vector (
const T& v
)
{
// You must use unsigned integral key types in your sparse vectors
typedef typename T::value_type::first_type idx_type;
typedef typename T::value_type::second_type value_type;
COMPILE_TIME_ASSERT(is_unsigned_type<idx_type>::value);
std::map<idx_type,value_type> temp;
for (typename T::const_iterator i = v.begin(); i != v.end(); ++i)
{
temp[i->first] += i->second;
}
return T(temp.begin(), temp.end());
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -371,7 +371,7 @@ namespace dlib ...@@ -371,7 +371,7 @@ namespace dlib
); );
/*! /*!
requires requires
- vect must be an unsorted sparse vector or a dense column vector. - vect must be a sparse vector or a dense column vector.
ensures ensures
- converts the single sparse or dense vector vect to a dense (column matrix form) - 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: representation. That is, this function returns a vector V such that:
...@@ -389,11 +389,11 @@ namespace dlib ...@@ -389,11 +389,11 @@ namespace dlib
> >
matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense ( matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
const sample_type& vect, const sample_type& vect,
long num_dimensions unsigned long num_dimensions
); );
/*! /*!
requires requires
- vect must be an unsorted sparse vector or a dense column vector. - vect must be a sparse vector or a dense column vector.
ensures ensures
- converts the single sparse or dense vector vect to a dense (column matrix form) - 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: representation. That is, this function returns a vector V such that:
...@@ -415,7 +415,7 @@ namespace dlib ...@@ -415,7 +415,7 @@ namespace dlib
); );
/*! /*!
requires requires
- all elements of samples must be unsorted sparse vectors or dense column vectors. - all elements of samples must be sparse vectors or dense column vectors.
ensures ensures
- converts from sparse sample vectors to dense (column matrix form) - converts from sparse sample vectors to dense (column matrix form)
- That is, this function returns a std::vector R such that: - That is, this function returns a std::vector R such that:
...@@ -427,6 +427,47 @@ namespace dlib ...@@ -427,6 +427,47 @@ namespace dlib
given by samples[i].) given by samples[i].)
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename alloc
>
std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
const std::vector<sample_type, alloc>& samples,
unsigned long num_dimensions
);
/*!
requires
- all elements of samples must be 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] == sparse_to_dense(samples[i], num_dimensions)
(i.e. the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i].)
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
T make_sparse_vector (
const T& v
);
/*!
requires
- v is an unsorted sparse vector
ensures
- returns a copy of v which is a sparse vector.
(i.e. it will be properly sorted and not have any duplicate
key values).
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
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