subview_bones.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 subview
  16. //! @{
  17. //! Class for storing data required to construct or apply operations to a submatrix
  18. //! (i.e. where the submatrix starts and ends as well as a reference/pointer to the original matrix),
  19. template<typename eT>
  20. class subview : public Base<eT, subview<eT> >
  21. {
  22. public:
  23. typedef eT elem_type;
  24. typedef typename get_pod_type<elem_type>::result pod_type;
  25. arma_aligned const Mat<eT>& m;
  26. static const bool is_row = false;
  27. static const bool is_col = false;
  28. static const bool is_xvec = false;
  29. const uword aux_row1;
  30. const uword aux_col1;
  31. const uword n_rows;
  32. const uword n_cols;
  33. const uword n_elem;
  34. protected:
  35. arma_inline subview(const Mat<eT>& in_m, const uword in_row1, const uword in_col1, const uword in_n_rows, const uword in_n_cols);
  36. public:
  37. inline ~subview();
  38. template<typename op_type > inline void inplace_op(const eT val );
  39. template<typename op_type, typename T1> inline void inplace_op(const Base<eT,T1>& x, const char* identifier);
  40. template<typename op_type > inline void inplace_op(const subview<eT>& x, const char* identifier);
  41. // deliberately returning void
  42. inline void operator= (const eT val);
  43. inline void operator+= (const eT val);
  44. inline void operator-= (const eT val);
  45. inline void operator*= (const eT val);
  46. inline void operator/= (const eT val);
  47. inline void operator= (const subview& x);
  48. inline void operator+= (const subview& x);
  49. inline void operator-= (const subview& x);
  50. inline void operator%= (const subview& x);
  51. inline void operator/= (const subview& x);
  52. template<typename T1> inline void operator= (const Base<eT,T1>& x);
  53. template<typename T1> inline void operator+= (const Base<eT,T1>& x);
  54. template<typename T1> inline void operator-= (const Base<eT,T1>& x);
  55. template<typename T1> inline void operator%= (const Base<eT,T1>& x);
  56. template<typename T1> inline void operator/= (const Base<eT,T1>& x);
  57. template<typename T1> inline void operator= (const SpBase<eT,T1>& x);
  58. template<typename T1> inline void operator+= (const SpBase<eT,T1>& x);
  59. template<typename T1> inline void operator-= (const SpBase<eT,T1>& x);
  60. template<typename T1> inline void operator%= (const SpBase<eT,T1>& x);
  61. template<typename T1> inline void operator/= (const SpBase<eT,T1>& x);
  62. template<typename T1, typename gen_type>
  63. inline typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result operator=(const Gen<T1,gen_type>& x);
  64. inline static void extract(Mat<eT>& out, const subview& in);
  65. inline static void plus_inplace(Mat<eT>& out, const subview& in);
  66. inline static void minus_inplace(Mat<eT>& out, const subview& in);
  67. inline static void schur_inplace(Mat<eT>& out, const subview& in);
  68. inline static void div_inplace(Mat<eT>& out, const subview& in);
  69. template<typename functor> inline void for_each(functor F);
  70. template<typename functor> inline void for_each(functor F) const;
  71. template<typename functor> inline void transform(functor F);
  72. template<typename functor> inline void imbue(functor F);
  73. inline void replace(const eT old_val, const eT new_val);
  74. inline void clean(const pod_type threshold);
  75. inline void fill(const eT val);
  76. inline void zeros();
  77. inline void ones();
  78. inline void eye();
  79. inline void randu();
  80. inline void randn();
  81. inline eT at_alt (const uword ii) const;
  82. inline eT& operator[](const uword ii);
  83. inline eT operator[](const uword ii) const;
  84. inline eT& operator()(const uword ii);
  85. inline eT operator()(const uword ii) const;
  86. inline eT& operator()(const uword in_row, const uword in_col);
  87. inline eT operator()(const uword in_row, const uword in_col) const;
  88. inline eT& at(const uword in_row, const uword in_col);
  89. inline eT at(const uword in_row, const uword in_col) const;
  90. inline eT& front();
  91. inline eT front() const;
  92. inline eT& back();
  93. inline eT back() const;
  94. arma_inline eT* colptr(const uword in_col);
  95. arma_inline const eT* colptr(const uword in_col) const;
  96. template<typename eT2>
  97. inline bool check_overlap(const subview<eT2>& x) const;
  98. inline arma_warn_unused bool is_vec() const;
  99. inline arma_warn_unused bool is_finite() const;
  100. inline arma_warn_unused bool is_zero(const pod_type tol = 0) const;
  101. inline arma_warn_unused bool has_inf() const;
  102. inline arma_warn_unused bool has_nan() const;
  103. inline subview_row<eT> row(const uword row_num);
  104. inline const subview_row<eT> row(const uword row_num) const;
  105. inline subview_row<eT> operator()(const uword row_num, const span& col_span);
  106. inline const subview_row<eT> operator()(const uword row_num, const span& col_span) const;
  107. inline subview_col<eT> col(const uword col_num);
  108. inline const subview_col<eT> col(const uword col_num) const;
  109. inline subview_col<eT> operator()(const span& row_span, const uword col_num);
  110. inline const subview_col<eT> operator()(const span& row_span, const uword col_num) const;
  111. inline Col<eT> unsafe_col(const uword col_num);
  112. inline const Col<eT> unsafe_col(const uword col_num) const;
  113. inline subview<eT> rows(const uword in_row1, const uword in_row2);
  114. inline const subview<eT> rows(const uword in_row1, const uword in_row2) const;
  115. inline subview<eT> cols(const uword in_col1, const uword in_col2);
  116. inline const subview<eT> cols(const uword in_col1, const uword in_col2) const;
  117. inline subview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2);
  118. inline const subview<eT> submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const;
  119. inline subview<eT> submat (const span& row_span, const span& col_span);
  120. inline const subview<eT> submat (const span& row_span, const span& col_span) const;
  121. inline subview<eT> operator()(const span& row_span, const span& col_span);
  122. inline const subview<eT> operator()(const span& row_span, const span& col_span) const;
  123. inline subview_each1< subview<eT>, 0 > each_col();
  124. inline subview_each1< subview<eT>, 1 > each_row();
  125. template<typename T1> inline subview_each2< subview<eT>, 0, T1 > each_col(const Base<uword, T1>& indices);
  126. template<typename T1> inline subview_each2< subview<eT>, 1, T1 > each_row(const Base<uword, T1>& indices);
  127. #if defined(ARMA_USE_CXX11)
  128. inline void each_col(const std::function< void( Col<eT>&) >& F);
  129. inline void each_col(const std::function< void(const Col<eT>&) >& F) const;
  130. inline void each_row(const std::function< void( Row<eT>&) >& F);
  131. inline void each_row(const std::function< void(const Row<eT>&) >& F) const;
  132. #endif
  133. inline diagview<eT> diag(const sword in_id = 0);
  134. inline const diagview<eT> diag(const sword in_id = 0) const;
  135. inline void swap_rows(const uword in_row1, const uword in_row2);
  136. inline void swap_cols(const uword in_col1, const uword in_col2);
  137. class const_iterator;
  138. class iterator
  139. {
  140. public:
  141. inline iterator();
  142. inline iterator(const iterator& X);
  143. inline iterator(subview<eT>& in_sv, const uword in_row, const uword in_col);
  144. inline arma_warn_unused eT& operator*();
  145. inline iterator& operator++();
  146. inline arma_warn_unused iterator operator++(int);
  147. inline arma_warn_unused bool operator==(const iterator& rhs) const;
  148. inline arma_warn_unused bool operator!=(const iterator& rhs) const;
  149. inline arma_warn_unused bool operator==(const const_iterator& rhs) const;
  150. inline arma_warn_unused bool operator!=(const const_iterator& rhs) const;
  151. typedef std::forward_iterator_tag iterator_category;
  152. typedef eT value_type;
  153. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  154. typedef eT* pointer;
  155. typedef eT& reference;
  156. arma_aligned Mat<eT>* M;
  157. arma_aligned eT* current_ptr;
  158. arma_aligned uword current_row;
  159. arma_aligned uword current_col;
  160. arma_aligned const uword aux_row1;
  161. arma_aligned const uword aux_row2_p1;
  162. };
  163. class const_iterator
  164. {
  165. public:
  166. inline const_iterator();
  167. inline const_iterator(const iterator& X);
  168. inline const_iterator(const const_iterator& X);
  169. inline const_iterator(const subview<eT>& in_sv, const uword in_row, const uword in_col);
  170. inline arma_warn_unused const eT& operator*();
  171. inline const_iterator& operator++();
  172. inline arma_warn_unused const_iterator operator++(int);
  173. inline arma_warn_unused bool operator==(const iterator& rhs) const;
  174. inline arma_warn_unused bool operator!=(const iterator& rhs) const;
  175. inline arma_warn_unused bool operator==(const const_iterator& rhs) const;
  176. inline arma_warn_unused bool operator!=(const const_iterator& rhs) const;
  177. // So that we satisfy the STL iterator types.
  178. typedef std::forward_iterator_tag iterator_category;
  179. typedef eT value_type;
  180. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  181. typedef const eT* pointer;
  182. typedef const eT& reference;
  183. arma_aligned const Mat<eT>* M;
  184. arma_aligned const eT* current_ptr;
  185. arma_aligned uword current_row;
  186. arma_aligned uword current_col;
  187. arma_aligned const uword aux_row1;
  188. arma_aligned const uword aux_row2_p1;
  189. };
  190. class const_row_iterator;
  191. class row_iterator
  192. {
  193. public:
  194. inline row_iterator();
  195. inline row_iterator(const row_iterator& X);
  196. inline row_iterator(subview<eT>& in_sv, const uword in_row, const uword in_col);
  197. inline arma_warn_unused eT& operator* ();
  198. inline row_iterator& operator++();
  199. inline arma_warn_unused row_iterator operator++(int);
  200. inline arma_warn_unused bool operator!=(const row_iterator& X) const;
  201. inline arma_warn_unused bool operator==(const row_iterator& X) const;
  202. inline arma_warn_unused bool operator!=(const const_row_iterator& X) const;
  203. inline arma_warn_unused bool operator==(const const_row_iterator& X) const;
  204. typedef std::forward_iterator_tag iterator_category;
  205. typedef eT value_type;
  206. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  207. typedef eT* pointer;
  208. typedef eT& reference;
  209. arma_aligned Mat<eT>* M;
  210. arma_aligned uword current_row;
  211. arma_aligned uword current_col;
  212. arma_aligned const uword aux_col1;
  213. arma_aligned const uword aux_col2_p1;
  214. };
  215. class const_row_iterator
  216. {
  217. public:
  218. inline const_row_iterator();
  219. inline const_row_iterator(const row_iterator& X);
  220. inline const_row_iterator(const const_row_iterator& X);
  221. inline const_row_iterator(const subview<eT>& in_sv, const uword in_row, const uword in_col);
  222. inline arma_warn_unused const eT& operator*() const;
  223. inline const_row_iterator& operator++();
  224. inline arma_warn_unused const_row_iterator operator++(int);
  225. inline arma_warn_unused bool operator!=(const row_iterator& X) const;
  226. inline arma_warn_unused bool operator==(const row_iterator& X) const;
  227. inline arma_warn_unused bool operator!=(const const_row_iterator& X) const;
  228. inline arma_warn_unused bool operator==(const const_row_iterator& X) const;
  229. typedef std::forward_iterator_tag iterator_category;
  230. typedef eT value_type;
  231. typedef std::ptrdiff_t difference_type; // TODO: not certain on this one
  232. typedef const eT* pointer;
  233. typedef const eT& reference;
  234. arma_aligned const Mat<eT>* M;
  235. arma_aligned uword current_row;
  236. arma_aligned uword current_col;
  237. arma_aligned const uword aux_col1;
  238. arma_aligned const uword aux_col2_p1;
  239. };
  240. inline iterator begin();
  241. inline const_iterator begin() const;
  242. inline const_iterator cbegin() const;
  243. inline iterator end();
  244. inline const_iterator end() const;
  245. inline const_iterator cend() const;
  246. private:
  247. friend class Mat<eT>;
  248. subview();
  249. };
  250. template<typename eT>
  251. class subview_col : public subview<eT>
  252. {
  253. public:
  254. typedef eT elem_type;
  255. typedef typename get_pod_type<elem_type>::result pod_type;
  256. static const bool is_row = false;
  257. static const bool is_col = true;
  258. static const bool is_xvec = false;
  259. const eT* colmem;
  260. inline void operator= (const subview<eT>& x);
  261. inline void operator= (const subview_col& x);
  262. inline void operator= (const eT val);
  263. template<typename T1>
  264. inline void operator= (const Base<eT,T1>& x);
  265. template<typename T1, typename gen_type>
  266. inline typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result operator=(const Gen<T1,gen_type>& x);
  267. arma_inline const Op<subview_col<eT>,op_htrans> t() const;
  268. arma_inline const Op<subview_col<eT>,op_htrans> ht() const;
  269. arma_inline const Op<subview_col<eT>,op_strans> st() const;
  270. arma_inline const Op<subview_col<eT>,op_strans> as_row() const;
  271. inline void fill(const eT val);
  272. inline void zeros();
  273. inline void ones();
  274. arma_inline eT at_alt (const uword i) const;
  275. arma_inline eT& operator[](const uword i);
  276. arma_inline eT operator[](const uword i) const;
  277. inline eT& operator()(const uword i);
  278. inline eT operator()(const uword i) const;
  279. inline eT& operator()(const uword in_row, const uword in_col);
  280. inline eT operator()(const uword in_row, const uword in_col) const;
  281. inline eT& at(const uword in_row, const uword in_col);
  282. inline eT at(const uword in_row, const uword in_col) const;
  283. arma_inline eT* colptr(const uword in_col);
  284. arma_inline const eT* colptr(const uword in_col) const;
  285. inline subview_col<eT> rows(const uword in_row1, const uword in_row2);
  286. inline const subview_col<eT> rows(const uword in_row1, const uword in_row2) const;
  287. inline subview_col<eT> subvec(const uword in_row1, const uword in_row2);
  288. inline const subview_col<eT> subvec(const uword in_row1, const uword in_row2) const;
  289. inline subview_col<eT> subvec(const uword start_row, const SizeMat& s);
  290. inline const subview_col<eT> subvec(const uword start_row, const SizeMat& s) const;
  291. inline subview_col<eT> head(const uword N);
  292. inline const subview_col<eT> head(const uword N) const;
  293. inline subview_col<eT> tail(const uword N);
  294. inline const subview_col<eT> tail(const uword N) const;
  295. inline arma_warn_unused eT min() const;
  296. inline arma_warn_unused eT max() const;
  297. inline eT min(uword& index_of_min_val) const;
  298. inline eT max(uword& index_of_max_val) const;
  299. inline arma_warn_unused uword index_min() const;
  300. inline arma_warn_unused uword index_max() const;
  301. protected:
  302. inline subview_col(const Mat<eT>& in_m, const uword in_col);
  303. inline subview_col(const Mat<eT>& in_m, const uword in_col, const uword in_row1, const uword in_n_rows);
  304. private:
  305. friend class Mat<eT>;
  306. friend class Col<eT>;
  307. friend class subview<eT>;
  308. subview_col();
  309. };
  310. template<typename eT>
  311. class subview_row : public subview<eT>
  312. {
  313. public:
  314. typedef eT elem_type;
  315. typedef typename get_pod_type<elem_type>::result pod_type;
  316. static const bool is_row = true;
  317. static const bool is_col = false;
  318. static const bool is_xvec = false;
  319. inline void operator= (const subview<eT>& x);
  320. inline void operator= (const subview_row& x);
  321. inline void operator= (const eT val);
  322. template<typename T1>
  323. inline void operator= (const Base<eT,T1>& x);
  324. template<typename T1, typename gen_type>
  325. inline typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result operator=(const Gen<T1,gen_type>& x);
  326. arma_inline const Op<subview_row<eT>,op_htrans> t() const;
  327. arma_inline const Op<subview_row<eT>,op_htrans> ht() const;
  328. arma_inline const Op<subview_row<eT>,op_strans> st() const;
  329. arma_inline const Op<subview_row<eT>,op_strans> as_col() const;
  330. inline eT at_alt (const uword i) const;
  331. inline eT& operator[](const uword i);
  332. inline eT operator[](const uword i) const;
  333. inline eT& operator()(const uword i);
  334. inline eT operator()(const uword i) const;
  335. inline eT& operator()(const uword in_row, const uword in_col);
  336. inline eT operator()(const uword in_row, const uword in_col) const;
  337. inline eT& at(const uword in_row, const uword in_col);
  338. inline eT at(const uword in_row, const uword in_col) const;
  339. inline subview_row<eT> cols(const uword in_col1, const uword in_col2);
  340. inline const subview_row<eT> cols(const uword in_col1, const uword in_col2) const;
  341. inline subview_row<eT> subvec(const uword in_col1, const uword in_col2);
  342. inline const subview_row<eT> subvec(const uword in_col1, const uword in_col2) const;
  343. inline subview_row<eT> subvec(const uword start_col, const SizeMat& s);
  344. inline const subview_row<eT> subvec(const uword start_col, const SizeMat& s) const;
  345. inline subview_row<eT> head(const uword N);
  346. inline const subview_row<eT> head(const uword N) const;
  347. inline subview_row<eT> tail(const uword N);
  348. inline const subview_row<eT> tail(const uword N) const;
  349. inline arma_warn_unused uword index_min() const;
  350. inline arma_warn_unused uword index_max() const;
  351. inline typename subview<eT>::row_iterator begin();
  352. inline typename subview<eT>::const_row_iterator begin() const;
  353. inline typename subview<eT>::const_row_iterator cbegin() const;
  354. inline typename subview<eT>::row_iterator end();
  355. inline typename subview<eT>::const_row_iterator end() const;
  356. inline typename subview<eT>::const_row_iterator cend() const;
  357. protected:
  358. inline subview_row(const Mat<eT>& in_m, const uword in_row);
  359. inline subview_row(const Mat<eT>& in_m, const uword in_row, const uword in_col1, const uword in_n_cols);
  360. private:
  361. friend class Mat<eT>;
  362. friend class Row<eT>;
  363. friend class subview<eT>;
  364. subview_row();
  365. };
  366. template<typename eT>
  367. class subview_row_strans : public Base<eT, subview_row_strans<eT> >
  368. {
  369. public:
  370. typedef eT elem_type;
  371. typedef typename get_pod_type<elem_type>::result pod_type;
  372. static const bool is_row = false;
  373. static const bool is_col = true;
  374. static const bool is_xvec = false;
  375. arma_aligned const subview_row<eT>& sv_row;
  376. const uword n_rows; // equal to n_elem
  377. const uword n_elem;
  378. static const uword n_cols = 1;
  379. inline explicit subview_row_strans(const subview_row<eT>& in_sv_row);
  380. inline void extract(Mat<eT>& out) const;
  381. inline eT at_alt (const uword i) const;
  382. inline eT operator[](const uword i) const;
  383. inline eT operator()(const uword i) const;
  384. inline eT operator()(const uword in_row, const uword in_col) const;
  385. inline eT at(const uword in_row, const uword in_col) const;
  386. };
  387. template<typename eT>
  388. class subview_row_htrans : public Base<eT, subview_row_htrans<eT> >
  389. {
  390. public:
  391. typedef eT elem_type;
  392. typedef typename get_pod_type<elem_type>::result pod_type;
  393. static const bool is_row = false;
  394. static const bool is_col = true;
  395. static const bool is_xvec = false;
  396. arma_aligned const subview_row<eT>& sv_row;
  397. const uword n_rows; // equal to n_elem
  398. const uword n_elem;
  399. static const uword n_cols = 1;
  400. inline explicit subview_row_htrans(const subview_row<eT>& in_sv_row);
  401. inline void extract(Mat<eT>& out) const;
  402. inline eT at_alt (const uword i) const;
  403. inline eT operator[](const uword i) const;
  404. inline eT operator()(const uword i) const;
  405. inline eT operator()(const uword in_row, const uword in_col) const;
  406. inline eT at(const uword in_row, const uword in_col) const;
  407. };
  408. //! @}