SparseVectorT.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file SparseVectorT.h
  3. * @brief sparse vector template class
  4. * @author Erik Rodner and Björn Fröhlich
  5. * @date 06.03.2012
  6. */
  7. #ifndef SPARSEVECTORTINCLUDE
  8. #define SPARSEVECTORTINCLUDE
  9. #include <vector>
  10. #include <map>
  11. #include <iostream>
  12. #include "core/basics/Persistent.h"
  13. #include "core/vector/VectorT.h"
  14. namespace NICE {
  15. /** sparse vector template*/
  16. template<class I, class V>
  17. class SparseVectorT : public std::map<I, V>, public Persistent
  18. {
  19. public:
  20. //! input/ouput formats
  21. enum {
  22. // SVECTOR dimension size index value index value ... END
  23. FORMAT_INDEX = 0,
  24. // index:value index:value \n
  25. FORMAT_INDEX_LINE = -9999
  26. };
  27. protected:
  28. //! dimension of the SparseVector, not properly implemented! FIXME
  29. uint dim;
  30. public:
  31. /**
  32. * constructor which add one element to the SparseVector
  33. * @param k position of the value
  34. * @param v value
  35. */
  36. SparseVectorT ( I _k,
  37. V _v
  38. );
  39. SparseVectorT ( const std::map<I, V> & _mymap );
  40. /**
  41. * simple constructor -> does nothing
  42. */
  43. SparseVectorT ():dim(0) {}
  44. /**
  45. * Constructor with the dimension
  46. * @param _dim dimension of the SparseVector
  47. */
  48. SparseVectorT ( uint _dim );
  49. /**
  50. * converts a ICE::Vector to a SparseVector with a tolerance factor
  51. * @param v input ICE::Vector
  52. * @param tolerance tolerance value
  53. */
  54. SparseVectorT ( const NICE::Vector & _v,
  55. double _tolerance = 1e-15
  56. );
  57. /**
  58. * simple destructor
  59. */
  60. virtual ~SparseVectorT () {};
  61. /**
  62. * add the elements of a SparseVector to this SparseVector
  63. * @param v input SparseVector
  64. */
  65. void add ( const SparseVectorT<I,V> & _v );
  66. /**
  67. * add the elements of a SparseVector to this NICE::Vector and multiply each element with a constant lambda
  68. * @param v
  69. * @param lambda
  70. */
  71. void add ( const SparseVectorT<I,V> & _v,
  72. double _lambda
  73. );
  74. /**
  75. * add to each element a constant
  76. * @param value
  77. */
  78. void add ( V _value );
  79. /**
  80. * sub the elements of a SparseVector from this SparseVector
  81. * @param v input SparseVector
  82. */
  83. void sub ( const SparseVectorT<I,V> & _v );
  84. /** add a sparse vector given as a STL map with integer values multiplied with a scalar to the current vector */
  85. void addMap ( const std::map<uint, int> & _v,
  86. double _lambda = 1.0
  87. );
  88. /** add a sparse vector given as a STL map with double values multiplied with a scalar to the current vector */
  89. void addMap ( const std::map<uint, double> & _v,
  90. double _lambda = 1.0
  91. );
  92. /** normalize, such that all elements sum to one */
  93. void normalize ();
  94. /** normalize in given interval between maxv and minv */
  95. void normalize (V _minv,
  96. V _maxv
  97. );
  98. /** read from a stream */
  99. void restore ( std::istream & _is,
  100. int _format = 0
  101. );
  102. /** write to a stream */
  103. void store ( std::ostream & _os,
  104. int _format = 0
  105. ) const;
  106. /** clear the data of the vector */
  107. void clear ();
  108. /** calculate the entropy of the vector */
  109. double entropy () const;
  110. /**
  111. * each element of the SparseVector is multiplied by the elements of the input SparseVector
  112. * @param v input SparseVector
  113. */
  114. void multiply ( const SparseVectorT<I,V> & _v );
  115. /**
  116. * each element of the SparseVector is multiplied by a constant value
  117. * @param val factor
  118. */
  119. void multiply ( V _val );
  120. /**
  121. * each element of the SparseVector is divided by the elements of the input SparseVector
  122. * @param v input SparseVector
  123. */
  124. void divide ( const SparseVectorT<I,V> & _v );
  125. /**
  126. * each element of the SparseVector is divided by a constant value
  127. * @param v divisor
  128. */
  129. void divide ( V _v );
  130. /**
  131. * computes the inner product of two SparseVectors
  132. * @param v the second sparse vector
  133. * @return inner product
  134. */
  135. double innerProduct ( const SparseVectorT<I,V> & _v ) const;
  136. /** get the sum of all elements */
  137. V sum () const;
  138. /** get the maximum of all non-zero elements */
  139. V max () const;
  140. /** get the minimum of all non-zero elements */
  141. V min () const;
  142. /** get the index of the element with maximum value */
  143. I maxElement () const;
  144. /** get the index of the element with maximum value but do not consider a specific element */
  145. I maxElementExclusive ( I key ) const;
  146. /** get all indices of elements with non-zero values as a sorted STL vector */
  147. void getSortedIndices ( std::vector<I> & _indizes ) const;
  148. /** get an element */
  149. V get ( I _i ) const;
  150. /** set an element */
  151. bool set ( I _i ,
  152. V _newValue
  153. );
  154. /**
  155. * set the dimension of the SparseVector
  156. * @param _dim
  157. */
  158. void setDim ( uint _dim );
  159. /**
  160. * returns the dimension of the SparseVector
  161. * @return dimension
  162. */
  163. uint getDim() const;
  164. /**
  165. * @brief calculate the value of the minimum kernel
  166. *
  167. * @param b 2nd argument of the minimum kernel, which is symmetric
  168. * @return resulting value of the minimum kernel
  169. */
  170. double minimumKernel ( const SparseVectorT<I,V> & _b ) const;
  171. /** pick a random index by interpreting the elements of the vector as
  172. an unnormalized multinomial distribution */
  173. I pickRandomSample() const;
  174. /**
  175. * @brief computes a full NICE::VectorT from the current SparseVectorT object. the dimension has to be set properly for this method!
  176. * @param v resulting NICE::VectorT
  177. */
  178. void convertToVectorT(NICE::VectorT<V> & _v ) const ;
  179. };
  180. typedef SparseVectorT<unsigned long, double> SparseVectorLong;
  181. typedef SparseVectorT<unsigned int, double> SparseVectorInt;
  182. typedef SparseVectorT<unsigned short, double> SparseVector;
  183. } // namespace
  184. #include "core/vector/SparseVectorT.tcc"
  185. #endif