GMSparseVectorMatrix.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file GMSparseVectorMatrix.h
  3. * @brief Sparse Matrix depends on SparseVectors
  4. * @author Björn Fröhlich
  5. * @date 05/14/2009
  6. */
  7. #ifndef GMSPARSE2INCLUDE
  8. #define GMSPARSE2INCLUDE
  9. #include "GenericMatrix.h"
  10. namespace NICE
  11. {
  12. /** implementation of GenericMatrix using a sparse representation of a matrix as a collection of sparse vectors
  13. * @remark the functions in this class do not really exploit the sparseness of the involved vectors */
  14. class GMSparseVectorMatrix : public GenericMatrix
  15. {
  16. protected:
  17. //! the sparse matrix
  18. std::vector < NICE::SparseVector * >A;
  19. //! count of rows
  20. uint m_rows;
  21. //! count of columns
  22. uint m_cols;
  23. //! are the vectors new ones or come they from somewhere else
  24. bool newvectors;
  25. public:
  26. /**
  27. * simple constructor -> does nothing
  28. */
  29. GMSparseVectorMatrix ()
  30. {
  31. resize (0, 0);
  32. };
  33. /**
  34. * constructor which set the rows and cols of the SparseMatrix
  35. * @param _rows number of rows of the matrix
  36. * @param _cols number of cols of the matrix
  37. */
  38. GMSparseVectorMatrix (uint _rows, uint _cols);
  39. /**
  40. * converts a dense matrix to a sparse matrix
  41. * @param A input ICE::Matrix
  42. * @param epsilon tolerance value
  43. */
  44. GMSparseVectorMatrix (const NICE::Matrix & iceA, double epsilon = 1e-9);
  45. /**
  46. * simple destructor
  47. */
  48. ~GMSparseVectorMatrix ();
  49. /**
  50. * resize the matrix
  51. * @param _rows new count of rows
  52. * @param _cols new count of cols
  53. */
  54. void resize (int _rows, int _cols);
  55. /**
  56. * clean up
  57. */
  58. void clear ();
  59. /** get the number of rows in A */
  60. uint rows () const
  61. {
  62. return m_rows;
  63. };
  64. /** get the number of columns in A */
  65. uint cols () const
  66. {
  67. return m_cols;
  68. };
  69. /** multiply with a vector: A*x = y */
  70. void multiply (NICE::Vector & y, const NICE::Vector & x) const;
  71. /**
  72. * return the i-th row
  73. * @param i
  74. * @return SparseVector at row i
  75. */
  76. NICE::SparseVector & operator[] (int i) const;
  77. /**
  78. * restore the information of the sparse matrix
  79. * @param is inputstream
  80. * @param format
  81. */
  82. void restore (std::istream & is, int format = 0);
  83. /**
  84. * store the information of the sparse matrix
  85. * @param os outputstream
  86. * @param format
  87. */
  88. void store (std::ostream & os, int format = 0) const;
  89. /**
  90. * Should the data be deleted when the destructor is called
  91. * @param del
  92. */
  93. void setDel (bool del = true);
  94. /**
  95. * add a new row
  96. * @param x ICE::Vector
  97. */
  98. void addRow (const NICE::Vector & x);
  99. /**
  100. * add a new row
  101. * @param x SparseVector
  102. */
  103. void addRow (NICE::SparseVector * x);
  104. /**
  105. * matrixmultiplication x*y = out
  106. * @param y input
  107. * @param out output
  108. * @param transpx use the transpose of x
  109. * @param transpy use the transpose of y
  110. * @return z
  111. */
  112. void mult (GMSparseVectorMatrix & y, GMSparseVectorMatrix & out, bool transpx =
  113. false, bool transpy = false);
  114. /**
  115. * matrixmultiplication x*y = out
  116. * @param y input Sparse Vector
  117. * @param out output
  118. * @param transpx use the transpose of x
  119. * @param transpy use the transpose of y
  120. * @return z
  121. */
  122. void mult (NICE::SparseVector & y, GMSparseVectorMatrix & out, bool transpx =
  123. false, bool transpy = false);
  124. /**
  125. * same like mult, but faster when y ist very sparse and slower if not
  126. * @param y input
  127. * @param out output
  128. * @param transpx use the transpose of x
  129. * @param transpy use the transpose of y
  130. * @return z
  131. */
  132. void mult2 (GMSparseVectorMatrix & y, GMSparseVectorMatrix & out, bool transpx =
  133. false, bool transpy = false);
  134. };
  135. } // namespace
  136. #endif