Mat_bones.hpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 Mat
  16. //! @{
  17. //! Dense matrix class
  18. template<typename eT>
  19. class Mat : public Base< eT, Mat<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. const uword n_rows; //!< number of rows (read-only)
  25. const uword n_cols; //!< number of columns (read-only)
  26. const uword n_elem; //!< number of elements (read-only)
  27. const uhword vec_state; //!< 0: matrix layout; 1: column vector layout; 2: row vector layout
  28. const uhword mem_state;
  29. // mem_state = 0: normal matrix which manages its own memory
  30. // mem_state = 1: use auxiliary memory until a size change
  31. // mem_state = 2: use auxiliary memory and don't allow the number of elements to be changed
  32. // mem_state = 3: fixed size (eg. via template based size specification)
  33. #ifdef LUOYC20220705
  34. arma_aligned eT* mem; //!< pointer to the memory used for storing elements (memory is read-only)
  35. #else
  36. arma_aligned const eT* const mem; //!< pointer to the memory used for storing elements (memory is read-only)
  37. #endif
  38. protected:
  39. arma_align_mem eT mem_local[ arma_config::mat_prealloc ]; // local storage, for small vectors and matrices
  40. public:
  41. static const bool is_col = false;
  42. static const bool is_row = false;
  43. static const bool is_xvec = false;
  44. inline ~Mat();
  45. inline Mat();
  46. inline explicit Mat(const uword in_rows, const uword in_cols);
  47. inline explicit Mat(const SizeMat& s);
  48. template<typename fill_type> inline Mat(const uword in_rows, const uword in_cols, const fill::fill_class<fill_type>& f);
  49. template<typename fill_type> inline Mat(const SizeMat& s, const fill::fill_class<fill_type>& f);
  50. inline arma_cold Mat(const char* text);
  51. inline arma_cold Mat& operator=(const char* text);
  52. inline arma_cold Mat(const std::string& text);
  53. inline arma_cold Mat& operator=(const std::string& text);
  54. inline Mat(const std::vector<eT>& x);
  55. inline Mat& operator=(const std::vector<eT>& x);
  56. #if defined(ARMA_USE_CXX11)
  57. inline Mat(const std::initializer_list<eT>& list);
  58. inline Mat& operator=(const std::initializer_list<eT>& list);
  59. inline Mat(const std::initializer_list< std::initializer_list<eT> >& list);
  60. inline Mat& operator=(const std::initializer_list< std::initializer_list<eT> >& list);
  61. inline Mat(Mat&& m);
  62. inline Mat& operator=(Mat&& m);
  63. #endif
  64. inline Mat( eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const bool copy_aux_mem = true, const bool strict = false);
  65. inline Mat(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols);
  66. inline Mat& operator=(const eT val);
  67. inline Mat& operator+=(const eT val);
  68. inline Mat& operator-=(const eT val);
  69. inline Mat& operator*=(const eT val);
  70. inline Mat& operator/=(const eT val);
  71. inline Mat(const Mat& m);
  72. inline Mat& operator=(const Mat& m);
  73. inline Mat& operator+=(const Mat& m);
  74. inline Mat& operator-=(const Mat& m);
  75. inline Mat& operator*=(const Mat& m);
  76. inline Mat& operator%=(const Mat& m);
  77. inline Mat& operator/=(const Mat& m);
  78. template<typename T1> inline Mat(const BaseCube<eT,T1>& X);
  79. template<typename T1> inline Mat& operator=(const BaseCube<eT,T1>& X);
  80. template<typename T1> inline Mat& operator+=(const BaseCube<eT,T1>& X);
  81. template<typename T1> inline Mat& operator-=(const BaseCube<eT,T1>& X);
  82. template<typename T1> inline Mat& operator*=(const BaseCube<eT,T1>& X);
  83. template<typename T1> inline Mat& operator%=(const BaseCube<eT,T1>& X);
  84. template<typename T1> inline Mat& operator/=(const BaseCube<eT,T1>& X);
  85. template<typename T1, typename T2>
  86. inline explicit Mat(const Base<pod_type,T1>& A, const Base<pod_type,T2>& B);
  87. #ifdef LUOYC20220705
  88. inline explicit Mat(subview<eT>& X, const bool use_colmem);
  89. #else
  90. inline explicit Mat(const subview<eT>& X, const bool use_colmem); // only to be used by the quasi_unwrap class
  91. #endif
  92. inline Mat(const subview<eT>& X);
  93. inline Mat& operator=(const subview<eT>& X);
  94. inline Mat& operator+=(const subview<eT>& X);
  95. inline Mat& operator-=(const subview<eT>& X);
  96. inline Mat& operator*=(const subview<eT>& X);
  97. inline Mat& operator%=(const subview<eT>& X);
  98. inline Mat& operator/=(const subview<eT>& X);
  99. inline Mat(const subview_row_strans<eT>& X); // subview_row_strans can only be generated by the Proxy class
  100. inline Mat(const subview_row_htrans<eT>& X); // subview_row_htrans can only be generated by the Proxy class
  101. inline Mat(const xvec_htrans<eT>& X); // xvec_htrans can only be generated by the Proxy class
  102. template<bool do_conj>
  103. inline Mat(const xtrans_mat<eT,do_conj>& X); // xtrans_mat can only be generated by the Proxy class
  104. inline Mat(const subview_cube<eT>& X);
  105. inline Mat& operator=(const subview_cube<eT>& X);
  106. inline Mat& operator+=(const subview_cube<eT>& X);
  107. inline Mat& operator-=(const subview_cube<eT>& X);
  108. inline Mat& operator*=(const subview_cube<eT>& X);
  109. inline Mat& operator%=(const subview_cube<eT>& X);
  110. inline Mat& operator/=(const subview_cube<eT>& X);
  111. inline Mat(const diagview<eT>& X);
  112. inline Mat& operator=(const diagview<eT>& X);
  113. inline Mat& operator+=(const diagview<eT>& X);
  114. inline Mat& operator-=(const diagview<eT>& X);
  115. inline Mat& operator*=(const diagview<eT>& X);
  116. inline Mat& operator%=(const diagview<eT>& X);
  117. inline Mat& operator/=(const diagview<eT>& X);
  118. template<typename T1> inline Mat(const subview_elem1<eT,T1>& X);
  119. template<typename T1> inline Mat& operator= (const subview_elem1<eT,T1>& X);
  120. template<typename T1> inline Mat& operator+=(const subview_elem1<eT,T1>& X);
  121. template<typename T1> inline Mat& operator-=(const subview_elem1<eT,T1>& X);
  122. template<typename T1> inline Mat& operator*=(const subview_elem1<eT,T1>& X);
  123. template<typename T1> inline Mat& operator%=(const subview_elem1<eT,T1>& X);
  124. template<typename T1> inline Mat& operator/=(const subview_elem1<eT,T1>& X);
  125. template<typename T1, typename T2> inline Mat(const subview_elem2<eT,T1,T2>& X);
  126. template<typename T1, typename T2> inline Mat& operator= (const subview_elem2<eT,T1,T2>& X);
  127. template<typename T1, typename T2> inline Mat& operator+=(const subview_elem2<eT,T1,T2>& X);
  128. template<typename T1, typename T2> inline Mat& operator-=(const subview_elem2<eT,T1,T2>& X);
  129. template<typename T1, typename T2> inline Mat& operator*=(const subview_elem2<eT,T1,T2>& X);
  130. template<typename T1, typename T2> inline Mat& operator%=(const subview_elem2<eT,T1,T2>& X);
  131. template<typename T1, typename T2> inline Mat& operator/=(const subview_elem2<eT,T1,T2>& X);
  132. // Operators on sparse matrices (and subviews)
  133. template<typename T1> inline explicit Mat(const SpBase<eT, T1>& m);
  134. template<typename T1> inline Mat& operator=(const SpBase<eT, T1>& m);
  135. template<typename T1> inline Mat& operator+=(const SpBase<eT, T1>& m);
  136. template<typename T1> inline Mat& operator-=(const SpBase<eT, T1>& m);
  137. template<typename T1> inline Mat& operator*=(const SpBase<eT, T1>& m);
  138. template<typename T1> inline Mat& operator%=(const SpBase<eT, T1>& m);
  139. template<typename T1> inline Mat& operator/=(const SpBase<eT, T1>& m);
  140. inline explicit Mat(const SpSubview<eT>& X);
  141. inline Mat& operator=(const SpSubview<eT>& X);
  142. inline explicit Mat(const spdiagview<eT>& X);
  143. inline Mat& operator=(const spdiagview<eT>& X);
  144. inline Mat& operator+=(const spdiagview<eT>& X);
  145. inline Mat& operator-=(const spdiagview<eT>& X);
  146. inline Mat& operator*=(const spdiagview<eT>& X);
  147. inline Mat& operator%=(const spdiagview<eT>& X);
  148. inline Mat& operator/=(const spdiagview<eT>& X);
  149. inline mat_injector<Mat> operator<<(const eT val);
  150. inline mat_injector<Mat> operator<<(const injector_end_of_row<>& x);
  151. arma_inline subview_row<eT> row(const uword row_num);
  152. arma_inline const subview_row<eT> row(const uword row_num) const;
  153. inline subview_row<eT> operator()(const uword row_num, const span& col_span);
  154. inline const subview_row<eT> operator()(const uword row_num, const span& col_span) const;
  155. arma_inline subview_col<eT> col(const uword col_num);
  156. arma_inline const subview_col<eT> col(const uword col_num) const;
  157. inline subview_col<eT> operator()(const span& row_span, const uword col_num);
  158. inline const subview_col<eT> operator()(const span& row_span, const uword col_num) const;
  159. inline Col<eT> unsafe_col(const uword col_num);
  160. inline const Col<eT> unsafe_col(const uword col_num) const;
  161. arma_inline subview<eT> rows(const uword in_row1, const uword in_row2);
  162. arma_inline const subview<eT> rows(const uword in_row1, const uword in_row2) const;
  163. arma_inline subview<eT> cols(const uword in_col1, const uword in_col2);
  164. arma_inline const subview<eT> cols(const uword in_col1, const uword in_col2) const;
  165. inline subview<eT> rows(const span& row_span);
  166. inline const subview<eT> rows(const span& row_span) const;
  167. arma_inline subview<eT> cols(const span& col_span);
  168. arma_inline const subview<eT> cols(const span& col_span) const;
  169. arma_inline subview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2);
  170. arma_inline const subview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const;
  171. arma_inline subview<eT> submat(const uword in_row1, const uword in_col1, const SizeMat& s);
  172. arma_inline const subview<eT> submat(const uword in_row1, const uword in_col1, const SizeMat& s) const;
  173. inline subview<eT> submat (const span& row_span, const span& col_span);
  174. inline const subview<eT> submat (const span& row_span, const span& col_span) const;
  175. inline subview<eT> operator()(const span& row_span, const span& col_span);
  176. inline const subview<eT> operator()(const span& row_span, const span& col_span) const;
  177. inline subview<eT> operator()(const uword in_row1, const uword in_col1, const SizeMat& s);
  178. inline const subview<eT> operator()(const uword in_row1, const uword in_col1, const SizeMat& s) const;
  179. inline subview<eT> head_rows(const uword N);
  180. inline const subview<eT> head_rows(const uword N) const;
  181. inline subview<eT> tail_rows(const uword N);
  182. inline const subview<eT> tail_rows(const uword N) const;
  183. inline subview<eT> head_cols(const uword N);
  184. inline const subview<eT> head_cols(const uword N) const;
  185. inline subview<eT> tail_cols(const uword N);
  186. inline const subview<eT> tail_cols(const uword N) const;
  187. template<typename T1> arma_inline subview_elem1<eT,T1> elem(const Base<uword,T1>& a);
  188. template<typename T1> arma_inline const subview_elem1<eT,T1> elem(const Base<uword,T1>& a) const;
  189. template<typename T1> arma_inline subview_elem1<eT,T1> operator()(const Base<uword,T1>& a);
  190. template<typename T1> arma_inline const subview_elem1<eT,T1> operator()(const Base<uword,T1>& a) const;
  191. template<typename T1, typename T2> arma_inline subview_elem2<eT,T1,T2> elem(const Base<uword,T1>& ri, const Base<uword,T2>& ci);
  192. template<typename T1, typename T2> arma_inline const subview_elem2<eT,T1,T2> elem(const Base<uword,T1>& ri, const Base<uword,T2>& ci) const;
  193. template<typename T1, typename T2> arma_inline subview_elem2<eT,T1,T2> submat(const Base<uword,T1>& ri, const Base<uword,T2>& ci);
  194. template<typename T1, typename T2> arma_inline const subview_elem2<eT,T1,T2> submat(const Base<uword,T1>& ri, const Base<uword,T2>& ci) const;
  195. template<typename T1, typename T2> arma_inline subview_elem2<eT,T1,T2> operator()(const Base<uword,T1>& ri, const Base<uword,T2>& ci);
  196. template<typename T1, typename T2> arma_inline const subview_elem2<eT,T1,T2> operator()(const Base<uword,T1>& ri, const Base<uword,T2>& ci) const;
  197. template<typename T1> arma_inline subview_elem2<eT,T1,T1> rows(const Base<uword,T1>& ri);
  198. template<typename T1> arma_inline const subview_elem2<eT,T1,T1> rows(const Base<uword,T1>& ri) const;
  199. template<typename T2> arma_inline subview_elem2<eT,T2,T2> cols(const Base<uword,T2>& ci);
  200. template<typename T2> arma_inline const subview_elem2<eT,T2,T2> cols(const Base<uword,T2>& ci) const;
  201. arma_inline subview_each1< Mat<eT>, 0 > each_col();
  202. arma_inline subview_each1< Mat<eT>, 1 > each_row();
  203. arma_inline const subview_each1< Mat<eT>, 0 > each_col() const;
  204. arma_inline const subview_each1< Mat<eT>, 1 > each_row() const;
  205. template<typename T1> inline subview_each2< Mat<eT>, 0, T1 > each_col(const Base<uword, T1>& indices);
  206. template<typename T1> inline subview_each2< Mat<eT>, 1, T1 > each_row(const Base<uword, T1>& indices);
  207. template<typename T1> inline const subview_each2< Mat<eT>, 0, T1 > each_col(const Base<uword, T1>& indices) const;
  208. template<typename T1> inline const subview_each2< Mat<eT>, 1, T1 > each_row(const Base<uword, T1>& indices) const;
  209. #if defined(ARMA_USE_CXX11)
  210. inline const Mat& each_col(const std::function< void( Col<eT>&) >& F);
  211. inline const Mat& each_col(const std::function< void(const Col<eT>&) >& F) const;
  212. inline const Mat& each_row(const std::function< void( Row<eT>&) >& F);
  213. inline const Mat& each_row(const std::function< void(const Row<eT>&) >& F) const;
  214. #endif
  215. arma_inline diagview<eT> diag(const sword in_id = 0);
  216. arma_inline const diagview<eT> diag(const sword in_id = 0) const;
  217. inline void swap_rows(const uword in_row1, const uword in_row2);
  218. inline void swap_cols(const uword in_col1, const uword in_col2);
  219. inline void shed_row(const uword row_num);
  220. inline void shed_col(const uword col_num);
  221. inline void shed_rows(const uword in_row1, const uword in_row2);
  222. inline void shed_cols(const uword in_col1, const uword in_col2);
  223. template<typename T1> inline void shed_rows(const Base<uword, T1>& indices);
  224. template<typename T1> inline void shed_cols(const Base<uword, T1>& indices);
  225. inline void insert_rows(const uword row_num, const uword N, const bool set_to_zero = true);
  226. inline void insert_cols(const uword col_num, const uword N, const bool set_to_zero = true);
  227. template<typename T1> inline void insert_rows(const uword row_num, const Base<eT,T1>& X);
  228. template<typename T1> inline void insert_cols(const uword col_num, const Base<eT,T1>& X);
  229. template<typename T1, typename gen_type> inline Mat(const Gen<T1, gen_type>& X);
  230. template<typename T1, typename gen_type> inline Mat& operator=(const Gen<T1, gen_type>& X);
  231. template<typename T1, typename gen_type> inline Mat& operator+=(const Gen<T1, gen_type>& X);
  232. template<typename T1, typename gen_type> inline Mat& operator-=(const Gen<T1, gen_type>& X);
  233. template<typename T1, typename gen_type> inline Mat& operator*=(const Gen<T1, gen_type>& X);
  234. template<typename T1, typename gen_type> inline Mat& operator%=(const Gen<T1, gen_type>& X);
  235. template<typename T1, typename gen_type> inline Mat& operator/=(const Gen<T1, gen_type>& X);
  236. template<typename T1, typename op_type> inline Mat(const Op<T1, op_type>& X);
  237. template<typename T1, typename op_type> inline Mat& operator=(const Op<T1, op_type>& X);
  238. template<typename T1, typename op_type> inline Mat& operator+=(const Op<T1, op_type>& X);
  239. template<typename T1, typename op_type> inline Mat& operator-=(const Op<T1, op_type>& X);
  240. template<typename T1, typename op_type> inline Mat& operator*=(const Op<T1, op_type>& X);
  241. template<typename T1, typename op_type> inline Mat& operator%=(const Op<T1, op_type>& X);
  242. template<typename T1, typename op_type> inline Mat& operator/=(const Op<T1, op_type>& X);
  243. template<typename T1, typename eop_type> inline Mat(const eOp<T1, eop_type>& X);
  244. template<typename T1, typename eop_type> inline Mat& operator=(const eOp<T1, eop_type>& X);
  245. template<typename T1, typename eop_type> inline Mat& operator+=(const eOp<T1, eop_type>& X);
  246. template<typename T1, typename eop_type> inline Mat& operator-=(const eOp<T1, eop_type>& X);
  247. template<typename T1, typename eop_type> inline Mat& operator*=(const eOp<T1, eop_type>& X);
  248. template<typename T1, typename eop_type> inline Mat& operator%=(const eOp<T1, eop_type>& X);
  249. template<typename T1, typename eop_type> inline Mat& operator/=(const eOp<T1, eop_type>& X);
  250. template<typename T1, typename op_type> inline Mat(const mtOp<eT, T1, op_type>& X);
  251. template<typename T1, typename op_type> inline Mat& operator=(const mtOp<eT, T1, op_type>& X);
  252. template<typename T1, typename op_type> inline Mat& operator+=(const mtOp<eT, T1, op_type>& X);
  253. template<typename T1, typename op_type> inline Mat& operator-=(const mtOp<eT, T1, op_type>& X);
  254. template<typename T1, typename op_type> inline Mat& operator*=(const mtOp<eT, T1, op_type>& X);
  255. template<typename T1, typename op_type> inline Mat& operator%=(const mtOp<eT, T1, op_type>& X);
  256. template<typename T1, typename op_type> inline Mat& operator/=(const mtOp<eT, T1, op_type>& X);
  257. template<typename T1, typename op_type> inline Mat(const CubeToMatOp<T1, op_type>& X);
  258. template<typename T1, typename op_type> inline Mat& operator=(const CubeToMatOp<T1, op_type>& X);
  259. template<typename T1, typename op_type> inline Mat& operator+=(const CubeToMatOp<T1, op_type>& X);
  260. template<typename T1, typename op_type> inline Mat& operator-=(const CubeToMatOp<T1, op_type>& X);
  261. template<typename T1, typename op_type> inline Mat& operator*=(const CubeToMatOp<T1, op_type>& X);
  262. template<typename T1, typename op_type> inline Mat& operator%=(const CubeToMatOp<T1, op_type>& X);
  263. template<typename T1, typename op_type> inline Mat& operator/=(const CubeToMatOp<T1, op_type>& X);
  264. template<typename T1, typename op_type> inline Mat(const SpToDOp<T1, op_type>& X);
  265. template<typename T1, typename op_type> inline Mat& operator=(const SpToDOp<T1, op_type>& X);
  266. template<typename T1, typename op_type> inline Mat& operator+=(const SpToDOp<T1, op_type>& X);
  267. template<typename T1, typename op_type> inline Mat& operator-=(const SpToDOp<T1, op_type>& X);
  268. template<typename T1, typename op_type> inline Mat& operator*=(const SpToDOp<T1, op_type>& X);
  269. template<typename T1, typename op_type> inline Mat& operator%=(const SpToDOp<T1, op_type>& X);
  270. template<typename T1, typename op_type> inline Mat& operator/=(const SpToDOp<T1, op_type>& X);
  271. template<typename T1, typename T2, typename glue_type> inline Mat(const Glue<T1, T2, glue_type>& X);
  272. template<typename T1, typename T2, typename glue_type> inline Mat& operator=(const Glue<T1, T2, glue_type>& X);
  273. template<typename T1, typename T2, typename glue_type> inline Mat& operator+=(const Glue<T1, T2, glue_type>& X);
  274. template<typename T1, typename T2, typename glue_type> inline Mat& operator-=(const Glue<T1, T2, glue_type>& X);
  275. template<typename T1, typename T2, typename glue_type> inline Mat& operator*=(const Glue<T1, T2, glue_type>& X);
  276. template<typename T1, typename T2, typename glue_type> inline Mat& operator%=(const Glue<T1, T2, glue_type>& X);
  277. template<typename T1, typename T2, typename glue_type> inline Mat& operator/=(const Glue<T1, T2, glue_type>& X);
  278. template<typename T1, typename T2> inline Mat& operator+=(const Glue<T1, T2, glue_times>& X);
  279. template<typename T1, typename T2> inline Mat& operator-=(const Glue<T1, T2, glue_times>& X);
  280. template<typename T1, typename T2, typename eglue_type> inline Mat(const eGlue<T1, T2, eglue_type>& X);
  281. template<typename T1, typename T2, typename eglue_type> inline Mat& operator=(const eGlue<T1, T2, eglue_type>& X);
  282. template<typename T1, typename T2, typename eglue_type> inline Mat& operator+=(const eGlue<T1, T2, eglue_type>& X);
  283. template<typename T1, typename T2, typename eglue_type> inline Mat& operator-=(const eGlue<T1, T2, eglue_type>& X);
  284. template<typename T1, typename T2, typename eglue_type> inline Mat& operator*=(const eGlue<T1, T2, eglue_type>& X);
  285. template<typename T1, typename T2, typename eglue_type> inline Mat& operator%=(const eGlue<T1, T2, eglue_type>& X);
  286. template<typename T1, typename T2, typename eglue_type> inline Mat& operator/=(const eGlue<T1, T2, eglue_type>& X);
  287. template<typename T1, typename T2, typename glue_type> inline Mat(const mtGlue<eT, T1, T2, glue_type>& X);
  288. template<typename T1, typename T2, typename glue_type> inline Mat& operator=(const mtGlue<eT, T1, T2, glue_type>& X);
  289. template<typename T1, typename T2, typename glue_type> inline Mat& operator+=(const mtGlue<eT, T1, T2, glue_type>& X);
  290. template<typename T1, typename T2, typename glue_type> inline Mat& operator-=(const mtGlue<eT, T1, T2, glue_type>& X);
  291. template<typename T1, typename T2, typename glue_type> inline Mat& operator*=(const mtGlue<eT, T1, T2, glue_type>& X);
  292. template<typename T1, typename T2, typename glue_type> inline Mat& operator%=(const mtGlue<eT, T1, T2, glue_type>& X);
  293. template<typename T1, typename T2, typename glue_type> inline Mat& operator/=(const mtGlue<eT, T1, T2, glue_type>& X);
  294. arma_inline arma_warn_unused const eT& at_alt (const uword ii) const;
  295. arma_inline arma_warn_unused eT& operator[] (const uword ii);
  296. arma_inline arma_warn_unused const eT& operator[] (const uword ii) const;
  297. arma_inline arma_warn_unused eT& at (const uword ii);
  298. arma_inline arma_warn_unused const eT& at (const uword ii) const;
  299. arma_inline arma_warn_unused eT& operator() (const uword ii);
  300. arma_inline arma_warn_unused const eT& operator() (const uword ii) const;
  301. arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col);
  302. arma_inline arma_warn_unused const eT& at (const uword in_row, const uword in_col) const;
  303. arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col);
  304. arma_inline arma_warn_unused const eT& operator() (const uword in_row, const uword in_col) const;
  305. arma_inline const Mat& operator++();
  306. arma_inline void operator++(int);
  307. arma_inline const Mat& operator--();
  308. arma_inline void operator--(int);
  309. arma_inline arma_warn_unused bool is_empty() const;
  310. arma_inline arma_warn_unused bool is_vec() const;
  311. arma_inline arma_warn_unused bool is_rowvec() const;
  312. arma_inline arma_warn_unused bool is_colvec() const;
  313. arma_inline arma_warn_unused bool is_square() const;
  314. inline arma_warn_unused bool is_finite() const;
  315. inline arma_warn_unused bool has_inf() const;
  316. inline arma_warn_unused bool has_nan() const;
  317. inline arma_warn_unused bool is_sorted(const char* direction = "ascend") const;
  318. inline arma_warn_unused bool is_sorted(const char* direction, const uword dim) const;
  319. template<typename comparator>
  320. inline arma_warn_unused bool is_sorted_helper(const comparator& comp, const uword dim) const;
  321. arma_inline arma_warn_unused bool in_range(const uword ii) const;
  322. arma_inline arma_warn_unused bool in_range(const span& x ) const;
  323. arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col) const;
  324. arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col) const;
  325. arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const;
  326. arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const;
  327. arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col, const SizeMat& s) const;
  328. arma_inline arma_warn_unused eT* colptr(const uword in_col);
  329. arma_inline arma_warn_unused const eT* colptr(const uword in_col) const;
  330. arma_inline arma_warn_unused eT* memptr();
  331. arma_inline arma_warn_unused const eT* memptr() const;
  332. arma_cold inline void impl_print( const std::string& extra_text) const;
  333. arma_cold inline void impl_print(std::ostream& user_stream, const std::string& extra_text) const;
  334. arma_cold inline void impl_raw_print( const std::string& extra_text) const;
  335. arma_cold inline void impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const;
  336. template<typename eT2, typename expr>
  337. inline void copy_size(const Base<eT2,expr>& X);
  338. inline void set_size(const uword in_elem);
  339. inline void set_size(const uword in_rows, const uword in_cols);
  340. inline void set_size(const SizeMat& s);
  341. inline void resize(const uword in_elem);
  342. inline void resize(const uword in_rows, const uword in_cols);
  343. inline void resize(const SizeMat& s);
  344. inline void reshape(const uword in_rows, const uword in_cols);
  345. inline void reshape(const SizeMat& s);
  346. 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
  347. template<typename functor> inline const Mat& for_each(functor F);
  348. template<typename functor> inline const Mat& for_each(functor F) const;
  349. template<typename functor> inline const Mat& transform(functor F);
  350. template<typename functor> inline const Mat& imbue(functor F);
  351. inline const Mat& replace(const eT old_val, const eT new_val);
  352. inline const Mat& clean(const pod_type threshold);
  353. inline const Mat& fill(const eT val);
  354. template<typename fill_type>
  355. inline const Mat& fill(const fill::fill_class<fill_type>& f);
  356. inline const Mat& zeros();
  357. inline const Mat& zeros(const uword in_elem);
  358. inline const Mat& zeros(const uword in_rows, const uword in_cols);
  359. inline const Mat& zeros(const SizeMat& s);
  360. inline const Mat& ones();
  361. inline const Mat& ones(const uword in_elem);
  362. inline const Mat& ones(const uword in_rows, const uword in_cols);
  363. inline const Mat& ones(const SizeMat& s);
  364. inline const Mat& randu();
  365. inline const Mat& randu(const uword in_elem);
  366. inline const Mat& randu(const uword in_rows, const uword in_cols);
  367. inline const Mat& randu(const SizeMat& s);
  368. inline const Mat& randn();
  369. inline const Mat& randn(const uword in_elem);
  370. inline const Mat& randn(const uword in_rows, const uword in_cols);
  371. inline const Mat& randn(const SizeMat& s);
  372. inline const Mat& eye();
  373. inline const Mat& eye(const uword in_rows, const uword in_cols);
  374. inline const Mat& eye(const SizeMat& s);
  375. inline arma_cold void reset();
  376. inline arma_cold void soft_reset();
  377. template<typename T1> inline void set_real(const Base<pod_type,T1>& X);
  378. template<typename T1> inline void set_imag(const Base<pod_type,T1>& X);
  379. inline arma_warn_unused eT min() const;
  380. inline arma_warn_unused eT max() const;
  381. inline eT min(uword& index_of_min_val) const;
  382. inline eT max(uword& index_of_max_val) const;
  383. inline eT min(uword& row_of_min_val, uword& col_of_min_val) const;
  384. inline eT max(uword& row_of_max_val, uword& col_of_max_val) const;
  385. inline arma_cold bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const;
  386. inline arma_cold bool save(const hdf5_name& spec, const file_type type = hdf5_binary, const bool print_status = true) const;
  387. inline arma_cold bool save(const csv_name& spec, const file_type type = csv_ascii, const bool print_status = true) const;
  388. inline arma_cold bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const;
  389. inline arma_cold bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true);
  390. inline arma_cold bool load(const hdf5_name& spec, const file_type type = hdf5_binary, const bool print_status = true);
  391. inline arma_cold bool load(const csv_name& spec, const file_type type = csv_ascii, const bool print_status = true);
  392. inline arma_cold bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true);
  393. inline arma_cold bool quiet_save(const std::string name, const file_type type = arma_binary) const;
  394. inline arma_cold bool quiet_save(const hdf5_name& spec, const file_type type = hdf5_binary) const;
  395. inline arma_cold bool quiet_save(const csv_name& spec, const file_type type = csv_ascii) const;
  396. inline arma_cold bool quiet_save( std::ostream& os, const file_type type = arma_binary) const;
  397. inline arma_cold bool quiet_load(const std::string name, const file_type type = auto_detect);
  398. inline arma_cold bool quiet_load(const hdf5_name& spec, const file_type type = hdf5_binary);
  399. inline arma_cold bool quiet_load(const csv_name& spec, const file_type type = csv_ascii);
  400. inline arma_cold bool quiet_load( std::istream& is, const file_type type = auto_detect);
  401. // for container-like functionality
  402. typedef eT value_type;
  403. typedef uword size_type;
  404. typedef eT* iterator;
  405. typedef const eT* const_iterator;
  406. typedef eT* col_iterator;
  407. typedef const eT* const_col_iterator;
  408. class const_row_iterator;
  409. class row_iterator
  410. {
  411. public:
  412. inline row_iterator();
  413. inline row_iterator(const row_iterator& X);
  414. inline row_iterator(Mat<eT>& in_M, const uword in_row, const uword in_col);
  415. inline arma_warn_unused eT& operator* ();
  416. inline row_iterator& operator++();
  417. inline arma_warn_unused row_iterator operator++(int);
  418. inline row_iterator& operator--();
  419. inline arma_warn_unused row_iterator operator--(int);
  420. inline arma_warn_unused bool operator!=(const row_iterator& X) const;
  421. inline arma_warn_unused bool operator==(const row_iterator& X) const;
  422. inline arma_warn_unused bool operator!=(const const_row_iterator& X) const;
  423. inline arma_warn_unused bool operator==(const const_row_iterator& X) const;
  424. typedef std::bidirectional_iterator_tag iterator_category;
  425. typedef eT value_type;
  426. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  427. typedef eT* pointer;
  428. typedef eT& reference;
  429. arma_aligned Mat<eT>* M;
  430. arma_aligned uword current_row;
  431. arma_aligned uword current_col;
  432. };
  433. class const_row_iterator
  434. {
  435. public:
  436. inline const_row_iterator();
  437. inline const_row_iterator(const row_iterator& X);
  438. inline const_row_iterator(const const_row_iterator& X);
  439. inline const_row_iterator(const Mat<eT>& in_M, const uword in_row, const uword in_col);
  440. inline arma_warn_unused const eT& operator*() const;
  441. inline const_row_iterator& operator++();
  442. inline arma_warn_unused const_row_iterator operator++(int);
  443. inline const_row_iterator& operator--();
  444. inline arma_warn_unused const_row_iterator operator--(int);
  445. inline arma_warn_unused bool operator!=(const row_iterator& X) const;
  446. inline arma_warn_unused bool operator==(const row_iterator& X) const;
  447. inline arma_warn_unused bool operator!=(const const_row_iterator& X) const;
  448. inline arma_warn_unused bool operator==(const const_row_iterator& X) const;
  449. typedef std::bidirectional_iterator_tag iterator_category;
  450. typedef eT value_type;
  451. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  452. typedef const eT* pointer;
  453. typedef const eT& reference;
  454. arma_aligned const Mat<eT>* M;
  455. arma_aligned uword current_row;
  456. arma_aligned uword current_col;
  457. };
  458. class const_row_col_iterator;
  459. class row_col_iterator
  460. {
  461. public:
  462. inline row_col_iterator();
  463. inline row_col_iterator(const row_col_iterator& in_it);
  464. inline row_col_iterator(Mat<eT>& in_M, const uword row = 0, const uword col = 0);
  465. inline arma_warn_unused eT& operator*();
  466. inline row_col_iterator& operator++();
  467. inline arma_warn_unused row_col_iterator operator++(int);
  468. inline row_col_iterator& operator--();
  469. inline arma_warn_unused row_col_iterator operator--(int);
  470. inline arma_warn_unused uword row() const;
  471. inline arma_warn_unused uword col() const;
  472. inline arma_warn_unused bool operator==(const row_col_iterator& rhs) const;
  473. inline arma_warn_unused bool operator!=(const row_col_iterator& rhs) const;
  474. inline arma_warn_unused bool operator==(const const_row_col_iterator& rhs) const;
  475. inline arma_warn_unused bool operator!=(const const_row_col_iterator& rhs) const;
  476. typedef std::bidirectional_iterator_tag iterator_category;
  477. typedef eT value_type;
  478. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  479. typedef eT* pointer;
  480. typedef eT& reference;
  481. arma_aligned Mat<eT>* M;
  482. arma_aligned eT* current_ptr;
  483. arma_aligned uword current_col;
  484. arma_aligned uword current_row;
  485. };
  486. class const_row_col_iterator
  487. {
  488. public:
  489. inline const_row_col_iterator();
  490. inline const_row_col_iterator(const row_col_iterator& in_it);
  491. inline const_row_col_iterator(const const_row_col_iterator& in_it);
  492. inline const_row_col_iterator(const Mat<eT>& in_M, const uword row = 0, const uword col = 0);
  493. inline arma_warn_unused const eT& operator*() const;
  494. inline const_row_col_iterator& operator++();
  495. inline arma_warn_unused const_row_col_iterator operator++(int);
  496. inline const_row_col_iterator& operator--();
  497. inline arma_warn_unused const_row_col_iterator operator--(int);
  498. inline arma_warn_unused uword row() const;
  499. inline arma_warn_unused uword col() const;
  500. inline arma_warn_unused bool operator==(const const_row_col_iterator& rhs) const;
  501. inline arma_warn_unused bool operator!=(const const_row_col_iterator& rhs) const;
  502. inline arma_warn_unused bool operator==(const row_col_iterator& rhs) const;
  503. inline arma_warn_unused bool operator!=(const row_col_iterator& rhs) const;
  504. // So that we satisfy the STL iterator types.
  505. typedef std::bidirectional_iterator_tag iterator_category;
  506. typedef eT value_type;
  507. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  508. typedef const eT* pointer;
  509. typedef const eT& reference;
  510. arma_aligned const Mat<eT>* M;
  511. arma_aligned const eT* current_ptr;
  512. arma_aligned uword current_col;
  513. arma_aligned uword current_row;
  514. };
  515. inline iterator begin();
  516. inline const_iterator begin() const;
  517. inline const_iterator cbegin() const;
  518. inline iterator end();
  519. inline const_iterator end() const;
  520. inline const_iterator cend() const;
  521. inline col_iterator begin_col(const uword col_num);
  522. inline const_col_iterator begin_col(const uword col_num) const;
  523. inline col_iterator end_col (const uword col_num);
  524. inline const_col_iterator end_col (const uword col_num) const;
  525. inline row_iterator begin_row(const uword row_num);
  526. inline const_row_iterator begin_row(const uword row_num) const;
  527. inline row_iterator end_row (const uword row_num);
  528. inline const_row_iterator end_row (const uword row_num) const;
  529. inline row_col_iterator begin_row_col();
  530. inline const_row_col_iterator begin_row_col() const;
  531. inline row_col_iterator end_row_col();
  532. inline const_row_col_iterator end_row_col() const;
  533. inline void clear();
  534. inline bool empty() const;
  535. inline uword size() const;
  536. inline eT& front();
  537. inline const eT& front() const;
  538. inline eT& back();
  539. inline const eT& back() const;
  540. inline void swap(Mat& B);
  541. inline void steal_mem(Mat& X); //!< don't use this unless you're writing code internal to Armadillo
  542. inline void steal_mem_col(Mat& X, const uword max_n_rows);
  543. template<uword fixed_n_rows, uword fixed_n_cols> class fixed;
  544. protected:
  545. inline void init_cold();
  546. inline void init_warm(uword in_rows, uword in_cols);
  547. inline arma_cold void init(const std::string& text);
  548. #if defined(ARMA_USE_CXX11)
  549. inline void init(const std::initializer_list<eT>& list);
  550. inline void init(const std::initializer_list< std::initializer_list<eT> >& list);
  551. #endif
  552. template<typename T1, typename T2>
  553. inline void init(const Base<pod_type,T1>& A, const Base<pod_type,T2>& B);
  554. inline Mat(const char junk, const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols);
  555. inline Mat(const arma_vec_indicator&, const uhword in_vec_state);
  556. inline Mat(const arma_vec_indicator&, const uword in_n_rows, const uword in_n_cols, const uhword in_vec_state);
  557. inline Mat(const arma_fixed_indicator&, const uword in_n_rows, const uword in_n_cols, const uhword in_vec_state, const eT* in_mem);
  558. friend class Cube<eT>;
  559. friend class subview_cube<eT>;
  560. friend class glue_join;
  561. friend class op_strans;
  562. friend class op_htrans;
  563. friend class op_resize;
  564. friend class op_mean;
  565. friend class op_max;
  566. friend class op_min;
  567. public:
  568. #ifdef ARMA_EXTRA_MAT_PROTO
  569. #include ARMA_INCFILE_WRAP(ARMA_EXTRA_MAT_PROTO)
  570. #endif
  571. };
  572. template<typename eT>
  573. template<uword fixed_n_rows, uword fixed_n_cols>
  574. class Mat<eT>::fixed : public Mat<eT>
  575. {
  576. private:
  577. static const uword fixed_n_elem = fixed_n_rows * fixed_n_cols;
  578. static const bool use_extra = (fixed_n_elem > arma_config::mat_prealloc);
  579. arma_align_mem eT mem_local_extra[ (use_extra) ? fixed_n_elem : 1 ];
  580. public:
  581. typedef fixed<fixed_n_rows, fixed_n_cols> Mat_fixed_type;
  582. typedef eT elem_type;
  583. typedef typename get_pod_type<eT>::result pod_type;
  584. static const bool is_col = (fixed_n_cols == 1);
  585. static const bool is_row = (fixed_n_rows == 1);
  586. static const bool is_xvec = false;
  587. static const uword n_rows; // value provided below the class definition
  588. static const uword n_cols; // value provided below the class definition
  589. static const uword n_elem; // value provided below the class definition
  590. arma_inline fixed();
  591. arma_inline fixed(const fixed<fixed_n_rows, fixed_n_cols>& X);
  592. template<typename fill_type> inline fixed(const fill::fill_class<fill_type>& f);
  593. template<typename T1> inline fixed(const Base<eT,T1>& A);
  594. template<typename T1, typename T2> inline fixed(const Base<pod_type,T1>& A, const Base<pod_type,T2>& B);
  595. inline fixed(const eT* aux_mem);
  596. inline fixed(const char* text);
  597. inline fixed(const std::string& text);
  598. using Mat<eT>::operator=;
  599. using Mat<eT>::operator();
  600. #if defined(ARMA_USE_CXX11)
  601. inline fixed(const std::initializer_list<eT>& list);
  602. inline Mat& operator=(const std::initializer_list<eT>& list);
  603. inline fixed(const std::initializer_list< std::initializer_list<eT> >& list);
  604. inline Mat& operator=(const std::initializer_list< std::initializer_list<eT> >& list);
  605. #endif
  606. arma_inline Mat& operator=(const fixed<fixed_n_rows, fixed_n_cols>& X);
  607. #if defined(ARMA_GOOD_COMPILER)
  608. template<typename T1, typename eop_type> inline Mat& operator=(const eOp<T1, eop_type>& X);
  609. template<typename T1, typename T2, typename eglue_type> inline Mat& operator=(const eGlue<T1, T2, eglue_type>& X);
  610. #endif
  611. arma_inline const Op< Mat_fixed_type, op_htrans > t() const;
  612. arma_inline const Op< Mat_fixed_type, op_htrans > ht() const;
  613. arma_inline const Op< Mat_fixed_type, op_strans > st() const;
  614. arma_inline arma_warn_unused const eT& at_alt (const uword i) const;
  615. arma_inline arma_warn_unused eT& operator[] (const uword i);
  616. arma_inline arma_warn_unused const eT& operator[] (const uword i) const;
  617. arma_inline arma_warn_unused eT& at (const uword i);
  618. arma_inline arma_warn_unused const eT& at (const uword i) const;
  619. arma_inline arma_warn_unused eT& operator() (const uword i);
  620. arma_inline arma_warn_unused const eT& operator() (const uword i) const;
  621. arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col);
  622. arma_inline arma_warn_unused const eT& at (const uword in_row, const uword in_col) const;
  623. arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col);
  624. arma_inline arma_warn_unused const eT& operator() (const uword in_row, const uword in_col) const;
  625. arma_inline arma_warn_unused eT* colptr(const uword in_col);
  626. arma_inline arma_warn_unused const eT* colptr(const uword in_col) const;
  627. arma_inline arma_warn_unused eT* memptr();
  628. arma_inline arma_warn_unused const eT* memptr() const;
  629. arma_inline arma_warn_unused bool is_vec() const;
  630. inline const Mat<eT>& fill(const eT val);
  631. inline const Mat<eT>& zeros();
  632. inline const Mat<eT>& ones();
  633. };
  634. // these definitions are outside of the class due to bizarre C++ rules;
  635. // C++17 has inline variables to address this shortcoming
  636. template<typename eT>
  637. template<uword fixed_n_rows, uword fixed_n_cols>
  638. const uword Mat<eT>::fixed<fixed_n_rows, fixed_n_cols>::n_rows = fixed_n_rows;
  639. template<typename eT>
  640. template<uword fixed_n_rows, uword fixed_n_cols>
  641. const uword Mat<eT>::fixed<fixed_n_rows, fixed_n_cols>::n_cols = fixed_n_cols;
  642. template<typename eT>
  643. template<uword fixed_n_rows, uword fixed_n_cols>
  644. const uword Mat<eT>::fixed<fixed_n_rows, fixed_n_cols>::n_elem = fixed_n_rows * fixed_n_cols;
  645. class Mat_aux
  646. {
  647. public:
  648. template<typename eT> inline static void prefix_pp(Mat<eT>& x);
  649. template<typename T> inline static void prefix_pp(Mat< std::complex<T> >& x);
  650. template<typename eT> inline static void postfix_pp(Mat<eT>& x);
  651. template<typename T> inline static void postfix_pp(Mat< std::complex<T> >& x);
  652. template<typename eT> inline static void prefix_mm(Mat<eT>& x);
  653. template<typename T> inline static void prefix_mm(Mat< std::complex<T> >& x);
  654. template<typename eT> inline static void postfix_mm(Mat<eT>& x);
  655. template<typename T> inline static void postfix_mm(Mat< std::complex<T> >& x);
  656. template<typename eT, typename T1> inline static void set_real(Mat<eT>& out, const Base<eT,T1>& X);
  657. template<typename T, typename T1> inline static void set_real(Mat< std::complex<T> >& out, const Base< T,T1>& X);
  658. template<typename eT, typename T1> inline static void set_imag(Mat<eT>& out, const Base<eT,T1>& X);
  659. template<typename T, typename T1> inline static void set_imag(Mat< std::complex<T> >& out, const Base< T,T1>& X);
  660. };
  661. //! @}