SpMat_bones.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 SpMat
  16. //! @{
  17. //! Sparse matrix class, with data stored in compressed sparse column (CSC) format
  18. template<typename eT>
  19. class SpMat : public SpBase< eT, SpMat<eT> >
  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. const uword n_nonzero; //!< number of nonzero elements (read-only)
  31. const uword vec_state; //!< 0: matrix; 1: column vector; 2: row vector
  32. // The memory used to store the values of the matrix.
  33. // In accordance with the CSC format, this stores only the actual values.
  34. // The correct locations of the values are assembled from the row indices and column pointers.
  35. //
  36. // The length of this array is (n_nonzero + 1).
  37. // The final value values[n_nonzero] must be zero to ensure integrity of iterators.
  38. // Use mem_resize(new_n_nonzero) to resize this array.
  39. //
  40. // WARNING: the 'values' array is only valid after sync() is called;
  41. // WARNING: there is a separate cache for fast element insertion
  42. arma_aligned const eT* const values;
  43. // The row indices of each value. row_indices[i] is the row of values[i].
  44. //
  45. // The length of this array is (n_nonzero + 1).
  46. // The final value row_indices[n_nonzero] must be zero to ensure integrity of iterators.
  47. // Use mem_resize(new_n_nonzero) to resize this array.
  48. //
  49. // WARNING: the 'row_indices' array is only valid after sync() is called;
  50. // WARNING: there is a separate cache for fast element insertion
  51. arma_aligned const uword* const row_indices;
  52. // The column pointers. This stores the index of the first item in column i.
  53. // That is, values[col_ptrs[i]] is the first value in column i,
  54. // and it is in the row indicated by row_indices[col_ptrs[i]].
  55. //
  56. // The length of this array is (n_cols + 2).
  57. // The element col_ptrs[n_cols] must be equal to n_nonzero.
  58. // The element col_ptrs[n_cols + 1] must be an invalid very large value to ensure integrity of iterators.
  59. //
  60. // The col_ptrs array is set by the init() function
  61. // (which is called by constructors, set_size() and other functions that change the matrix size).
  62. //
  63. // WARNING: the 'col_ptrs' array is only valid after sync() is called;
  64. // WARNING: there is a separate cache for fast element insertion
  65. arma_aligned const uword* const col_ptrs;
  66. inline SpMat();
  67. inline ~SpMat();
  68. inline explicit SpMat(const uword in_rows, const uword in_cols);
  69. inline explicit SpMat(const SizeMat& s);
  70. inline SpMat(const char* text);
  71. inline SpMat& operator=(const char* text);
  72. inline SpMat(const std::string& text);
  73. inline SpMat& operator=(const std::string& text);
  74. inline SpMat(const SpMat<eT>& x);
  75. #if defined(ARMA_USE_CXX11)
  76. inline SpMat(SpMat&& m);
  77. inline SpMat& operator=(SpMat&& m);
  78. #endif
  79. inline explicit SpMat(const MapMat<eT>& x);
  80. inline SpMat& operator=(const MapMat<eT>& x);
  81. template<typename T1, typename T2, typename T3>
  82. inline SpMat(const Base<uword,T1>& rowind, const Base<uword,T2>& colptr, const Base<eT,T3>& values, const uword n_rows, const uword n_cols);
  83. template<typename T1, typename T2>
  84. inline SpMat(const Base<uword,T1>& locations, const Base<eT,T2>& values, const bool sort_locations = true);
  85. template<typename T1, typename T2>
  86. inline SpMat(const Base<uword,T1>& locations, const Base<eT,T2>& values, const uword n_rows, const uword n_cols, const bool sort_locations = true, const bool check_for_zeros = true);
  87. template<typename T1, typename T2>
  88. inline SpMat(const bool add_values, const Base<uword,T1>& locations, const Base<eT,T2>& values, const uword n_rows, const uword n_cols, const bool sort_locations = true, const bool check_for_zeros = true);
  89. inline SpMat& operator=(const eT val); //! sets size to 1x1
  90. inline SpMat& operator*=(const eT val);
  91. inline SpMat& operator/=(const eT val);
  92. // operator+=(val) and operator-=(val) are not defined as they don't make sense for sparse matrices
  93. inline SpMat& operator=(const SpMat& m);
  94. inline SpMat& operator+=(const SpMat& m);
  95. inline SpMat& operator-=(const SpMat& m);
  96. inline SpMat& operator*=(const SpMat& m);
  97. inline SpMat& operator%=(const SpMat& m);
  98. inline SpMat& operator/=(const SpMat& m);
  99. template<typename T1> inline explicit SpMat(const Base<eT, T1>& m);
  100. template<typename T1> inline SpMat& operator=(const Base<eT, T1>& m);
  101. template<typename T1> inline SpMat& operator+=(const Base<eT, T1>& m);
  102. template<typename T1> inline SpMat& operator-=(const Base<eT, T1>& m);
  103. template<typename T1> inline SpMat& operator*=(const Base<eT, T1>& m);
  104. template<typename T1> inline SpMat& operator/=(const Base<eT, T1>& m);
  105. template<typename T1> inline SpMat& operator%=(const Base<eT, T1>& m);
  106. template<typename T1> inline explicit SpMat(const Op<T1, op_diagmat>& expr);
  107. template<typename T1> inline SpMat& operator=(const Op<T1, op_diagmat>& expr);
  108. template<typename T1> inline SpMat& operator+=(const Op<T1, op_diagmat>& expr);
  109. template<typename T1> inline SpMat& operator-=(const Op<T1, op_diagmat>& expr);
  110. template<typename T1> inline SpMat& operator*=(const Op<T1, op_diagmat>& expr);
  111. template<typename T1> inline SpMat& operator/=(const Op<T1, op_diagmat>& expr);
  112. template<typename T1> inline SpMat& operator%=(const Op<T1, op_diagmat>& expr);
  113. //! explicit specification of sparse +/- scalar
  114. template<typename T1, typename op_type> inline explicit SpMat(const SpToDOp<T1, op_type>& expr);
  115. //! construction of complex matrix out of two non-complex matrices
  116. template<typename T1, typename T2>
  117. inline explicit SpMat(const SpBase<pod_type, T1>& A, const SpBase<pod_type, T2>& B);
  118. inline SpMat(const SpSubview<eT>& X);
  119. inline SpMat& operator=(const SpSubview<eT>& X);
  120. inline SpMat& operator+=(const SpSubview<eT>& X);
  121. inline SpMat& operator-=(const SpSubview<eT>& X);
  122. inline SpMat& operator*=(const SpSubview<eT>& X);
  123. inline SpMat& operator%=(const SpSubview<eT>& X);
  124. inline SpMat& operator/=(const SpSubview<eT>& X);
  125. inline SpMat(const spdiagview<eT>& X);
  126. inline SpMat& operator=(const spdiagview<eT>& X);
  127. inline SpMat& operator+=(const spdiagview<eT>& X);
  128. inline SpMat& operator-=(const spdiagview<eT>& X);
  129. inline SpMat& operator*=(const spdiagview<eT>& X);
  130. inline SpMat& operator%=(const spdiagview<eT>& X);
  131. inline SpMat& operator/=(const spdiagview<eT>& X);
  132. // delayed unary ops
  133. template<typename T1, typename spop_type> inline SpMat(const SpOp<T1, spop_type>& X);
  134. template<typename T1, typename spop_type> inline SpMat& operator=(const SpOp<T1, spop_type>& X);
  135. template<typename T1, typename spop_type> inline SpMat& operator+=(const SpOp<T1, spop_type>& X);
  136. template<typename T1, typename spop_type> inline SpMat& operator-=(const SpOp<T1, spop_type>& X);
  137. template<typename T1, typename spop_type> inline SpMat& operator*=(const SpOp<T1, spop_type>& X);
  138. template<typename T1, typename spop_type> inline SpMat& operator%=(const SpOp<T1, spop_type>& X);
  139. template<typename T1, typename spop_type> inline SpMat& operator/=(const SpOp<T1, spop_type>& X);
  140. // delayed binary ops
  141. template<typename T1, typename T2, typename spglue_type> inline SpMat(const SpGlue<T1, T2, spglue_type>& X);
  142. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator=(const SpGlue<T1, T2, spglue_type>& X);
  143. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator+=(const SpGlue<T1, T2, spglue_type>& X);
  144. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator-=(const SpGlue<T1, T2, spglue_type>& X);
  145. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator*=(const SpGlue<T1, T2, spglue_type>& X);
  146. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator%=(const SpGlue<T1, T2, spglue_type>& X);
  147. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator/=(const SpGlue<T1, T2, spglue_type>& X);
  148. // delayed mixed-type unary ops
  149. template<typename T1, typename spop_type> inline SpMat(const mtSpOp<eT, T1, spop_type>& X);
  150. template<typename T1, typename spop_type> inline SpMat& operator=(const mtSpOp<eT, T1, spop_type>& X);
  151. template<typename T1, typename spop_type> inline SpMat& operator+=(const mtSpOp<eT, T1, spop_type>& X);
  152. template<typename T1, typename spop_type> inline SpMat& operator-=(const mtSpOp<eT, T1, spop_type>& X);
  153. template<typename T1, typename spop_type> inline SpMat& operator*=(const mtSpOp<eT, T1, spop_type>& X);
  154. template<typename T1, typename spop_type> inline SpMat& operator%=(const mtSpOp<eT, T1, spop_type>& X);
  155. template<typename T1, typename spop_type> inline SpMat& operator/=(const mtSpOp<eT, T1, spop_type>& X);
  156. // delayed mixed-type binary ops
  157. template<typename T1, typename T2, typename spglue_type> inline SpMat(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  158. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  159. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator+=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  160. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator-=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  161. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator*=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  162. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator%=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  163. template<typename T1, typename T2, typename spglue_type> inline SpMat& operator/=(const mtSpGlue<eT, T1, T2, spglue_type>& X);
  164. arma_inline SpSubview_row<eT> row(const uword row_num);
  165. arma_inline const SpSubview_row<eT> row(const uword row_num) const;
  166. inline SpSubview_row<eT> operator()(const uword row_num, const span& col_span);
  167. inline const SpSubview_row<eT> operator()(const uword row_num, const span& col_span) const;
  168. arma_inline SpSubview_col<eT> col(const uword col_num);
  169. arma_inline const SpSubview_col<eT> col(const uword col_num) const;
  170. inline SpSubview_col<eT> operator()(const span& row_span, const uword col_num);
  171. inline const SpSubview_col<eT> operator()(const span& row_span, const uword col_num) const;
  172. arma_inline SpSubview<eT> rows(const uword in_row1, const uword in_row2);
  173. arma_inline const SpSubview<eT> rows(const uword in_row1, const uword in_row2) const;
  174. arma_inline SpSubview<eT> cols(const uword in_col1, const uword in_col2);
  175. arma_inline const SpSubview<eT> cols(const uword in_col1, const uword in_col2) const;
  176. arma_inline SpSubview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2);
  177. arma_inline const SpSubview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const;
  178. arma_inline SpSubview<eT> submat(const uword in_row1, const uword in_col1, const SizeMat& s);
  179. arma_inline const SpSubview<eT> submat(const uword in_row1, const uword in_col1, const SizeMat& s) const;
  180. inline SpSubview<eT> submat (const span& row_span, const span& col_span);
  181. inline const SpSubview<eT> submat (const span& row_span, const span& col_span) const;
  182. inline SpSubview<eT> operator()(const span& row_span, const span& col_span);
  183. inline const SpSubview<eT> operator()(const span& row_span, const span& col_span) const;
  184. arma_inline SpSubview<eT> operator()(const uword in_row1, const uword in_col1, const SizeMat& s);
  185. arma_inline const SpSubview<eT> operator()(const uword in_row1, const uword in_col1, const SizeMat& s) const;
  186. inline SpSubview<eT> head_rows(const uword N);
  187. inline const SpSubview<eT> head_rows(const uword N) const;
  188. inline SpSubview<eT> tail_rows(const uword N);
  189. inline const SpSubview<eT> tail_rows(const uword N) const;
  190. inline SpSubview<eT> head_cols(const uword N);
  191. inline const SpSubview<eT> head_cols(const uword N) const;
  192. inline SpSubview<eT> tail_cols(const uword N);
  193. inline const SpSubview<eT> tail_cols(const uword N) const;
  194. inline spdiagview<eT> diag(const sword in_id = 0);
  195. inline const spdiagview<eT> diag(const sword in_id = 0) const;
  196. inline void swap_rows(const uword in_row1, const uword in_row2);
  197. inline void swap_cols(const uword in_col1, const uword in_col2);
  198. inline void shed_row(const uword row_num);
  199. inline void shed_col(const uword col_num);
  200. inline void shed_rows(const uword in_row1, const uword in_row2);
  201. inline void shed_cols(const uword in_col1, const uword in_col2);
  202. // access the i-th element; if there is nothing at element i, 0 is returned
  203. arma_inline arma_warn_unused SpMat_MapMat_val<eT> operator[] (const uword i);
  204. arma_inline arma_warn_unused eT operator[] (const uword i) const;
  205. arma_inline arma_warn_unused SpMat_MapMat_val<eT> at (const uword i);
  206. arma_inline arma_warn_unused eT at (const uword i) const;
  207. arma_inline arma_warn_unused SpMat_MapMat_val<eT> operator() (const uword i);
  208. arma_inline arma_warn_unused eT operator() (const uword i) const;
  209. // access the element at the given row and column; if there is nothing at that position, 0 is returned
  210. arma_inline arma_warn_unused SpMat_MapMat_val<eT> at (const uword in_row, const uword in_col);
  211. arma_inline arma_warn_unused eT at (const uword in_row, const uword in_col) const;
  212. arma_inline arma_warn_unused SpMat_MapMat_val<eT> operator() (const uword in_row, const uword in_col);
  213. arma_inline arma_warn_unused eT operator() (const uword in_row, const uword in_col) const;
  214. arma_inline arma_warn_unused bool is_empty() const;
  215. arma_inline arma_warn_unused bool is_vec() const;
  216. arma_inline arma_warn_unused bool is_rowvec() const;
  217. arma_inline arma_warn_unused bool is_colvec() const;
  218. arma_inline arma_warn_unused bool is_square() const;
  219. inline arma_warn_unused bool is_finite() const;
  220. inline arma_warn_unused bool is_symmetric() const;
  221. inline arma_warn_unused bool is_symmetric(const typename get_pod_type<eT>::result tol) const;
  222. inline arma_warn_unused bool is_hermitian() const;
  223. inline arma_warn_unused bool is_hermitian(const typename get_pod_type<eT>::result tol) const;
  224. inline arma_warn_unused bool has_inf() const;
  225. inline arma_warn_unused bool has_nan() const;
  226. arma_inline arma_warn_unused bool in_range(const uword i) const;
  227. arma_inline arma_warn_unused bool in_range(const span& x) const;
  228. arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col) const;
  229. arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col) const;
  230. arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const;
  231. arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const;
  232. arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col, const SizeMat& s) const;
  233. arma_cold inline void impl_print( const std::string& extra_text) const;
  234. arma_cold inline void impl_print(std::ostream& user_stream, const std::string& extra_text) const;
  235. arma_cold inline void impl_raw_print( const std::string& extra_text) const;
  236. arma_cold inline void impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const;
  237. arma_cold inline void impl_print_dense( const std::string& extra_text) const;
  238. arma_cold inline void impl_print_dense(std::ostream& user_stream, const std::string& extra_text) const;
  239. arma_cold inline void impl_raw_print_dense( const std::string& extra_text) const;
  240. arma_cold inline void impl_raw_print_dense(std::ostream& user_stream, const std::string& extra_text) const;
  241. template<typename eT2> inline void copy_size(const SpMat<eT2>& m);
  242. template<typename eT2> inline void copy_size(const Mat<eT2>& m);
  243. inline void set_size(const uword in_elem);
  244. inline void set_size(const uword in_rows, const uword in_cols);
  245. inline void set_size(const SizeMat& s);
  246. inline void resize(const uword in_rows, const uword in_cols);
  247. inline void resize(const SizeMat& s);
  248. inline void reshape(const uword in_rows, const uword in_cols);
  249. inline void reshape(const SizeMat& s);
  250. inline void reshape_helper_generic(const uword in_rows, const uword in_cols); //! internal use only
  251. inline void reshape_helper_intovec(); //! internal use only
  252. arma_deprecated inline void reshape(const uword in_rows, const uword in_cols, const uword dim); //!< NOTE: don't use this form: it will be removed
  253. template<typename functor> inline const SpMat& for_each(functor F);
  254. template<typename functor> inline const SpMat& for_each(functor F) const;
  255. template<typename functor> inline const SpMat& transform(functor F);
  256. inline const SpMat& replace(const eT old_val, const eT new_val);
  257. inline const SpMat& clean(const pod_type threshold);
  258. inline const SpMat& zeros();
  259. inline const SpMat& zeros(const uword in_elem);
  260. inline const SpMat& zeros(const uword in_rows, const uword in_cols);
  261. inline const SpMat& zeros(const SizeMat& s);
  262. inline const SpMat& eye();
  263. inline const SpMat& eye(const uword in_rows, const uword in_cols);
  264. inline const SpMat& eye(const SizeMat& s);
  265. inline const SpMat& speye();
  266. inline const SpMat& speye(const uword in_rows, const uword in_cols);
  267. inline const SpMat& speye(const SizeMat& s);
  268. inline const SpMat& sprandu(const uword in_rows, const uword in_cols, const double density);
  269. inline const SpMat& sprandu(const SizeMat& s, const double density);
  270. inline const SpMat& sprandn(const uword in_rows, const uword in_cols, const double density);
  271. inline const SpMat& sprandn(const SizeMat& s, const double density);
  272. inline void reset();
  273. //! don't use this unless you're writing internal Armadillo code
  274. inline void reserve(const uword in_rows, const uword in_cols, const uword new_n_nonzero);
  275. //! don't use this unless you're writing internal Armadillo code
  276. inline SpMat(const arma_reserve_indicator&, const uword in_rows, const uword in_cols, const uword new_n_nonzero);
  277. //! don't use this unless you're writing internal Armadillo code
  278. template<typename eT2>
  279. inline SpMat(const arma_layout_indicator&, const SpMat<eT2>& x);
  280. template<typename T1> inline void set_real(const SpBase<pod_type,T1>& X);
  281. template<typename T1> inline void set_imag(const SpBase<pod_type,T1>& X);
  282. // saving and loading
  283. // TODO: implement auto_detect for sparse matrices
  284. inline arma_cold bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const;
  285. inline arma_cold bool save(const csv_name& spec, const file_type type = csv_ascii, const bool print_status = true) const;
  286. inline arma_cold bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const;
  287. inline arma_cold bool load(const std::string name, const file_type type = arma_binary, const bool print_status = true);
  288. inline arma_cold bool load(const csv_name& spec, const file_type type = csv_ascii, const bool print_status = true);
  289. inline arma_cold bool load( std::istream& is, const file_type type = arma_binary, const bool print_status = true);
  290. inline arma_cold bool quiet_save(const std::string name, const file_type type = arma_binary) const;
  291. inline arma_cold bool quiet_save(const csv_name& spec, const file_type type = csv_ascii) const;
  292. inline arma_cold bool quiet_save( std::ostream& os, const file_type type = arma_binary) const;
  293. inline arma_cold bool quiet_load(const std::string name, const file_type type = arma_binary);
  294. inline arma_cold bool quiet_load(const csv_name& spec, const file_type type = csv_ascii);
  295. inline arma_cold bool quiet_load( std::istream& is, const file_type type = arma_binary);
  296. // necessary forward declarations
  297. class iterator_base;
  298. class iterator;
  299. class const_iterator;
  300. class row_iterator;
  301. class const_row_iterator;
  302. // iterator_base provides basic operators but not how to compare or how to iterate
  303. class iterator_base
  304. {
  305. public:
  306. inline iterator_base();
  307. inline iterator_base(const SpMat& in_M);
  308. inline iterator_base(const SpMat& in_M, const uword col, const uword pos);
  309. arma_inline eT operator*() const;
  310. // don't hold location internally; call "dummy" methods to get that information
  311. arma_inline uword row() const { return M->row_indices[internal_pos]; }
  312. arma_inline uword col() const { return internal_col; }
  313. arma_inline uword pos() const { return internal_pos; }
  314. arma_aligned const SpMat* M;
  315. arma_aligned uword internal_col;
  316. arma_aligned uword internal_pos;
  317. typedef std::bidirectional_iterator_tag iterator_category;
  318. typedef eT value_type;
  319. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  320. typedef const eT* pointer;
  321. typedef const eT& reference;
  322. };
  323. class const_iterator : public iterator_base
  324. {
  325. public:
  326. inline const_iterator();
  327. inline const_iterator(const SpMat& in_M, uword initial_pos = 0); // assumes initial_pos is valid
  328. //! once initialised, will be at the first nonzero value after the given position (using forward columnwise traversal)
  329. inline const_iterator(const SpMat& in_M, uword in_row, uword in_col);
  330. //! if you know the exact position of the iterator; in_row is a dummy argument
  331. inline const_iterator(const SpMat& in_M, uword in_row, uword in_col, uword in_pos);
  332. inline const_iterator(const const_iterator& other);
  333. inline arma_hot const_iterator& operator++();
  334. inline arma_warn_unused const_iterator operator++(int);
  335. inline arma_hot const_iterator& operator--();
  336. inline arma_warn_unused const_iterator operator--(int);
  337. inline arma_hot bool operator==(const const_iterator& rhs) const;
  338. inline arma_hot bool operator!=(const const_iterator& rhs) const;
  339. inline arma_hot bool operator==(const typename SpSubview<eT>::const_iterator& rhs) const;
  340. inline arma_hot bool operator!=(const typename SpSubview<eT>::const_iterator& rhs) const;
  341. inline arma_hot bool operator==(const const_row_iterator& rhs) const;
  342. inline arma_hot bool operator!=(const const_row_iterator& rhs) const;
  343. inline arma_hot bool operator==(const typename SpSubview<eT>::const_row_iterator& rhs) const;
  344. inline arma_hot bool operator!=(const typename SpSubview<eT>::const_row_iterator& rhs) const;
  345. };
  346. /**
  347. * So that we can iterate over nonzero values, we need an iterator implementation.
  348. * This can't be as simple as for Mat, which is just a pointer to an eT.
  349. * If a value is set to 0 using this iterator, the iterator is no longer valid!
  350. */
  351. class iterator : public const_iterator
  352. {
  353. public:
  354. inline iterator() : const_iterator() { }
  355. inline iterator(SpMat& in_M, uword initial_pos = 0) : const_iterator(in_M, initial_pos) { }
  356. inline iterator(SpMat& in_M, uword in_row, uword in_col) : const_iterator(in_M, in_row, in_col) { }
  357. inline iterator(SpMat& in_M, uword in_row, uword in_col, uword in_pos) : const_iterator(in_M, in_row, in_col, in_pos) { }
  358. inline iterator(const iterator& other) : const_iterator(other) { }
  359. inline arma_hot SpValProxy<SpMat<eT> > operator*();
  360. // overloads needed for return type correctness
  361. inline arma_hot iterator& operator++();
  362. inline arma_warn_unused iterator operator++(int);
  363. inline arma_hot iterator& operator--();
  364. inline arma_warn_unused iterator operator--(int);
  365. // this has a different value_type than iterator_base
  366. typedef SpValProxy<SpMat<eT> > value_type;
  367. typedef const SpValProxy<SpMat<eT> >* pointer;
  368. typedef const SpValProxy<SpMat<eT> >& reference;
  369. };
  370. class const_row_iterator : public iterator_base
  371. {
  372. public:
  373. inline const_row_iterator();
  374. inline const_row_iterator(const SpMat& in_M, uword initial_pos = 0);
  375. //! once initialised, will be at the first nonzero value after the given position (using forward row-wise traversal)
  376. inline const_row_iterator(const SpMat& in_M, uword in_row, uword in_col);
  377. inline const_row_iterator(const const_row_iterator& other);
  378. inline arma_hot const_row_iterator& operator++();
  379. inline arma_warn_unused const_row_iterator operator++(int);
  380. inline arma_hot const_row_iterator& operator--();
  381. inline arma_warn_unused const_row_iterator operator--(int);
  382. uword internal_row; // hold row internally
  383. uword actual_pos; // this holds the true position we are at in the matrix, as column-major indexing
  384. arma_inline eT operator*() const { return iterator_base::M->values[actual_pos]; }
  385. arma_inline uword row() const { return internal_row; }
  386. inline arma_hot bool operator==(const const_iterator& rhs) const;
  387. inline arma_hot bool operator!=(const const_iterator& rhs) const;
  388. inline arma_hot bool operator==(const typename SpSubview<eT>::const_iterator& rhs) const;
  389. inline arma_hot bool operator!=(const typename SpSubview<eT>::const_iterator& rhs) const;
  390. inline arma_hot bool operator==(const const_row_iterator& rhs) const;
  391. inline arma_hot bool operator!=(const const_row_iterator& rhs) const;
  392. inline arma_hot bool operator==(const typename SpSubview<eT>::const_row_iterator& rhs) const;
  393. inline arma_hot bool operator!=(const typename SpSubview<eT>::const_row_iterator& rhs) const;
  394. };
  395. class row_iterator : public const_row_iterator
  396. {
  397. public:
  398. inline row_iterator() : const_row_iterator() {}
  399. inline row_iterator(SpMat& in_M, uword initial_pos = 0) : const_row_iterator(in_M, initial_pos) { }
  400. //! once initialised, will be at the first nonzero value after the given position (using forward row-wise traversal)
  401. inline row_iterator(SpMat& in_M, uword in_row, uword in_col) : const_row_iterator(in_M, in_row, in_col) { }
  402. inline row_iterator(const row_iterator& other) : const_row_iterator(other) { }
  403. inline arma_hot SpValProxy<SpMat<eT> > operator*();
  404. // overloads required for return type correctness
  405. inline arma_hot row_iterator& operator++();
  406. inline arma_warn_unused row_iterator operator++(int);
  407. inline arma_hot row_iterator& operator--();
  408. inline arma_warn_unused row_iterator operator--(int);
  409. // this has a different value_type than iterator_base
  410. typedef SpValProxy<SpMat<eT> > value_type;
  411. typedef const SpValProxy<SpMat<eT> >* pointer;
  412. typedef const SpValProxy<SpMat<eT> >& reference;
  413. };
  414. typedef iterator col_iterator;
  415. typedef const_iterator const_col_iterator;
  416. typedef iterator row_col_iterator;
  417. typedef const_iterator const_row_col_iterator;
  418. inline iterator begin();
  419. inline const_iterator begin() const;
  420. inline const_iterator cbegin() const;
  421. inline iterator end();
  422. inline const_iterator end() const;
  423. inline const_iterator cend() const;
  424. inline col_iterator begin_col(const uword col_num);
  425. inline const_col_iterator begin_col(const uword col_num) const;
  426. inline col_iterator begin_col_no_sync(const uword col_num);
  427. inline const_col_iterator begin_col_no_sync(const uword col_num) const;
  428. inline col_iterator end_col(const uword col_num);
  429. inline const_col_iterator end_col(const uword col_num) const;
  430. inline col_iterator end_col_no_sync(const uword col_num);
  431. inline const_col_iterator end_col_no_sync(const uword col_num) const;
  432. inline row_iterator begin_row(const uword row_num = 0);
  433. inline const_row_iterator begin_row(const uword row_num = 0) const;
  434. inline row_iterator end_row();
  435. inline const_row_iterator end_row() const;
  436. inline row_iterator end_row(const uword row_num);
  437. inline const_row_iterator end_row(const uword row_num) const;
  438. inline row_col_iterator begin_row_col();
  439. inline const_row_col_iterator begin_row_col() const;
  440. inline row_col_iterator end_row_col();
  441. inline const_row_col_iterator end_row_col() const;
  442. inline void clear();
  443. inline bool empty() const;
  444. inline uword size() const;
  445. arma_inline arma_warn_unused SpMat_MapMat_val<eT> front();
  446. arma_inline arma_warn_unused eT front() const;
  447. arma_inline arma_warn_unused SpMat_MapMat_val<eT> back();
  448. arma_inline arma_warn_unused eT back() const;
  449. // Resize memory.
  450. // If the new size is larger, the column pointers and new memory still need to be correctly set.
  451. // If the new size is smaller, the first new_n_nonzero elements will be copied.
  452. // n_nonzero is updated.
  453. inline void mem_resize(const uword new_n_nonzero);
  454. //! synchronise CSC from cache
  455. inline void sync() const;
  456. //! don't use this unless you're writing internal Armadillo code
  457. inline void remove_zeros();
  458. //! don't use this unless you're writing internal Armadillo code
  459. inline void steal_mem(SpMat& X);
  460. //! don't use this unless you're writing internal Armadillo code
  461. inline void steal_mem_simple(SpMat& X);
  462. //! don't use this unless you're writing internal Armadillo code
  463. template< typename T1, typename Functor> arma_hot inline void init_xform (const SpBase<eT, T1>& x, const Functor& func);
  464. template<typename eT2, typename T1, typename Functor> arma_hot inline void init_xform_mt(const SpBase<eT2,T1>& x, const Functor& func);
  465. //! don't use this unless you're writing internal Armadillo code
  466. arma_inline bool is_alias(const SpMat<eT>& X) const;
  467. protected:
  468. inline void init(uword in_rows, uword in_cols, const uword new_n_nonzero = 0);
  469. inline void arma_cold init_cold(uword in_rows, uword in_cols, const uword new_n_nonzero = 0);
  470. inline void init(const std::string& text);
  471. inline void init(const SpMat<eT>& x);
  472. inline void init(const MapMat<eT>& x);
  473. inline void init_simple(const SpMat<eT>& x);
  474. inline void init_batch_std(const Mat<uword>& locations, const Mat<eT>& values, const bool sort_locations);
  475. inline void init_batch_add(const Mat<uword>& locations, const Mat<eT>& values, const bool sort_locations);
  476. inline SpMat(const arma_vec_indicator&, const uword in_vec_state);
  477. inline SpMat(const arma_vec_indicator&, const uword in_n_rows, const uword in_n_cols, const uword in_vec_state);
  478. private:
  479. inline arma_hot arma_warn_unused const eT* find_value_csc(const uword in_row, const uword in_col) const;
  480. inline arma_hot arma_warn_unused eT get_value(const uword i ) const;
  481. inline arma_hot arma_warn_unused eT get_value(const uword in_row, const uword in_col) const;
  482. inline arma_hot arma_warn_unused eT get_value_csc(const uword i ) const;
  483. inline arma_hot arma_warn_unused eT get_value_csc(const uword in_row, const uword in_col) const;
  484. inline arma_hot arma_warn_unused bool try_set_value_csc(const uword in_row, const uword in_col, const eT in_val);
  485. inline arma_hot arma_warn_unused bool try_add_value_csc(const uword in_row, const uword in_col, const eT in_val);
  486. inline arma_hot arma_warn_unused bool try_sub_value_csc(const uword in_row, const uword in_col, const eT in_val);
  487. inline arma_hot arma_warn_unused bool try_mul_value_csc(const uword in_row, const uword in_col, const eT in_val);
  488. inline arma_hot arma_warn_unused bool try_div_value_csc(const uword in_row, const uword in_col, const eT in_val);
  489. inline arma_warn_unused eT& insert_element(const uword in_row, const uword in_col, const eT in_val = eT(0));
  490. inline void delete_element(const uword in_row, const uword in_col);
  491. // cache related
  492. arma_aligned mutable MapMat<eT> cache;
  493. arma_aligned mutable state_type sync_state;
  494. // 0: cache needs to be updated from CSC (ie. CSC has more recent data)
  495. // 1: CSC needs to be updated from cache (ie. cache has more recent data)
  496. // 2: no update required (ie. CSC and cache contain the same data)
  497. #if (defined(ARMA_USE_CXX11) && !defined(ARMA_DONT_USE_CXX11_MUTEX))
  498. arma_aligned mutable std::mutex cache_mutex;
  499. #endif
  500. arma_inline void invalidate_cache() const;
  501. arma_inline void invalidate_csc() const;
  502. inline void sync_cache() const;
  503. inline void sync_cache_simple() const;
  504. inline void sync_csc() const;
  505. inline void sync_csc_simple() const;
  506. friend class SpValProxy< SpMat<eT> >; // allow SpValProxy to call insert_element() and delete_element()
  507. friend class SpSubview<eT>;
  508. friend class SpRow<eT>;
  509. friend class SpCol<eT>;
  510. friend class SpMat_MapMat_val<eT>;
  511. friend class SpSubview_MapMat_val<eT>;
  512. friend class spdiagview<eT>;
  513. public:
  514. #ifdef ARMA_EXTRA_SPMAT_PROTO
  515. #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPMAT_PROTO)
  516. #endif
  517. };
  518. class SpMat_aux
  519. {
  520. public:
  521. template<typename eT, typename T1> inline static void set_real(SpMat<eT>& out, const SpBase<eT,T1>& X);
  522. template<typename T, typename T1> inline static void set_real(SpMat< std::complex<T> >& out, const SpBase< T,T1>& X);
  523. template<typename eT, typename T1> inline static void set_imag(SpMat<eT>& out, const SpBase<eT,T1>& X);
  524. template<typename T, typename T1> inline static void set_imag(SpMat< std::complex<T> >& out, const SpBase< T,T1>& X);
  525. };
  526. #define ARMA_HAS_SPMAT
  527. //! @}