1 /*!
2  * \file  include/TFEL/Math/matrix.hxx
3  * \brief
4  * \author Thomas Helfer
5  * \brief 10 avr 2009
6  * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights
7  * reserved.
8  * This project is publicly released under either the GNU GPL Licence
9  * or the CECILL-A licence. A copy of thoses licences are delivered
10  * with the sources of TFEL. CEA or EDF may also distribute this
11  * project under specific licensing conditions.
12  */
13 
14 #ifndef LIB_TFEL_MATH_MATRIX_HXX
15 #define LIB_TFEL_MATH_MATRIX_HXX
16 
17 #include "TFEL/Math/vector.hxx"
18 #include "TFEL/Math/Matrix/MatrixConcept.hxx"
19 #include "TFEL/Math/Forward/matrix.hxx"
20 
21 #ifdef NO_RUNTIME_CHECK_BOUNDS
22 #include "TFEL/Math/General/EmptyRunTimeProperties.hxx"
23 #endif /* LIB_TFEL_MATH_MATRIX_HXX */
24 
25 namespace tfel {
26 
27   namespace math {
28 
29     template <typename T>
30     struct MatrixProperties;
31 
32     //! Partial specialisation for matrixs.
33     /*
34      * This is a MatrixConcept requirement.
35      * \see MatrixTraits.
36      */
37     template <typename T>
38     struct TFEL_VISIBILITY_LOCAL MatrixTraits<matrix<T>> {
39       //! the type holded by the tmatrix.
40       typedef typename tfel::math::vector<T>::value_type NumType;
41       //! the type of the index used by the tmatrix.
42       typedef typename tfel::math::vector<T>::size_type IndexType;
43 /*!
44  * RunTime Properties
45  */
46 #ifdef NO_RUNTIME_CHECK_BOUNDS
47       typedef EmptyRunTimeProperties RunTimeProperties;
48 #else  /* NO_RUNTIME_CHECK_BOUNDS */
49       typedef MatrixProperties<T> RunTimeProperties;
50 #endif /* LIB_TFEL_MATH_MATRIX_HXX */
51     };
52 
53     template <typename T>
54     struct TFEL_VISIBILITY_LOCAL MatrixProperties {
55       //! a simple alias
56       typedef typename MatrixTraits<matrix<T>>::IndexType IndexType;
57 
58       MatrixProperties(const IndexType, const IndexType);
59 
60       MatrixProperties(const MatrixProperties&);
61 
62       MatrixProperties& operator=(const MatrixProperties&);
63 
64       bool operator==(const MatrixProperties&) const;
65 
66       bool operator!=(const MatrixProperties&) const;
67 
68      protected:
69       typename MatrixTraits<matrix<T>>::IndexType nb_rows;
70 
71       typename MatrixTraits<matrix<T>>::IndexType nb_cols;
72 
73     };  // end of MatrixProperties
74 
75     template <typename T>
76     struct TFEL_VISIBILITY_LOCAL matrix : protected tfel::math::vector<T>,
77                                           protected MatrixProperties<T>,
78                                           public MatrixConcept<matrix<T>> {
79       //! a simple alias
80       using ConceptTag = typename MatrixConcept<matrix<T>>::ConceptTag;
81 //! a simple alias
82 #ifdef NO_RUNTIME_CHECK_BOUNDS
83       typedef EmptyRunTimeProperties RunTimeProperties;
84 #else  /* NO_RUNTIME_CHECK_BOUNDS */
85       typedef MatrixProperties<T> RunTimeProperties;
86 #endif /* LIB_TFEL_MATH_MATRIX_HXX */
87        /*!
88         * type of the matrix's values.
89         * (this i<s a stl requirement).
90         */
91       using typename tfel::math::vector<T>::value_type;
92       /*!
93        * type of a pointer to the value contained.
94        * (this is a stl requirement).
95        */
96       using typename tfel::math::vector<T>::pointer;
97       /*!
98        * type of a const pointer to the value contained.
99        * (this is a stl requirement).
100        */
101       using typename tfel::math::vector<T>::const_pointer;
102       /*!
103        * type of the matrix's iterator.
104        * (provided for stl compatibility).
105        */
106       using typename tfel::math::vector<T>::iterator;
107       /*!
108        * type of the matrix's const iterator.
109        * (provided for stl compatibility).
110        */
111       using typename tfel::math::vector<T>::const_iterator;
112       /*!
113        * type of the matrix's reverse iterator.
114        * (provided for stl compatibility).
115        */
116       using typename tfel::math::vector<T>::const_reverse_iterator;
117       /*!
118        * type of the matrix's const reverse iterator.
119        * (provided for stl compatibility).
120        */
121       using typename tfel::math::vector<T>::reverse_iterator;
122       /*!
123        * type of a reference to the value contained.
124        * (this is a stl requirement).
125        */
126       using typename tfel::math::vector<T>::reference;
127       /*!
128        * type of a const reference to the value contained.
129        * (this is a stl requirement).
130        */
131       using typename tfel::math::vector<T>::const_reference;
132       /*!
133        * type of the size of the container.
134        * (this is a stl requirement).
135        */
136       typedef typename tfel::math::vector<T>::size_type size_type;
137       /*!
138        * type of the difference between two iterators.
139        * (this is a stl requirement).
140        */
141       using typename tfel::math::vector<T>::difference_type;
142 
143       TFEL_MATH_INLINE2
144       matrix();
145 
146       TFEL_MATH_INLINE2
147       matrix(const size_type, const size_type);
148 
149       TFEL_MATH_INLINE2
150       matrix(const size_type, const size_type, const T&);
151 
152       TFEL_MATH_INLINE2
153       matrix(const matrix&);
154 
155       /*!
156        * resize the matrix
157        * \param[in] n : number of rows
158        * \param[in] m : number of columns
159        * \param[in] v : values of the newly inserted elements
160        */
161       void resize(const size_type, const size_type, const T& = T());
162 
163       /*!
164        * clear the matrix
165        */
166       void clear();
167 
168       TFEL_MATH_INLINE T& operator()(const size_type, const size_type);
169 
170       TFEL_MATH_INLINE const T& operator()(size_type, size_type) const;
171 
172 #ifdef NO_RUNTIME_CHECK_BOUNDS
173       TFEL_MATH_INLINE const RunTimeProperties
174 #else
175       TFEL_MATH_INLINE const RunTimeProperties&
176 #endif /* LIB_TFEL_MATH_MATRIX_HXX */
177       getRunTimeProperties() const;
178 
179       /*
180        * return an iterator to the first element of the matrix
181        * (provided for stl compatibility)
182        * \return iterator, an iterator to the first element
183        */
184       using tfel::math::vector<T>::begin;
185 
186       /*
187        * return an iterator after the last element of the matrix
188        * (provided for stl compatibility)
189        * \return iterator, an iterator after the last element
190        */
191       using tfel::math::vector<T>::end;
192 
193       /*
194        * return an reverse iterator to the last element of the matrix
195        * (provided for stl compatibility)
196        * \return reverse_iterator, a reverse iterator to the last element
197        */
198       using tfel::math::vector<T>::rbegin;
199 
200       /*
201        * return an  reverse iterator before the first element of the matrix
202        * (provided for stl compatibility)
203        * \return reverse_iterator, a reverse iterator before the first element
204        */
205       using tfel::math::vector<T>::rend;
206 
207       /*
208        * Assignement operator.
209        * \param  const matrix&, the matrix to be copied.
210        * \return matrix&, a reference to itself.
211        */
212       matrix& operator=(const matrix&);
213 
214       /*
215        * Assignement operator.
216        * \param  const matrix&, the matrix to be copied.
217        * \return matrix&, a reference to itself.
218        */
219       matrix& operator+=(const matrix&);
220 
221       /*
222        * Assignement operator.
223        * \param  const matrix&, the matrix to be copied.
224        * \return matrix&, a reference to itself.
225        */
226       matrix& operator-=(const matrix&);
227 
228       /*
229        * Assignement operator
230        * \param const expr : a matrix
231        * expression based on matrix
232        * \return matrix&, a reference to itself.
233        */
234       template <typename T2, typename Operation>
235       TFEL_MATH_INLINE2
236           typename std::enable_if<tfel::typetraits::IsAssignableTo<T2, T>::cond,
237                                   matrix<T>&>::type
238           operator=(const Expr<matrix<T2>, Operation>&);
239 
240       /*
241        * Assignement operator
242        * \param const expr : a matrix
243        * expression based on matrix
244        * \return matrix&, a reference to itself.
245        */
246       template <typename T2, typename Operation>
247       TFEL_MATH_INLINE2
248           typename std::enable_if<tfel::typetraits::IsAssignableTo<T2, T>::cond,
249                                   matrix<T>&>::type
250           operator+=(const Expr<matrix<T2>, Operation>&);
251 
252       /*
253        * Assignement operator
254        * \param const expr : a matrix
255        * expression based on matrix
256        * \return matrix&, a reference to itself.
257        */
258       template <typename T2, typename Operation>
259       TFEL_MATH_INLINE2
260           typename std::enable_if<tfel::typetraits::IsAssignableTo<T2, T>::cond,
261                                   matrix<T>&>::type
262           operator-=(const Expr<matrix<T2>, Operation>&);
263 
264       void swap(matrix&);
265 
266       TFEL_MATH_INLINE size_type getNbRows() const;
267 
268       TFEL_MATH_INLINE size_type getNbCols() const;
269     };
270 
271   }  // end of namespace math
272 
273 }  // end of namespace tfel
274 
275 #include "TFEL/Math/Matrix/matrix.ixx"
276 
277 #endif /* LIB_TFEL_MATH_MATRIX_HXX */
278