SparseVectorT.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. int 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, V v );
  37. SparseVectorT ( const std::map<I, V> & mymap );
  38. /**
  39. * simple constructor -> does nothing
  40. */
  41. SparseVectorT ():dim(-1) {}
  42. /**
  43. * Constructor with the dimension
  44. * @param _dim dimension of the SparseVector
  45. */
  46. SparseVectorT ( int _dim );
  47. /**
  48. * converts a ICE::Vector to a SparseVector with a tolerance factor
  49. * @param v input ICE::Vector
  50. * @param tolerance tolerance value
  51. */
  52. SparseVectorT ( const NICE::Vector &v, double tolerance = 1e-15 );
  53. /**
  54. * simple destructor
  55. */
  56. virtual ~SparseVectorT () {};
  57. /**
  58. * add the elements of a SparseVector to this SparseVector
  59. * @param v input SparseVector
  60. */
  61. void add ( const SparseVectorT<I,V> & v );
  62. /**
  63. * add the elements of a SparseVector to this NICE::Vector and multiply each element with a constant lambda
  64. * @param v
  65. * @param lambda
  66. */
  67. void add ( const SparseVectorT<I,V> & v, double lambda );
  68. /**
  69. * add to each element a constant
  70. * @param value
  71. */
  72. void add ( V value );
  73. /**
  74. * sub the elements of a SparseVector from this SparseVector
  75. * @param v input SparseVector
  76. */
  77. void sub ( const SparseVectorT<I,V> & v );
  78. /** add a sparse vector given as a STL map with integer values multiplied with a scalar to the current vector */
  79. void addMap ( const std::map<int, int> & v, double lambda = 1.0 );
  80. /** add a sparse vector given as a STL map with double values multiplied with a scalar to the current vector */
  81. void addMap ( const std::map<int, double> & v, double lambda = 1.0 );
  82. /** normalize, such that all elements sum to one */
  83. void normalize ();
  84. /** normalize in given interval between maxv and minv */
  85. void normalize (V minv, V maxv);
  86. /** read from a stream */
  87. void restore ( std::istream & is, int format = 0 );
  88. /** write to a stream */
  89. void store ( std::ostream & os, int format = 0 ) const;
  90. /** clear the data of the vector */
  91. void clear ();
  92. /** calculate the entropy of the vector */
  93. double entropy () const;
  94. /**
  95. * each element of the SparseVector is multiplied by the elements of the input SparseVector
  96. * @param v input SparseVector
  97. */
  98. void multiply ( const SparseVectorT<I,V> & v );
  99. /**
  100. * each element of the SparseVector is multiplied by a constant value
  101. * @param val factor
  102. */
  103. void multiply ( V val );
  104. /**
  105. * each element of the SparseVector is divided by the elements of the input SparseVector
  106. * @param v input SparseVector
  107. */
  108. void divide ( const SparseVectorT<I,V> & v );
  109. /**
  110. * each element of the SparseVector is divided by a constant value
  111. * @param v divisor
  112. */
  113. void divide ( V v );
  114. /**
  115. * computes the inner product of two SparseVectors
  116. * @param v the second sparse vector
  117. * @return inner product
  118. */
  119. double innerProduct ( const SparseVectorT<I,V> & v ) const;
  120. /** get the sum of all elements */
  121. V sum () const;
  122. /** get the maximum of all non-zero elements */
  123. V max () const;
  124. /** get the minimum of all non-zero elements */
  125. V min () const;
  126. /** get the index of the element with maximum value */
  127. I maxElement () const;
  128. /** get the index of the element with maximum value but do not consider a specific element */
  129. I maxElementExclusive ( I key ) const;
  130. /** get all indices of elements with non-zero values as a sorted STL vector */
  131. void getSortedIndices ( std::vector<I> & indizes ) const;
  132. /** get an element */
  133. V get ( I i ) const;
  134. /** set an element */
  135. bool set ( I i , V newValue );
  136. /**
  137. * set the dimension of the SparseVector
  138. * @param _dim
  139. */
  140. void setDim ( int _dim );
  141. /**
  142. * returns the dimension of the SparseVector
  143. * @return dimension
  144. */
  145. int getDim() const;
  146. /**
  147. * @brief calculate the value of the minimum kernel
  148. *
  149. * @param b 2nd argument of the minimum kernel, which is symmetric
  150. * @return resulting value of the minimum kernel
  151. */
  152. double minimumKernel ( const SparseVectorT<I,V> & b ) const;
  153. /** pick a random index by interpreting the elements of the vector as
  154. an unnormalized multinomial distribution */
  155. I pickRandomSample() const;
  156. /**
  157. * @brief computes a full NICE::VectorT from the current SparseVectorT object. the dimension has to be set properly for this method!
  158. * @param v resulting NICE::VectorT
  159. */
  160. void convertToVectorT(NICE::VectorT<V> & v ) const ;
  161. };
  162. typedef SparseVectorT<long, double> SparseVectorLong;
  163. typedef SparseVectorT<int, double> SparseVectorInt;
  164. typedef SparseVectorT<short, double> SparseVector;
  165. } // namespace
  166. #include "core/vector/SparseVectorT.tcc"
  167. #endif