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.

244 lines
11 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_transposed.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date November 10, 2002.
  29. @brief Generic transposed matrices
  30. */
  31. #ifndef GMM_TRANSPOSED_H__
  32. #define GMM_TRANSPOSED_H__
  33. #include "gmm_def.h"
  34. namespace gmm {
  35. /* ********************************************************************* */
  36. /* transposed reference */
  37. /* ********************************************************************* */
  38. template <typename PT> struct transposed_row_ref {
  39. typedef transposed_row_ref<PT> this_type;
  40. typedef typename std::iterator_traits<PT>::value_type M;
  41. typedef M * CPT;
  42. typedef typename std::iterator_traits<PT>::reference ref_M;
  43. typedef typename select_ref<typename linalg_traits<this_type>
  44. ::const_col_iterator, typename linalg_traits<this_type>
  45. ::col_iterator, PT>::ref_type iterator;
  46. typedef typename linalg_traits<this_type>::reference reference;
  47. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  48. iterator begin_, end_;
  49. porigin_type origin;
  50. size_type nr, nc;
  51. transposed_row_ref(ref_M m)
  52. : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
  53. origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
  54. transposed_row_ref(const transposed_row_ref<CPT> &cr) :
  55. begin_(cr.begin_),end_(cr.end_), origin(cr.origin),nr(cr.nr),nc(cr.nc) {}
  56. reference operator()(size_type i, size_type j) const
  57. { return linalg_traits<M>::access(begin_+j, i); }
  58. };
  59. template <typename PT> struct linalg_traits<transposed_row_ref<PT> > {
  60. typedef transposed_row_ref<PT> this_type;
  61. typedef typename std::iterator_traits<PT>::value_type M;
  62. typedef typename linalg_traits<M>::origin_type origin_type;
  63. typedef typename select_ref<const origin_type *, origin_type *,
  64. PT>::ref_type porigin_type;
  65. typedef typename which_reference<PT>::is_reference is_reference;
  66. typedef abstract_matrix linalg_type;
  67. typedef typename linalg_traits<M>::value_type value_type;
  68. typedef typename select_ref<value_type,
  69. typename linalg_traits<M>::reference, PT>::ref_type reference;
  70. typedef typename linalg_traits<M>::storage_type storage_type;
  71. typedef abstract_null_type sub_row_type;
  72. typedef abstract_null_type const_sub_row_type;
  73. typedef abstract_null_type row_iterator;
  74. typedef abstract_null_type const_row_iterator;
  75. typedef typename linalg_traits<M>::const_sub_row_type const_sub_col_type;
  76. typedef typename select_ref<abstract_null_type, typename
  77. linalg_traits<M>::sub_row_type, PT>::ref_type sub_col_type;
  78. typedef typename linalg_traits<M>::const_row_iterator const_col_iterator;
  79. typedef typename select_ref<abstract_null_type, typename
  80. linalg_traits<M>::row_iterator, PT>::ref_type col_iterator;
  81. typedef col_major sub_orientation;
  82. typedef typename linalg_traits<M>::index_sorted index_sorted;
  83. static size_type ncols(const this_type &v) { return v.nc; }
  84. static size_type nrows(const this_type &v) { return v.nr; }
  85. static const_sub_col_type col(const const_col_iterator &it)
  86. { return linalg_traits<M>::row(it); }
  87. static sub_col_type col(const col_iterator &it)
  88. { return linalg_traits<M>::row(it); }
  89. static col_iterator col_begin(this_type &m) { return m.begin_; }
  90. static col_iterator col_end(this_type &m) { return m.end_; }
  91. static const_col_iterator col_begin(const this_type &m)
  92. { return m.begin_; }
  93. static const_col_iterator col_end(const this_type &m) { return m.end_; }
  94. static origin_type* origin(this_type &v) { return v.origin; }
  95. static const origin_type* origin(const this_type &v) { return v.origin; }
  96. static void do_clear(this_type &v);
  97. static value_type access(const const_col_iterator &itcol, size_type i)
  98. { return linalg_traits<M>::access(itcol, i); }
  99. static reference access(const col_iterator &itcol, size_type i)
  100. { return linalg_traits<M>::access(itcol, i); }
  101. };
  102. template <typename PT>
  103. void linalg_traits<transposed_row_ref<PT> >::do_clear(this_type &v) {
  104. col_iterator it = mat_col_begin(v), ite = mat_col_end(v);
  105. for (; it != ite; ++it) clear(col(it));
  106. }
  107. template<typename PT> std::ostream &operator <<
  108. (std::ostream &o, const transposed_row_ref<PT>& m)
  109. { gmm::write(o,m); return o; }
  110. template <typename PT> struct transposed_col_ref {
  111. typedef transposed_col_ref<PT> this_type;
  112. typedef typename std::iterator_traits<PT>::value_type M;
  113. typedef M * CPT;
  114. typedef typename std::iterator_traits<PT>::reference ref_M;
  115. typedef typename select_ref<typename linalg_traits<this_type>
  116. ::const_row_iterator, typename linalg_traits<this_type>
  117. ::row_iterator, PT>::ref_type iterator;
  118. typedef typename linalg_traits<this_type>::reference reference;
  119. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  120. iterator begin_, end_;
  121. porigin_type origin;
  122. size_type nr, nc;
  123. transposed_col_ref(ref_M m)
  124. : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
  125. origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
  126. transposed_col_ref(const transposed_col_ref<CPT> &cr) :
  127. begin_(cr.begin_),end_(cr.end_), origin(cr.origin),nr(cr.nr),nc(cr.nc) {}
  128. reference operator()(size_type i, size_type j) const
  129. { return linalg_traits<M>::access(begin_+i, j); }
  130. };
  131. template <typename PT> struct linalg_traits<transposed_col_ref<PT> > {
  132. typedef transposed_col_ref<PT> this_type;
  133. typedef typename std::iterator_traits<PT>::value_type M;
  134. typedef typename linalg_traits<M>::origin_type origin_type;
  135. typedef typename select_ref<const origin_type *, origin_type *,
  136. PT>::ref_type porigin_type;
  137. typedef typename which_reference<PT>::is_reference is_reference;
  138. typedef abstract_matrix linalg_type;
  139. typedef typename linalg_traits<M>::value_type value_type;
  140. typedef typename select_ref<value_type,
  141. typename linalg_traits<M>::reference, PT>::ref_type reference;
  142. typedef typename linalg_traits<M>::storage_type storage_type;
  143. typedef abstract_null_type sub_col_type;
  144. typedef abstract_null_type const_sub_col_type;
  145. typedef abstract_null_type col_iterator;
  146. typedef abstract_null_type const_col_iterator;
  147. typedef typename linalg_traits<M>::const_sub_col_type const_sub_row_type;
  148. typedef typename select_ref<abstract_null_type, typename
  149. linalg_traits<M>::sub_col_type, PT>::ref_type sub_row_type;
  150. typedef typename linalg_traits<M>::const_col_iterator const_row_iterator;
  151. typedef typename select_ref<abstract_null_type, typename
  152. linalg_traits<M>::col_iterator, PT>::ref_type row_iterator;
  153. typedef row_major sub_orientation;
  154. typedef typename linalg_traits<M>::index_sorted index_sorted;
  155. static size_type nrows(const this_type &v)
  156. { return v.nr; }
  157. static size_type ncols(const this_type &v)
  158. { return v.nc; }
  159. static const_sub_row_type row(const const_row_iterator &it)
  160. { return linalg_traits<M>::col(it); }
  161. static sub_row_type row(const row_iterator &it)
  162. { return linalg_traits<M>::col(it); }
  163. static row_iterator row_begin(this_type &m) { return m.begin_; }
  164. static row_iterator row_end(this_type &m) { return m.end_; }
  165. static const_row_iterator row_begin(const this_type &m)
  166. { return m.begin_; }
  167. static const_row_iterator row_end(const this_type &m) { return m.end_; }
  168. static origin_type* origin(this_type &v) { return v.origin; }
  169. static const origin_type* origin(const this_type &v) { return v.origin; }
  170. static void do_clear(this_type &m);
  171. static value_type access(const const_row_iterator &itrow, size_type i)
  172. { return linalg_traits<M>::access(itrow, i); }
  173. static reference access(const row_iterator &itrow, size_type i)
  174. { return linalg_traits<M>::access(itrow, i); }
  175. };
  176. template <typename PT>
  177. void linalg_traits<transposed_col_ref<PT> >::do_clear(this_type &v) {
  178. row_iterator it = mat_row_begin(v), ite = mat_row_end(v);
  179. for (; it != ite; ++it) clear(row(it));
  180. }
  181. template<typename PT> std::ostream &operator <<
  182. (std::ostream &o, const transposed_col_ref<PT>& m)
  183. { gmm::write(o,m); return o; }
  184. template <typename TYPE, typename PT> struct transposed_return_ {
  185. typedef abstract_null_type return_type;
  186. };
  187. template <typename PT> struct transposed_return_<row_major, PT> {
  188. typedef typename std::iterator_traits<PT>::value_type L;
  189. typedef typename select_return<transposed_row_ref<const L *>,
  190. transposed_row_ref< L *>, PT>::return_type return_type;
  191. };
  192. template <typename PT> struct transposed_return_<col_major, PT> {
  193. typedef typename std::iterator_traits<PT>::value_type L;
  194. typedef typename select_return<transposed_col_ref<const L *>,
  195. transposed_col_ref< L *>, PT>::return_type return_type;
  196. };
  197. template <typename PT> struct transposed_return {
  198. typedef typename std::iterator_traits<PT>::value_type L;
  199. typedef typename transposed_return_<typename principal_orientation_type<
  200. typename linalg_traits<L>::sub_orientation>::potype,
  201. PT>::return_type return_type;
  202. };
  203. template <typename L> inline
  204. typename transposed_return<const L *>::return_type transposed(const L &l) {
  205. return typename transposed_return<const L *>::return_type
  206. (linalg_cast(const_cast<L &>(l)));
  207. }
  208. template <typename L> inline
  209. typename transposed_return<L *>::return_type transposed(L &l)
  210. { return typename transposed_return<L *>::return_type(linalg_cast(l)); }
  211. }
  212. #endif // GMM_TRANSPOSED_H__