| 843 | { |
| 844 | template <typename sparse_vector_type> |
| 845 | inline matrix<typename sparse_vector_type::value_type::second_type,0,1> sparse_to_dense ( |
| 846 | const sparse_vector_type& vect, |
| 847 | unsigned long num_dimensions |
| 848 | ) |
| 849 | { |
| 850 | // You must use unsigned integral key types in your sparse vectors |
| 851 | typedef typename sparse_vector_type::value_type::first_type idx_type; |
| 852 | typedef typename sparse_vector_type::value_type::second_type value_type; |
| 853 | COMPILE_TIME_ASSERT(is_unsigned_type<idx_type>::value); |
| 854 | |
| 855 | matrix<value_type,0,1> result; |
| 856 | |
| 857 | if (vect.size() == 0) |
| 858 | return result; |
| 859 | |
| 860 | result.set_size(num_dimensions); |
| 861 | result = 0; |
| 862 | |
| 863 | for (typename sparse_vector_type::const_iterator j = vect.begin(); j != vect.end(); ++j) |
| 864 | { |
| 865 | if ((long)(j->first) < result.size()) |
| 866 | { |
| 867 | result(j->first) += j->second; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return result; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // ---------------------------------------------------------------------------------------- |