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 
46  struct Lower
47  {
48 
57  template <typename M>
58  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
59  {
60  if (i >= j)
61  return m.getData()(i, j);
62 
63  return m.zero;
64  }
65  };
66 
68  struct UnitLower
69  {
70 
79  template <typename M>
80  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
81  {
82  if (i == j)
83  return m.one;
84 
85  if (i >= j)
86  return m.getData()(i, j);
87 
88  return m.zero;
89  }
90  };
91 
93  struct Upper
94  {
95 
104  template <typename M>
105  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
106  {
107  if (i <= j)
108  return m.getData()(i, j);
109 
110  return m.zero;
111  }
112  };
113 
115  struct UnitUpper
116  {
117 
126  template <typename M>
127  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
128  {
129  if (i == j)
130  return m.one;
131 
132  if (i <= j)
133  return m.getData()(i, j);
134 
135  return m.zero;
136  }
137  };
138 
144  template <typename M, typename Tri>
145  class TriangularAdapter : public MatrixExpression<TriangularAdapter<M, Tri> >
146  {
147 
149 
150  friend struct Lower;
151  friend struct UnitLower;
152  friend struct Upper;
153  friend struct UnitUpper;
154 
155  public:
157  typedef M MatrixType;
159  typedef Tri TriangularType;
161  typedef typename M::SizeType SizeType;
163  typedef typename M::DifferenceType DifferenceType;
165  typedef typename M::ValueType ValueType;
167  typedef typename M::ConstReference ConstReference;
169  typedef typename std::conditional<std::is_const<M>::value,
170  typename M::ConstReference,
171  typename M::Reference>::type Reference;
173  typedef typename std::conditional<std::is_const<M>::value,
174  typename M::ConstClosureType,
175  typename M::ClosureType>::type MatrixClosureType;
177  typedef const SelfType ConstClosureType;
182 
188  data(m) {}
189 
198  {
199  return TriangularType::template get<SelfType>(*this, i, j);
200  }
201 
207  {
208  return data.getSize1();
209  }
210 
216  {
217  return data.getSize2();
218  }
219 
225  {
226  return data;
227  }
228 
233  const MatrixClosureType& getData() const
234  {
235  return data;
236  }
237 
242  bool isEmpty() const
243  {
244  return (data.getSize1() == 0 || data.getSize2() == 0);
245  }
246 
247  private:
248  MatrixClosureType data;
249  static const ValueType zero;
250  static const ValueType one;
251  };
252 
253  template <typename M, typename Tri>
254  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::zero = TriangularAdapter<M, Tri>::ValueType();
255 
256  template <typename M, typename Tri>
257  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::one = TriangularAdapter<M, Tri>::ValueType(1);
258 
264  template <typename M, typename Tri>
266  {};
267 
273  template <typename M, typename Tri>
275  {};
276 
282  template <typename M, typename Tri>
284  {};
285 
291  template <typename M, typename Tri>
293  {};
294 
302  template <typename Tri, typename E>
305  {
306  return TriangularAdapter<E, Tri>(e());
307  }
308 
316  template <typename Tri, typename E>
317  TriangularAdapter<const E, Tri>
319  {
321  }
322  } // namespace Math
323 } // namespace CDPL
324 
325 #endif // CDPL_MATH_MATRIXADAPTER_HPP
Definition of basic expression types.
Definition of type traits.
CRTP base class for all matrix expression types.
Definition: Expression.hpp:104
Matrix expression that exposes only the triangular part of an underlying matrix M selected by the pol...
Definition: MatrixAdapter.hpp:146
Range< SizeType > RangeType
The Math::Range type used to address sub-ranges.
Definition: MatrixAdapter.hpp:181
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: MatrixAdapter.hpp:177
M MatrixType
The wrapped matrix type.
Definition: MatrixAdapter.hpp:157
M::DifferenceType DifferenceType
The signed difference type used by the underlying matrix.
Definition: MatrixAdapter.hpp:163
M::ValueType ValueType
The element value type of the underlying matrix.
Definition: MatrixAdapter.hpp:165
SizeType getSize2() const
Returns the number of columns of the wrapped matrix.
Definition: MatrixAdapter.hpp:215
MatrixClosureType & getData()
Returns a reference to the wrapped matrix (via its stored closure).
Definition: MatrixAdapter.hpp:224
M::SizeType SizeType
The size type used by the underlying matrix.
Definition: MatrixAdapter.hpp:161
bool isEmpty() const
Tells whether the wrapped matrix is empty (zero rows or zero columns).
Definition: MatrixAdapter.hpp:242
M::ConstReference ConstReference
Constant reference type to an element of the underlying matrix.
Definition: MatrixAdapter.hpp:167
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: MatrixAdapter.hpp:179
TriangularAdapter(MatrixType &m)
Constructs the adapter wrapping m.
Definition: MatrixAdapter.hpp:187
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:197
Tri TriangularType
The triangular-view selection policy.
Definition: MatrixAdapter.hpp:159
const MatrixClosureType & getData() const
Returns a const reference to the wrapped matrix (via its stored closure).
Definition: MatrixAdapter.hpp:233
std::conditional< std::is_const< M >::value, typename M::ConstReference, typename M::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped matrix is const).
Definition: MatrixAdapter.hpp:171
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:175
SizeType getSize1() const
Returns the number of rows of the wrapped matrix.
Definition: MatrixAdapter.hpp:206
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:304
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:47
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:58
Selects a concrete temporary matrix type compatible with the matrix expression M.
Definition: TypeTraits.hpp:313
Tag selecting the unit-lower-triangular view (zero above the diagonal, one on the diagonal) for Math:...
Definition: MatrixAdapter.hpp:69
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:80
Tag selecting the unit-upper-triangular view (zero below the diagonal, one on the diagonal) for Math:...
Definition: MatrixAdapter.hpp:116
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:127
Tag selecting the upper-triangular view (entries strictly below the diagonal read as zero) for Math::...
Definition: MatrixAdapter.hpp:94
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:105
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:301