You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
19 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_sub_matrix.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Generic sub-matrices.
  30. */
  31. #ifndef GMM_SUB_MATRIX_H__
  32. #define GMM_SUB_MATRIX_H__
  33. #include "gmm_sub_vector.h"
  34. namespace gmm {
  35. /* ********************************************************************* */
  36. /* sub row matrices type */
  37. /* ********************************************************************* */
  38. template <typename PT, typename SUBI1, typename SUBI2>
  39. struct gen_sub_row_matrix {
  40. typedef gen_sub_row_matrix<PT, SUBI1, SUBI2> this_type;
  41. typedef typename std::iterator_traits<PT>::value_type M;
  42. typedef M * CPT;
  43. typedef typename std::iterator_traits<PT>::reference ref_M;
  44. typedef typename select_ref<typename linalg_traits<M>
  45. ::const_row_iterator, typename linalg_traits<M>::row_iterator,
  46. PT>::ref_type iterator;
  47. typedef typename linalg_traits<this_type>::reference reference;
  48. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  49. SUBI1 si1;
  50. SUBI2 si2;
  51. iterator begin_;
  52. porigin_type origin;
  53. reference operator()(size_type i, size_type j) const
  54. { return linalg_traits<M>::access(begin_ + si1.index(i), si2.index(j)); }
  55. size_type nrows(void) const { return si1.size(); }
  56. size_type ncols(void) const { return si2.size(); }
  57. gen_sub_row_matrix(ref_M m, const SUBI1 &s1, const SUBI2 &s2)
  58. : si1(s1), si2(s2), begin_(mat_row_begin(m)),
  59. origin(linalg_origin(m)) {}
  60. gen_sub_row_matrix() {}
  61. gen_sub_row_matrix(const gen_sub_row_matrix<CPT, SUBI1, SUBI2> &cr) :
  62. si1(cr.si1), si2(cr.si2), begin_(cr.begin_),origin(cr.origin) {}
  63. };
  64. template <typename PT, typename SUBI1, typename SUBI2>
  65. struct gen_sub_row_matrix_iterator {
  66. typedef gen_sub_row_matrix<PT, SUBI1, SUBI2> this_type;
  67. typedef typename modifiable_pointer<PT>::pointer MPT;
  68. typedef typename std::iterator_traits<PT>::value_type M;
  69. typedef typename select_ref<typename linalg_traits<M>
  70. ::const_row_iterator, typename linalg_traits<M>::row_iterator,
  71. PT>::ref_type ITER;
  72. typedef ITER value_type;
  73. typedef ITER *pointer;
  74. typedef ITER &reference;
  75. typedef ptrdiff_t difference_type;
  76. typedef size_t size_type;
  77. typedef std::random_access_iterator_tag iterator_category;
  78. typedef gen_sub_row_matrix_iterator<PT, SUBI1, SUBI2> iterator;
  79. ITER it;
  80. SUBI1 si1;
  81. SUBI2 si2;
  82. size_type ii;
  83. iterator operator ++(int) { iterator tmp = *this; ii++; return tmp; }
  84. iterator operator --(int) { iterator tmp = *this; ii--; return tmp; }
  85. iterator &operator ++() { ii++; return *this; }
  86. iterator &operator --() { ii--; return *this; }
  87. iterator &operator +=(difference_type i) { ii += i; return *this; }
  88. iterator &operator -=(difference_type i) { ii -= i; return *this; }
  89. iterator operator +(difference_type i) const
  90. { iterator itt = *this; return (itt += i); }
  91. iterator operator -(difference_type i) const
  92. { iterator itt = *this; return (itt -= i); }
  93. difference_type operator -(const iterator &i) const { return ii - i.ii; }
  94. ITER operator *() const { return it + si1.index(ii); }
  95. ITER operator [](int i) { return it + si1.index(ii+i); }
  96. bool operator ==(const iterator &i) const { return (ii == i.ii); }
  97. bool operator !=(const iterator &i) const { return !(i == *this); }
  98. bool operator < (const iterator &i) const { return (ii < i.ii); }
  99. gen_sub_row_matrix_iterator(void) {}
  100. gen_sub_row_matrix_iterator(const
  101. gen_sub_row_matrix_iterator<MPT, SUBI1, SUBI2> &itm)
  102. : it(itm.it), si1(itm.si1), si2(itm.si2), ii(itm.ii) {}
  103. gen_sub_row_matrix_iterator(const ITER &iter, const SUBI1 &s1,
  104. const SUBI2 &s2, size_type i)
  105. : it(iter), si1(s1), si2(s2), ii(i) { }
  106. };
  107. template <typename PT, typename SUBI1, typename SUBI2>
  108. struct linalg_traits<gen_sub_row_matrix<PT, SUBI1, SUBI2> > {
  109. typedef gen_sub_row_matrix<PT, SUBI1, SUBI2> this_type;
  110. typedef typename std::iterator_traits<PT>::value_type M;
  111. typedef typename which_reference<PT>::is_reference is_reference;
  112. typedef abstract_matrix linalg_type;
  113. typedef typename linalg_traits<M>::origin_type origin_type;
  114. typedef typename select_ref<const origin_type *, origin_type *,
  115. PT>::ref_type porigin_type;
  116. typedef typename linalg_traits<M>::value_type value_type;
  117. typedef typename select_ref<value_type,
  118. typename linalg_traits<M>::reference, PT>::ref_type reference;
  119. typedef abstract_null_type sub_col_type;
  120. typedef abstract_null_type col_iterator;
  121. typedef abstract_null_type const_sub_col_type;
  122. typedef abstract_null_type const_col_iterator;
  123. typedef typename sub_vector_type<const typename
  124. linalg_traits<M>::const_sub_row_type *, SUBI2>::vector_type
  125. const_sub_row_type;
  126. typedef typename select_ref<abstract_null_type,
  127. typename sub_vector_type<typename linalg_traits<M>::sub_row_type *,
  128. SUBI2>::vector_type, PT>::ref_type sub_row_type;
  129. typedef gen_sub_row_matrix_iterator<typename const_pointer<PT>::pointer,
  130. SUBI1, SUBI2> const_row_iterator;
  131. typedef typename select_ref<abstract_null_type,
  132. gen_sub_row_matrix_iterator<PT, SUBI1, SUBI2>, PT>::ref_type
  133. row_iterator;
  134. typedef typename linalg_traits<const_sub_row_type>::storage_type
  135. storage_type;
  136. typedef row_major sub_orientation;
  137. typedef linalg_true index_sorted;
  138. static size_type nrows(const this_type &m) { return m.nrows(); }
  139. static size_type ncols(const this_type &m) { return m.ncols(); }
  140. static const_sub_row_type row(const const_row_iterator &it)
  141. { return const_sub_row_type(linalg_traits<M>::row(*it), it.si2); }
  142. static sub_row_type row(const row_iterator &it)
  143. { return sub_row_type(linalg_traits<M>::row(*it), it.si2); }
  144. static const_row_iterator row_begin(const this_type &m)
  145. { return const_row_iterator(m.begin_, m.si1, m.si2, 0); }
  146. static row_iterator row_begin(this_type &m)
  147. { return row_iterator(m.begin_, m.si1, m.si2, 0); }
  148. static const_row_iterator row_end(const this_type &m)
  149. { return const_row_iterator(m.begin_, m.si1, m.si2, m.nrows()); }
  150. static row_iterator row_end(this_type &m)
  151. { return row_iterator(m.begin_, m.si1, m.si2, m.nrows()); }
  152. static origin_type* origin(this_type &v) { return v.origin; }
  153. static const origin_type* origin(const this_type &v) { return v.origin; }
  154. static void do_clear(this_type &m) {
  155. row_iterator it = mat_row_begin(m), ite = mat_row_end(m);
  156. for (; it != ite; ++it) clear(row(it));
  157. }
  158. static value_type access(const const_row_iterator &itrow, size_type i)
  159. { return linalg_traits<M>::access(*itrow, itrow.si2.index(i)); }
  160. static reference access(const row_iterator &itrow, size_type i)
  161. { return linalg_traits<M>::access(*itrow, itrow.si2.index(i)); }
  162. };
  163. template <typename PT, typename SUBI1, typename SUBI2>
  164. std::ostream &operator <<(std::ostream &o,
  165. const gen_sub_row_matrix<PT, SUBI1, SUBI2>& m)
  166. { gmm::write(o,m); return o; }
  167. /* ********************************************************************* */
  168. /* sub column matrices type */
  169. /* ********************************************************************* */
  170. template <typename PT, typename SUBI1, typename SUBI2>
  171. struct gen_sub_col_matrix {
  172. typedef gen_sub_col_matrix<PT, SUBI1, SUBI2> this_type;
  173. typedef typename std::iterator_traits<PT>::value_type M;
  174. typedef M * CPT;
  175. typedef typename std::iterator_traits<PT>::reference ref_M;
  176. typedef typename select_ref<typename linalg_traits<M>
  177. ::const_col_iterator, typename linalg_traits<M>::col_iterator,
  178. PT>::ref_type iterator;
  179. typedef typename linalg_traits<this_type>::reference reference;
  180. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  181. SUBI1 si1;
  182. SUBI2 si2;
  183. iterator begin_;
  184. porigin_type origin;
  185. reference operator()(size_type i, size_type j) const
  186. { return linalg_traits<M>::access(begin_ + si2.index(j), si1.index(i)); }
  187. size_type nrows(void) const { return si1.size(); }
  188. size_type ncols(void) const { return si2.size(); }
  189. gen_sub_col_matrix(ref_M m, const SUBI1 &s1, const SUBI2 &s2)
  190. : si1(s1), si2(s2), begin_(mat_col_begin(m)),
  191. origin(linalg_origin(m)) {}
  192. gen_sub_col_matrix() {}
  193. gen_sub_col_matrix(const gen_sub_col_matrix<CPT, SUBI1, SUBI2> &cr) :
  194. si1(cr.si1), si2(cr.si2), begin_(cr.begin_),origin(cr.origin) {}
  195. };
  196. template <typename PT, typename SUBI1, typename SUBI2>
  197. struct gen_sub_col_matrix_iterator {
  198. typedef gen_sub_col_matrix<PT, SUBI1, SUBI2> this_type;
  199. typedef typename modifiable_pointer<PT>::pointer MPT;
  200. typedef typename std::iterator_traits<PT>::value_type M;
  201. typedef typename select_ref<typename linalg_traits<M>::const_col_iterator,
  202. typename linalg_traits<M>::col_iterator,
  203. PT>::ref_type ITER;
  204. typedef ITER value_type;
  205. typedef ITER *pointer;
  206. typedef ITER &reference;
  207. typedef ptrdiff_t difference_type;
  208. typedef size_t size_type;
  209. typedef std::random_access_iterator_tag iterator_category;
  210. typedef gen_sub_col_matrix_iterator<PT, SUBI1, SUBI2> iterator;
  211. ITER it;
  212. SUBI1 si1;
  213. SUBI2 si2;
  214. size_type ii;
  215. iterator operator ++(int) { iterator tmp = *this; ii++; return tmp; }
  216. iterator operator --(int) { iterator tmp = *this; ii--; return tmp; }
  217. iterator &operator ++() { ii++; return *this; }
  218. iterator &operator --() { ii--; return *this; }
  219. iterator &operator +=(difference_type i) { ii += i; return *this; }
  220. iterator &operator -=(difference_type i) { ii -= i; return *this; }
  221. iterator operator +(difference_type i) const
  222. { iterator itt = *this; return (itt += i); }
  223. iterator operator -(difference_type i) const
  224. { iterator itt = *this; return (itt -= i); }
  225. difference_type operator -(const iterator &i) const { return ii - i.ii; }
  226. ITER operator *() const { return it + si2.index(ii); }
  227. ITER operator [](int i) { return it + si2.index(ii+i); }
  228. bool operator ==(const iterator &i) const { return (ii == i.ii); }
  229. bool operator !=(const iterator &i) const { return !(i == *this); }
  230. bool operator < (const iterator &i) const { return (ii < i.ii); }
  231. gen_sub_col_matrix_iterator(void) {}
  232. gen_sub_col_matrix_iterator(const
  233. gen_sub_col_matrix_iterator<MPT, SUBI1, SUBI2> &itm)
  234. : it(itm.it), si1(itm.si1), si2(itm.si2), ii(itm.ii) {}
  235. gen_sub_col_matrix_iterator(const ITER &iter, const SUBI1 &s1,
  236. const SUBI2 &s2, size_type i)
  237. : it(iter), si1(s1), si2(s2), ii(i) { }
  238. };
  239. template <typename PT, typename SUBI1, typename SUBI2>
  240. struct linalg_traits<gen_sub_col_matrix<PT, SUBI1, SUBI2> > {
  241. typedef gen_sub_col_matrix<PT, SUBI1, SUBI2> this_type;
  242. typedef typename std::iterator_traits<PT>::value_type M;
  243. typedef typename linalg_traits<M>::origin_type origin_type;
  244. typedef typename select_ref<const origin_type *, origin_type *,
  245. PT>::ref_type porigin_type;
  246. typedef typename which_reference<PT>::is_reference is_reference;
  247. typedef abstract_matrix linalg_type;
  248. typedef typename linalg_traits<M>::value_type value_type;
  249. typedef typename select_ref<value_type,
  250. typename linalg_traits<M>::reference, PT>::ref_type reference;
  251. typedef abstract_null_type sub_row_type;
  252. typedef abstract_null_type row_iterator;
  253. typedef abstract_null_type const_sub_row_type;
  254. typedef abstract_null_type const_row_iterator;
  255. typedef typename sub_vector_type<const typename
  256. linalg_traits<M>::const_sub_col_type *, SUBI1>::vector_type
  257. const_sub_col_type;
  258. typedef typename select_ref<abstract_null_type,
  259. typename sub_vector_type<typename linalg_traits<M>::sub_col_type *,
  260. SUBI1>::vector_type, PT>::ref_type sub_col_type;
  261. typedef gen_sub_col_matrix_iterator<typename const_pointer<PT>::pointer,
  262. SUBI1, SUBI2> const_col_iterator;
  263. typedef typename select_ref<abstract_null_type,
  264. gen_sub_col_matrix_iterator<PT, SUBI1, SUBI2>, PT>::ref_type
  265. col_iterator;
  266. typedef col_major sub_orientation;
  267. typedef linalg_true index_sorted;
  268. typedef typename linalg_traits<const_sub_col_type>::storage_type
  269. storage_type;
  270. static size_type nrows(const this_type &m) { return m.nrows(); }
  271. static size_type ncols(const this_type &m) { return m.ncols(); }
  272. static const_sub_col_type col(const const_col_iterator &it)
  273. { return const_sub_col_type(linalg_traits<M>::col(*it), it.si1); }
  274. static sub_col_type col(const col_iterator &it)
  275. { return sub_col_type(linalg_traits<M>::col(*it), it.si1); }
  276. static const_col_iterator col_begin(const this_type &m)
  277. { return const_col_iterator(m.begin_, m.si1, m.si2, 0); }
  278. static col_iterator col_begin(this_type &m)
  279. { return col_iterator(m.begin_, m.si1, m.si2, 0); }
  280. static const_col_iterator col_end(const this_type &m)
  281. { return const_col_iterator(m.begin_, m.si1, m.si2, m.ncols()); }
  282. static col_iterator col_end(this_type &m)
  283. { return col_iterator(m.begin_, m.si1, m.si2, m.ncols()); }
  284. static origin_type* origin(this_type &v) { return v.origin; }
  285. static const origin_type* origin(const this_type &v) { return v.origin; }
  286. static void do_clear(this_type &m) {
  287. col_iterator it = mat_col_begin(m), ite = mat_col_end(m);
  288. for (; it != ite; ++it) clear(col(it));
  289. }
  290. static value_type access(const const_col_iterator &itcol, size_type i)
  291. { return linalg_traits<M>::access(*itcol, itcol.si1.index(i)); }
  292. static reference access(const col_iterator &itcol, size_type i)
  293. { return linalg_traits<M>::access(*itcol, itcol.si1.index(i)); }
  294. };
  295. template <typename PT, typename SUBI1, typename SUBI2> std::ostream &operator <<
  296. (std::ostream &o, const gen_sub_col_matrix<PT, SUBI1, SUBI2>& m)
  297. { gmm::write(o,m); return o; }
  298. /* ******************************************************************** */
  299. /* sub matrices */
  300. /* ******************************************************************** */
  301. template <typename PT, typename SUBI1, typename SUBI2, typename ST>
  302. struct sub_matrix_type_ {
  303. typedef abstract_null_type return_type;
  304. };
  305. template <typename PT, typename SUBI1, typename SUBI2>
  306. struct sub_matrix_type_<PT, SUBI1, SUBI2, col_major>
  307. { typedef gen_sub_col_matrix<PT, SUBI1, SUBI2> matrix_type; };
  308. template <typename PT, typename SUBI1, typename SUBI2>
  309. struct sub_matrix_type_<PT, SUBI1, SUBI2, row_major>
  310. { typedef gen_sub_row_matrix<PT, SUBI1, SUBI2> matrix_type; };
  311. template <typename PT, typename SUBI1, typename SUBI2>
  312. struct sub_matrix_type {
  313. typedef typename std::iterator_traits<PT>::value_type M;
  314. typedef typename sub_matrix_type_<PT, SUBI1, SUBI2,
  315. typename principal_orientation_type<typename
  316. linalg_traits<M>::sub_orientation>::potype>::matrix_type matrix_type;
  317. };
  318. template <typename M, typename SUBI1, typename SUBI2> inline
  319. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI2>
  320. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>::matrix_type,
  321. M *>::return_type
  322. sub_matrix(M &m, const SUBI1 &si1, const SUBI2 &si2) {
  323. GMM_ASSERT2(si1.last() <= mat_nrows(m) && si2.last() <= mat_ncols(m),
  324. "sub matrix too large");
  325. return typename select_return<typename sub_matrix_type<const M *, SUBI1,
  326. SUBI2>::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>
  327. ::matrix_type, M *>::return_type(linalg_cast(m), si1, si2);
  328. }
  329. template <typename M, typename SUBI1, typename SUBI2> inline
  330. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI2>
  331. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>::matrix_type,
  332. const M *>::return_type
  333. sub_matrix(const M &m, const SUBI1 &si1, const SUBI2 &si2) {
  334. GMM_ASSERT2(si1.last() <= mat_nrows(m) && si2.last() <= mat_ncols(m),
  335. "sub matrix too large");
  336. return typename select_return<typename sub_matrix_type<const M *, SUBI1,
  337. SUBI2>::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>
  338. ::matrix_type, const M *>::return_type(linalg_cast(m), si1, si2);
  339. }
  340. template <typename M, typename SUBI1> inline
  341. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  342. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  343. M *>::return_type
  344. sub_matrix(M &m, const SUBI1 &si1) {
  345. GMM_ASSERT2(si1.last() <= mat_nrows(m) && si1.last() <= mat_ncols(m),
  346. "sub matrix too large");
  347. return typename select_return<typename sub_matrix_type<const M *, SUBI1,
  348. SUBI1>::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>
  349. ::matrix_type, M *>::return_type(linalg_cast(m), si1, si1);
  350. }
  351. template <typename M, typename SUBI1> inline
  352. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  353. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  354. const M *>::return_type
  355. sub_matrix(const M &m, const SUBI1 &si1) {
  356. GMM_ASSERT2(si1.last() <= mat_nrows(m) && si1.last() <= mat_ncols(m),
  357. "sub matrix too large");
  358. return typename select_return<typename sub_matrix_type<const M *, SUBI1,
  359. SUBI1>::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>
  360. ::matrix_type, const M *>::return_type(linalg_cast(m), si1, si1);
  361. }
  362. }
  363. #endif // GMM_SUB_MATRIX_H__