MapMat_bones.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup MapMat
  16. //! @{
  17. // this class is for internal use only; subject to change and/or removal without notice
  18. template<typename eT>
  19. class MapMat
  20. {
  21. public:
  22. typedef eT elem_type; //!< the type of elements stored in the matrix
  23. typedef typename get_pod_type<eT>::result pod_type; //!< if eT is std::complex<T>, pod_type is T; otherwise pod_type is eT
  24. static const bool is_row = false;
  25. static const bool is_col = false;
  26. static const bool is_xvec = false;
  27. const uword n_rows; //!< number of rows (read-only)
  28. const uword n_cols; //!< number of columns (read-only)
  29. const uword n_elem; //!< number of elements (read-only)
  30. private:
  31. typedef typename std::map<uword, eT> map_type;
  32. arma_aligned map_type* map_ptr;
  33. public:
  34. inline ~MapMat();
  35. inline MapMat();
  36. inline explicit MapMat(const uword in_n_rows, const uword in_n_cols);
  37. inline explicit MapMat(const SizeMat& s);
  38. inline MapMat(const MapMat<eT>& x);
  39. inline void operator=(const MapMat<eT>& x);
  40. inline explicit MapMat(const SpMat<eT>& x);
  41. inline void operator=(const SpMat<eT>& x);
  42. #if defined(ARMA_USE_CXX11)
  43. inline MapMat(MapMat<eT>&& x);
  44. inline void operator=(MapMat<eT>&& x);
  45. #endif
  46. inline void reset();
  47. inline void set_size(const uword in_n_rows);
  48. inline void set_size(const uword in_n_rows, const uword in_n_cols);
  49. inline void set_size(const SizeMat& s);
  50. inline void zeros();
  51. inline void zeros(const uword in_n_rows);
  52. inline void zeros(const uword in_n_rows, const uword in_n_cols);
  53. inline void zeros(const SizeMat& s);
  54. inline void eye();
  55. inline void eye(const uword in_n_rows, const uword in_n_cols);
  56. inline void eye(const SizeMat& s);
  57. inline void speye();
  58. inline void speye(const uword in_n_rows, const uword in_n_cols);
  59. inline void speye(const SizeMat& s);
  60. arma_inline arma_warn_unused MapMat_val<eT> operator[](const uword index);
  61. inline arma_warn_unused eT operator[](const uword index) const;
  62. arma_inline arma_warn_unused MapMat_val<eT> operator()(const uword index);
  63. inline arma_warn_unused eT operator()(const uword index) const;
  64. arma_inline arma_warn_unused MapMat_val<eT> at(const uword in_row, const uword in_col);
  65. inline arma_warn_unused eT at(const uword in_row, const uword in_col) const;
  66. arma_inline arma_warn_unused MapMat_val<eT> operator()(const uword in_row, const uword in_col);
  67. inline arma_warn_unused eT operator()(const uword in_row, const uword in_col) const;
  68. inline arma_warn_unused bool is_empty() const;
  69. inline arma_warn_unused bool is_vec() const;
  70. inline arma_warn_unused bool is_rowvec() const;
  71. inline arma_warn_unused bool is_colvec() const;
  72. inline arma_warn_unused bool is_square() const;
  73. inline void sprandu(const uword in_n_rows, const uword in_n_cols, const double density);
  74. inline void print(const std::string& extra_text) const;
  75. inline uword get_n_nonzero() const;
  76. inline void get_locval_format(umat& locs, Col<eT>& vals) const;
  77. private:
  78. inline void init_cold();
  79. inline void init_warm(const uword in_n_rows, const uword in_n_cols);
  80. arma_inline void set_val(const uword index, const eT& in_val);
  81. inline void erase_val(const uword index);
  82. friend class SpMat<eT>;
  83. friend class MapMat_val<eT>;
  84. friend class SpMat_MapMat_val<eT>;
  85. friend class SpSubview_MapMat_val<eT>;
  86. };
  87. template<typename eT>
  88. class MapMat_val
  89. {
  90. private:
  91. arma_aligned MapMat<eT>& parent;
  92. arma_aligned const uword index;
  93. inline MapMat_val(MapMat<eT>& in_parent, const uword in_index);
  94. friend class MapMat<eT>;
  95. public:
  96. arma_inline operator eT() const;
  97. arma_inline typename get_pod_type<eT>::result real() const;
  98. arma_inline typename get_pod_type<eT>::result imag() const;
  99. arma_inline void operator= (const MapMat_val<eT>& x);
  100. arma_inline void operator= (const eT in_val);
  101. arma_inline void operator+=(const eT in_val);
  102. arma_inline void operator-=(const eT in_val);
  103. arma_inline void operator*=(const eT in_val);
  104. arma_inline void operator/=(const eT in_val);
  105. arma_inline void operator++();
  106. arma_inline void operator++(int);
  107. arma_inline void operator--();
  108. arma_inline void operator--(int);
  109. };
  110. template<typename eT>
  111. class SpMat_MapMat_val
  112. {
  113. private:
  114. arma_aligned SpMat<eT>& s_parent;
  115. arma_aligned MapMat<eT>& m_parent;
  116. arma_aligned const uword row;
  117. arma_aligned const uword col;
  118. inline SpMat_MapMat_val(SpMat<eT>& in_s_parent, MapMat<eT>& in_m_parent, const uword in_row, const uword in_col);
  119. friend class SpMat<eT>;
  120. friend class MapMat<eT>;
  121. friend class SpSubview_MapMat_val<eT>;
  122. public:
  123. inline operator eT() const;
  124. inline typename get_pod_type<eT>::result real() const;
  125. inline typename get_pod_type<eT>::result imag() const;
  126. inline SpMat_MapMat_val<eT>& operator= (const SpMat_MapMat_val<eT>& x);
  127. inline SpMat_MapMat_val<eT>& operator= (const eT in_val);
  128. inline SpMat_MapMat_val<eT>& operator+=(const eT in_val);
  129. inline SpMat_MapMat_val<eT>& operator-=(const eT in_val);
  130. inline SpMat_MapMat_val<eT>& operator*=(const eT in_val);
  131. inline SpMat_MapMat_val<eT>& operator/=(const eT in_val);
  132. inline SpMat_MapMat_val<eT>& operator++();
  133. inline arma_warn_unused eT operator++(int);
  134. inline SpMat_MapMat_val<eT>& operator--();
  135. inline arma_warn_unused eT operator--(int);
  136. inline void set(const eT in_val);
  137. inline void add(const eT in_val);
  138. inline void sub(const eT in_val);
  139. inline void mul(const eT in_val);
  140. inline void div(const eT in_val);
  141. };
  142. template<typename eT>
  143. class SpSubview_MapMat_val : public SpMat_MapMat_val<eT>
  144. {
  145. private:
  146. arma_inline SpSubview_MapMat_val(SpSubview<eT>& in_sv_parent, MapMat<eT>& in_m_parent, const uword in_row, const uword in_col);
  147. arma_aligned SpSubview<eT>& sv_parent;
  148. friend class SpMat<eT>;
  149. friend class MapMat<eT>;
  150. friend class SpSubview<eT>;
  151. friend class SpMat_MapMat_val<eT>;
  152. public:
  153. inline SpSubview_MapMat_val<eT>& operator= (const SpSubview_MapMat_val<eT>& x);
  154. inline SpSubview_MapMat_val<eT>& operator= (const eT in_val);
  155. inline SpSubview_MapMat_val<eT>& operator+=(const eT in_val);
  156. inline SpSubview_MapMat_val<eT>& operator-=(const eT in_val);
  157. inline SpSubview_MapMat_val<eT>& operator*=(const eT in_val);
  158. inline SpSubview_MapMat_val<eT>& operator/=(const eT in_val);
  159. inline SpSubview_MapMat_val<eT>& operator++();
  160. inline arma_warn_unused eT operator++(int);
  161. inline SpSubview_MapMat_val<eT>& operator--();
  162. inline arma_warn_unused eT operator--(int);
  163. };
  164. //! @}