GenericMatrix.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file GenericMatrix.h
  3. * @brief matrix interface for indirect matrix multiplications
  4. * @author Erik Rodner
  5. * @date 05/14/2009
  6. */
  7. #ifndef GENERICMATRIXINCLUDE
  8. #define GENERICMATRIXINCLUDE
  9. #include "core/basics/triplet.h"
  10. #include "core/vector/SparseVectorT.h"
  11. namespace NICE
  12. {
  13. /** matrix interface for indirect matrix multiplications */
  14. class GenericMatrix
  15. {
  16. public:
  17. /** multiply with a vector: A*x = y */
  18. virtual void multiply ( NICE::Vector & y, const NICE::Vector & x ) const = 0;
  19. /** get the number of rows in A */
  20. virtual uint rows () const = 0;
  21. /** get the number of columns in A */
  22. virtual uint cols () const = 0;
  23. /** simple destructor */
  24. virtual ~ GenericMatrix ()
  25. {
  26. };
  27. };
  28. /** sparse matrix vector multiplication */
  29. class GMSparse : public GenericMatrix
  30. {
  31. protected:
  32. // our representation of sparse matrices
  33. std::vector < NICE::triplet < int, int, double > > A;
  34. // we also have to store the size of the matrix
  35. uint m_rows;
  36. uint m_cols;
  37. public:
  38. /**
  39. * @brief empty constructor only initializing the size
  40. *
  41. * @param _rows number of rows of the matrix
  42. * @param _cols number of cols of the matrix
  43. */
  44. GMSparse ( uint _rows, uint _cols ) : m_rows ( _rows ), m_cols ( _cols )
  45. {
  46. }
  47. /**
  48. * @brief initialize the sparse structure with a dense matrix and a given
  49. * sparseness threshold
  50. *
  51. * @param A input matrix
  52. * @param epsilon if fabs(x) < epsilon, x is considered as zero
  53. */
  54. GMSparse ( const NICE::Matrix & A, double epsilon = 1e-9 );
  55. /** get the number of rows in A */
  56. uint rows () const
  57. {
  58. return m_rows;
  59. };
  60. /** get the number of columns in A */
  61. uint cols () const
  62. {
  63. return m_cols;
  64. };
  65. /** multiply with a vector: A*x = y */
  66. void multiply ( NICE::Vector & y, const NICE::Vector & x ) const;
  67. };
  68. /** implicit representation of a covariance matrix */
  69. class GMCovariance: public GenericMatrix
  70. {
  71. protected:
  72. // our data vectors stored in a matrix
  73. const NICE::Matrix *data;
  74. public:
  75. GMCovariance ( const NICE::Matrix * data );
  76. /** get the number of rows in A */
  77. uint rows () const
  78. {
  79. return data->rows();
  80. };
  81. /** get the number of columns in A */
  82. uint cols () const
  83. {
  84. return data->rows();
  85. };
  86. /** multiply with a vector: A*x = y */
  87. void multiply ( NICE::Vector & y, const NICE::Vector & x ) const;
  88. };
  89. } // namespace
  90. #endif