Chemical Data Processing Library C++ API - Version 1.1.1
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 
45  struct Lower
46  {
47 
48  template <typename M>
49  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
50  {
51  if (i >= j)
52  return m.getData()(i, j);
53 
54  return m.zero;
55  }
56  };
57 
58  struct UnitLower
59  {
60 
61  template <typename M>
62  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
63  {
64  if (i == j)
65  return m.one;
66 
67  if (i >= j)
68  return m.getData()(i, j);
69 
70  return m.zero;
71  }
72  };
73 
74  struct Upper
75  {
76 
77  template <typename M>
78  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
79  {
80  if (i <= j)
81  return m.getData()(i, j);
82 
83  return m.zero;
84  }
85  };
86 
87  struct UnitUpper
88  {
89 
90  template <typename M>
91  static typename M::ConstReference get(const M& m, typename M::SizeType i, typename M::SizeType j)
92  {
93  if (i == j)
94  return m.one;
95 
96  if (i <= j)
97  return m.getData()(i, j);
98 
99  return m.zero;
100  }
101  };
102 
103  template <typename M, typename Tri>
104  class TriangularAdapter : public MatrixExpression<TriangularAdapter<M, Tri> >
105  {
106 
108 
109  friend struct Lower;
110  friend struct UnitLower;
111  friend struct Upper;
112  friend struct UnitUpper;
113 
114  public:
115  typedef M MatrixType;
116  typedef Tri TriangularType;
117  typedef typename M::SizeType SizeType;
118  typedef typename M::DifferenceType DifferenceType;
119  typedef typename M::ValueType ValueType;
120  typedef typename M::ConstReference ConstReference;
121  typedef typename std::conditional<std::is_const<M>::value,
122  typename M::ConstReference,
123  typename M::Reference>::type Reference;
124  typedef typename std::conditional<std::is_const<M>::value,
125  typename M::ConstClosureType,
126  typename M::ClosureType>::type MatrixClosureType;
127  typedef const SelfType ConstClosureType;
130 
132  data(m) {}
133 
135  {
136  return TriangularType::template get<SelfType>(*this, i, j);
137  }
138 
140  {
141  return data.getSize1();
142  }
143 
145  {
146  return data.getSize2();
147  }
148 
150  {
151  return data;
152  }
153 
154  const MatrixClosureType& getData() const
155  {
156  return data;
157  }
158 
159  bool isEmpty() const
160  {
161  return (data.getSize1() == 0 || data.getSize2() == 0);
162  }
163 
164  private:
165  MatrixClosureType data;
166  static const ValueType zero;
167  static const ValueType one;
168  };
169 
170  template <typename M, typename Tri>
171  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::zero = TriangularAdapter<M, Tri>::ValueType();
172 
173  template <typename M, typename Tri>
174  const typename TriangularAdapter<M, Tri>::ValueType TriangularAdapter<M, Tri>::one = TriangularAdapter<M, Tri>::ValueType(1);
175 
176  template <typename M, typename Tri>
178  {};
179 
180  template <typename M, typename Tri>
182  {};
183 
184  template <typename M, typename Tri>
186  {};
187 
188  template <typename M, typename Tri>
190  {};
191 
192  template <typename Tri, typename E>
195  {
196  return TriangularAdapter<E, Tri>(e());
197  }
198 
199  template <typename Tri, typename E>
200  TriangularAdapter<const E, Tri>
202  {
204  }
205  } // namespace Math
206 } // namespace CDPL
207 
208 #endif // CDPL_MATH_MATRIXADAPTER_HPP
CDPL::Math::VectorTemporaryTraits
Definition: TypeTraits.hpp:179
CDPL::Math::UnitLower
Definition: MatrixAdapter.hpp:59
CDPL::Math::TriangularAdapter::ValueType
M::ValueType ValueType
Definition: MatrixAdapter.hpp:119
CDPL::Math::UnitUpper
Definition: MatrixAdapter.hpp:88
CDPL::Math::TriangularAdapter::getData
const MatrixClosureType & getData() const
Definition: MatrixAdapter.hpp:154
CDPL::Math::TriangularAdapter::RangeType
Range< SizeType > RangeType
Definition: MatrixAdapter.hpp:129
CDPL::Math::TriangularAdapter::isEmpty
bool isEmpty() const
Definition: MatrixAdapter.hpp:159
CDPL::Math::TriangularAdapter::getSize1
SizeType getSize1() const
Definition: MatrixAdapter.hpp:139
CDPL::Math::Range< SizeType >
CDPL::Math::Lower::get
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Definition: MatrixAdapter.hpp:49
CDPL::Math::Lower
Definition: MatrixAdapter.hpp:46
CDPL::Math::MatrixExpression
Definition: Expression.hpp:76
CDPL::Math::TriangularAdapter::getSize2
SizeType getSize2() const
Definition: MatrixAdapter.hpp:144
CDPL::Math::TriangularAdapter
Definition: MatrixAdapter.hpp:105
CDPL::Math::UnitLower::get
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Definition: MatrixAdapter.hpp:62
CDPL::Math::TriangularAdapter::getData
MatrixClosureType & getData()
Definition: MatrixAdapter.hpp:149
CDPL::Chem::AtomType::M
const unsigned int M
A generic type that covers any element that is a metal.
Definition: AtomType.hpp:637
TypeTraits.hpp
Definition of type traits.
CDPL::Chem::CIPDescriptor::m
const unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
CDPL::Math::MatrixTemporaryTraits
Definition: TypeTraits.hpp:186
CDPL::Math::TriangularAdapter::SizeType
M::SizeType SizeType
Definition: MatrixAdapter.hpp:117
CDPL::Math::TriangularAdapter::TriangularAdapter
TriangularAdapter(MatrixType &m)
Definition: MatrixAdapter.hpp:131
CDPL::Math::Upper::get
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Definition: MatrixAdapter.hpp:78
CDPL::Math::TriangularAdapter::DifferenceType
M::DifferenceType DifferenceType
Definition: MatrixAdapter.hpp:118
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::UnitUpper::get
static M::ConstReference get(const M &m, typename M::SizeType i, typename M::SizeType j)
Definition: MatrixAdapter.hpp:91
CDPL::Math::TriangularAdapter::TriangularType
Tri TriangularType
Definition: MatrixAdapter.hpp:116
CDPL::Math::TriangularAdapter::MatrixType
M MatrixType
Definition: MatrixAdapter.hpp:115
CDPL::Math::Upper
Definition: MatrixAdapter.hpp:75
CDPL::Math::TriangularAdapter::ClosureType
SelfType ClosureType
Definition: MatrixAdapter.hpp:128
CDPL::Math::TriangularAdapter::MatrixClosureType
std::conditional< std::is_const< M >::value, typename M::ConstClosureType, typename M::ClosureType >::type MatrixClosureType
Definition: MatrixAdapter.hpp:126
CDPL::Math::triang
TriangularAdapter< E, Tri > triang(MatrixExpression< E > &e)
Definition: MatrixAdapter.hpp:194
CDPL::Math::TriangularAdapter::ConstClosureType
const SelfType ConstClosureType
Definition: MatrixAdapter.hpp:127
CDPL::Math::TriangularAdapter::Reference
std::conditional< std::is_const< M >::value, typename M::ConstReference, typename M::Reference >::type Reference
Definition: MatrixAdapter.hpp:123
Expression.hpp
Definition of basic expression types.
CDPL::Math::TriangularAdapter::ConstReference
M::ConstReference ConstReference
Definition: MatrixAdapter.hpp:120
CDPL::Math::TriangularAdapter::operator()
ConstReference operator()(SizeType i, SizeType j) const
Definition: MatrixAdapter.hpp:134