Chemical Data Processing Library C++ API - Version 1.4.0
MatrixAdapter.hpp
Go to the documentation of this file.
1 /*
2  * MatrixAdapter.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_MATRIXADAPTER_HPP
28 #define CDPL_MATH_MATRIXADAPTER_HPP
29 
30 #include <type_traits>
31 
32 #include "CDPL/Math/Expression.hpp"
33 #include "CDPL/Math/TypeTraits.hpp"
34 
35 
36 namespace CDPL
37 {
38 
39  namespace Math
40  {
41 
42  template <typename T>
43  class Range;
44 
48  struct Lower
49  {
50 
59  template <typename M>
60  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
61  {
62  if (i >= j)
63  return m.getData()(i, j);
64 
65  return m.zero;
66  }
67  };
68 
72  struct UnitLower
73  {
74 
83  template <typename M>
84  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
85  {
86  if (i == j)
87  return m.one;
88 
89  if (i >= j)
90  return m.getData()(i, j);
91 
92  return m.zero;
93  }
94  };
95 
99  struct Upper
100  {
101 
110  template <typename M>
111  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
112  {
113  if (i <= j)
114  return m.getData()(i, j);
115 
116  return m.zero;
117  }
118  };
119 
123  struct UnitUpper
124  {
125 
134  template <typename M>
135  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
136  {
137  if (i == j)
138  return m.one;
139 
140  if (i <= j)
141  return m.getData()(i, j);
142 
143  return m.zero;
144  }
145  };
146 
152  template <typename M, typename Tri>
153  class TriangularAdapter : public MatrixExpression<TriangularAdapter<M, Tri> >
154  {
155 
157 
158  friend struct Lower;
159  friend struct UnitLower;
160  friend struct Upper;
161  friend struct UnitUpper;
162 
163  public:
167  typedef M MatrixType;
168 
172  typedef Tri TriangularType;
173 
177  typedef typename M::SizeType SizeType;
178 
182  typedef typename M::DifferenceType DifferenceType;
183 
187  typedef typename M::ValueType ValueType;
188 
192  typedef typename M::ConstReference ConstReference;
193 
197  typedef typename std::conditional<std::is_const<M>::value,
198  typename M::ConstReference,
199  typename M::Reference>::type Reference;
200 
204  typedef typename std::conditional<std::is_const<M>::value,
205  typename M::ConstClosureType,
206  typename M::ClosureType>::type MatrixClosureType;
207 
211  typedef const SelfType ConstClosureType;
212 
217 
222 
228  data(m) {}
229 
238  {
239  return TriangularType::template get<SelfType>(*this, i, j);
240  }
241 
247  {
248  return data.getSize1();
249  }
250 
256  {
257  return data.getSize2();
258  }
259 
265  {
266  return data;
267  }
268 
273  const MatrixClosureType& getData() const
274  {
275  return data;
276  }
277 
282  bool isEmpty() const
283  {
284  return (data.getSize1() == 0 || data.getSize2() == 0);
285  }
286 
287  private:
288  MatrixClosureType data;
289  static const ValueType zero;
290  static const ValueType one;
291  };
292 
293  template <typename M, typename Tri>
294  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::zero = TriangularAdapter<M, Tri>::ValueType();
295 
296  template <typename M, typename Tri>
297  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::one = TriangularAdapter<M, Tri>::ValueType(1);
298 
304  template <typename M, typename Tri>
306  {};
307 
313  template <typename M, typename Tri>
315  {};
316 
322  template <typename M, typename Tri>
324  {};
325 
331  template <typename M, typename Tri>
333  {};
334 
342  template <typename Tri, typename E>
345  {
346  return TriangularAdapter<E, Tri>(e());
347  }
348 
356  template <typename Tri, typename E>
357  TriangularAdapter<const E, Tri>
359  {
361  }
362  } // namespace Math
363 } // namespace CDPL
364 
365 #endif // CDPL_MATH_MATRIXADAPTER_HPP
Definition of basic expression types.
Definition of type traits.
CRTP base class of all matrix expression types.
Definition: Expression.hpp:108
Matrix expression that exposes only the triangular part of an underlying matrix M selected by the pol...
Definition: MatrixAdapter.hpp:154
Range< SizeType > RangeType
The Math::Range type used to address sub-ranges.
Definition: MatrixAdapter.hpp:221
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: MatrixAdapter.hpp:211
M MatrixType
The wrapped matrix type.
Definition: MatrixAdapter.hpp:167
M::DifferenceType DifferenceType
The signed difference type used by the underlying matrix.
Definition: MatrixAdapter.hpp:182
M::ValueType ValueType
The element value type of the underlying matrix.
Definition: MatrixAdapter.hpp:187
SizeType getSize2() const
Returns the number of columns of the wrapped matrix.
Definition: MatrixAdapter.hpp:255
MatrixClosureType & getData()
Returns a reference to the wrapped matrix (via its stored closure).
Definition: MatrixAdapter.hpp:264
M::SizeType SizeType
The size type used by the underlying matrix.
Definition: MatrixAdapter.hpp:177
bool isEmpty() const
Tells whether the wrapped matrix is empty (zero rows or zero columns).
Definition: MatrixAdapter.hpp:282
M::ConstReference ConstReference
Constant element reference type of the underlying matrix.
Definition: MatrixAdapter.hpp:192
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: MatrixAdapter.hpp:216
TriangularAdapter(MatrixType &m)
Constructs the adapter wrapping m.
Definition: MatrixAdapter.hpp:227
ConstReference operator()(SizeType i, SizeType j) const
Returns the value of element (i, j) as seen through the triangular policy (off-policy entries return ...
Definition: MatrixAdapter.hpp:237
Tri TriangularType
The triangular-view selection policy.
Definition: MatrixAdapter.hpp:172
const MatrixClosureType & getData() const
Returns a const reference to the wrapped matrix (via its stored closure).
Definition: MatrixAdapter.hpp:273
std::conditional< std::is_const< M >::value, typename M::ConstReference, typename M::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped matrix is const).
Definition: MatrixAdapter.hpp:199
std::conditional< std::is_const< M >::value, typename M::ConstClosureType, typename M::ClosureType >::type MatrixClosureType
Closure type used to store the wrapped matrix internally (mutable or const flavor).
Definition: MatrixAdapter.hpp:206
SizeType getSize1() const
Returns the number of rows of the wrapped matrix.
Definition: MatrixAdapter.hpp:246
constexpr unsigned int M
Generic type that covers any element that is a metal.
Definition: AtomType.hpp:657
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
TriangularAdapter< E, Tri > triang(MatrixExpression< E > &e)
Creates a Math::TriangularAdapter view of the matrix expression e using the triangular policy Tri.
Definition: MatrixAdapter.hpp:344
The namespace of the Chemical Data Processing Library.
Tag selecting the lower-triangular view (entries strictly above the diagonal read as zero) for Math::...
Definition: MatrixAdapter.hpp:49
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Returns element (i, j) of m under the lower-triangular policy (zero above the diagonal).
Definition: MatrixAdapter.hpp:60
Selects a concrete temporary matrix type compatible with the matrix expression M.
Definition: TypeTraits.hpp:337
Tag selecting the unit-lower-triangular view (zero above the diagonal, one on the diagonal) for Math:...
Definition: MatrixAdapter.hpp:73
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Returns element (i, j) of m under the unit-lower-triangular policy (one on the diagonal,...
Definition: MatrixAdapter.hpp:84
Tag selecting the unit-upper-triangular view (zero below the diagonal, one on the diagonal) for Math:...
Definition: MatrixAdapter.hpp:124
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Returns element (i, j) of m under the unit-upper-triangular policy (one on the diagonal,...
Definition: MatrixAdapter.hpp:135
Tag selecting the upper-triangular view (entries strictly below the diagonal read as zero) for Math::...
Definition: MatrixAdapter.hpp:100
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Returns element (i, j) of m under the upper-triangular policy (zero below the diagonal).
Definition: MatrixAdapter.hpp:111
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:323