Chemical Data Processing Library C++ API - Version 1.2.0
GridExpression.hpp
Go to the documentation of this file.
1 /*
2  * GridExpression.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_GRIDEXPRESSION_HPP
28 #define CDPL_MATH_GRIDEXPRESSION_HPP
29 
30 #include <type_traits>
31 
32 #include "CDPL/Math/Check.hpp"
33 #include "CDPL/Math/Expression.hpp"
34 #include "CDPL/Math/CommonType.hpp"
35 #include "CDPL/Math/Functional.hpp"
36 #include "CDPL/Math/TypeTraits.hpp"
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Math
44  {
45 
46  template <typename C>
47  class GridContainer;
48 
49  template <typename E, typename F>
50  class GridUnary : public GridExpression<GridUnary<E, F> >
51  {
52 
53  typedef GridUnary<E, F> SelfType;
54  typedef F FunctorType;
55  typedef E ExpressionType;
56  typedef typename E::ConstClosureType ExpressionClosureType;
57 
58  public:
59  typedef typename F::ResultType ValueType;
60  typedef const ValueType ConstReference;
61  typedef const ValueType Reference;
62  typedef const SelfType ConstClosureType;
64  typedef typename E::SizeType SizeType;
65  typedef typename E::DifferenceType DifferenceType;
66 
68  expr(e) {}
69 
71  {
72  return expr.getSize1();
73  }
74 
76  {
77  return expr.getSize2();
78  }
79 
81  {
82  return expr.getSize3();
83  }
84 
86  {
87  return FunctorType::apply(expr(i, j, k));
88  }
89 
90  private:
91  ExpressionClosureType expr;
92  };
93 
94  template <typename E, typename F>
96  {
97 
100  };
101 
102  template <typename E1, typename E2, typename F>
103  class GridBinary1 : public GridExpression<GridBinary1<E1, E2, F> >
104  {
105 
107  typedef F FunctorType;
108  typedef E1 Expression1Type;
109  typedef E2 Expression2Type;
110  typedef typename E1::ConstClosureType Expression1ClosureType;
111  typedef typename E2::ConstClosureType Expression2ClosureType;
112 
113  public:
114  typedef typename F::ResultType ValueType;
115  typedef const ValueType ConstReference;
116  typedef const ValueType Reference;
117  typedef const SelfType ConstClosureType;
121 
122  GridBinary1(const Expression1Type& e1, const Expression2Type& e2):
123  expr1(e1), expr2(e2) {}
124 
126  {
127  return CDPL_MATH_CHECK_SIZE_EQUALITY(SizeType(expr1.getSize1()), SizeType(expr2.getSize1()), Base::SizeError);
128  }
129 
131  {
132  return CDPL_MATH_CHECK_SIZE_EQUALITY(SizeType(expr1.getSize2()), SizeType(expr2.getSize2()), Base::SizeError);
133  }
134 
136  {
137  return CDPL_MATH_CHECK_SIZE_EQUALITY(SizeType(expr1.getSize3()), SizeType(expr2.getSize3()), Base::SizeError);
138  }
139 
141  {
142  return FunctorType::apply(expr1(i, j, k), expr2(i, j, k));
143  }
144 
145  private:
146  Expression1ClosureType expr1;
147  Expression2ClosureType expr2;
148  };
149 
150  template <typename E1, typename E2, typename F>
152  {
153 
156  };
157 
158  template <typename E1, typename E2, typename F>
159  class Scalar1GridBinary : public GridExpression<Scalar1GridBinary<E1, E2, F> >
160  {
161 
163  typedef F FunctorType;
164  typedef E1 Expression1Type;
165  typedef E2 Expression2Type;
166  typedef const E1 Expression1ClosureType;
167  typedef typename E2::ConstClosureType Expression2ClosureType;
168 
169  public:
170  typedef typename F::ResultType ValueType;
171  typedef const ValueType ConstReference;
172  typedef const ValueType Reference;
173  typedef const SelfType ConstClosureType;
175  typedef typename E2::SizeType SizeType;
176  typedef typename E2::DifferenceType DifferenceType;
177 
178  Scalar1GridBinary(const Expression1Type& e1, const Expression2Type& e2):
179  expr1(e1), expr2(e2) {}
180 
182  {
183  return expr2.getSize1();
184  }
185 
187  {
188  return expr2.getSize2();
189  }
190 
192  {
193  return expr2.getSize3();
194  }
195 
197  {
198  return FunctorType::apply(expr1, expr2(i, j, k));
199  }
200 
201  private:
202  Expression1ClosureType expr1;
203  Expression2ClosureType expr2;
204  };
205 
206  template <typename E1, typename E2, typename F>
208  {
209 
212  };
213 
214  template <typename E1, typename E2, typename F>
215  class Scalar2GridBinary : public GridExpression<Scalar2GridBinary<E1, E2, F> >
216  {
217 
219  typedef F FunctorType;
220  typedef E1 Expression1Type;
221  typedef E2 Expression2Type;
222  typedef typename E1::ConstClosureType Expression1ClosureType;
223  typedef const E2 Expression2ClosureType;
224 
225  public:
226  typedef typename F::ResultType ValueType;
227  typedef const ValueType ConstReference;
228  typedef const ValueType Reference;
229  typedef const SelfType ConstClosureType;
231  typedef typename E1::SizeType SizeType;
232  typedef typename E1::DifferenceType DifferenceType;
233 
234  Scalar2GridBinary(const Expression1Type& e1, const Expression2Type& e2):
235  expr1(e1), expr2(e2) {}
236 
238  {
239  return expr1.getSize1();
240  }
241 
243  {
244  return expr1.getSize2();
245  }
246 
248  {
249  return expr1.getSize3();
250  }
251 
253  {
254  return FunctorType::apply(expr1(i, j, k), expr2);
255  }
256 
257  private:
258  Expression1ClosureType expr1;
259  Expression2ClosureType expr2;
260  };
261 
262  template <typename E1, typename E2, typename F>
264  {
265 
268  };
269 
270  template <typename E>
273  {
274  typedef typename GridUnaryTraits<E, ScalarNegation<typename E::ValueType> >::ExpressionType ExpressionType;
275 
276  return ExpressionType(e());
277  }
278 
279  template <typename E>
280 
281  const E&
283  {
284  return e();
285  }
286 
287  template <typename E1, typename E2>
288  typename GridBinary1Traits<E1, E2, ScalarAddition<typename E1::ValueType, typename E2::ValueType> >::ResultType
290  {
291  typedef typename GridBinary1Traits<E1, E2,
293 
294  return ExpressionType(e1(), e2());
295  }
296 
297  template <typename E1, typename E2>
298  typename GridBinary1Traits<E1, E2, ScalarSubtraction<typename E1::ValueType, typename E2::ValueType> >::ResultType
300  {
301  typedef typename GridBinary1Traits<E1, E2,
303 
304  return ExpressionType(e1(), e2());
305  }
306 
307  template <typename E, typename T>
308  typename std::enable_if<IsScalar<T>::value, typename Scalar2GridBinaryTraits<E, T, ScalarMultiplication<typename E::ValueType, T> >::ResultType>::type
309  operator*(const GridExpression<E>& e, const T& t)
310  {
311  typedef typename Scalar2GridBinaryTraits<E, T,
312  ScalarMultiplication<typename E::ValueType, T> >::ExpressionType ExpressionType;
313 
314  return ExpressionType(e(), t);
315  }
316 
317  template <typename T, typename E>
318  typename std::enable_if<IsScalar<T>::value, typename Scalar1GridBinaryTraits<T, E, ScalarMultiplication<T, typename E::ValueType> >::ResultType>::type
319  operator*(const T& t, const GridExpression<E>& e)
320  {
321  typedef typename Scalar1GridBinaryTraits<T, E,
322  ScalarMultiplication<T, typename E::ValueType> >::ExpressionType ExpressionType;
323 
324  return ExpressionType(t, e());
325  }
326 
327  template <typename E, typename T>
328  typename std::enable_if<IsScalar<T>::value, typename Scalar2GridBinaryTraits<E, T, ScalarDivision<typename E::ValueType, T> >::ResultType>::type
329  operator/(const GridExpression<E>& e, const T& t)
330  {
331  typedef typename Scalar2GridBinaryTraits<E, T,
332  ScalarDivision<typename E::ValueType, T> >::ExpressionType ExpressionType;
333 
334  return ExpressionType(e(), t);
335  }
336 
337  template <typename E1, typename E2>
340  {
341  return GridEquality<E1, E2>::apply(e1, e2);
342  }
343 
344  template <typename E1, typename E2>
347  {
348  return !GridEquality<E1, E2>::apply(e1, e2);
349  }
350 
351  template <typename E1, typename E2, typename T>
352  typename std::enable_if<std::is_arithmetic<T>::value, typename GridToleranceEquality<E1, E2, T>::ResultType>::type
353  equals(const GridExpression<E1>& e1, const GridExpression<E2>& e2, const T& eps)
354  {
355  return GridToleranceEquality<E1, E2, T>::apply(e1, e2, eps);
356  }
357 
358  template <typename E>
359  typename GridUnaryTraits<E, ScalarConjugation<typename E::ValueType> >::ResultType
361  {
362  typedef typename GridUnaryTraits<E, ScalarConjugation<typename E::ValueType> >::ExpressionType ExpressionType;
363 
364  return ExpressionType(e());
365  }
366 
367  template <typename E>
368  typename GridUnaryTraits<E, ScalarConjugation<typename E::ValueType> >::ResultType
370  {
371  typedef typename GridUnaryTraits<E, ScalarConjugation<typename E::ValueType> >::ExpressionType ExpressionType;
372 
373  return ExpressionType(e());
374  }
375 
376  template <typename E>
377  typename GridUnaryTraits<E, ScalarReal<typename E::ValueType> >::ResultType
379  {
380  typedef typename GridUnaryTraits<E, ScalarReal<typename E::ValueType> >::ExpressionType ExpressionType;
381 
382  return ExpressionType(e());
383  }
384 
385  template <typename E>
386  typename GridUnaryTraits<E, ScalarImaginary<typename E::ValueType> >::ResultType
388  {
389  typedef typename GridUnaryTraits<E, ScalarImaginary<typename E::ValueType> >::ExpressionType ExpressionType;
390 
391  return ExpressionType(e());
392  }
393 
394  template <typename E1, typename E2>
395  typename GridBinary1Traits<E1, E2, ScalarDivision<typename E1::ValueType, typename E2::ValueType> >::ResultType
397  {
398  typedef typename GridBinary1Traits<E1, E2,
400 
401  return ExpressionType(e1(), e2());
402  }
403 
404  template <typename E1, typename E2>
405  typename GridBinary1Traits<E1, E2, ScalarMultiplication<typename E1::ValueType, typename E2::ValueType> >::ResultType
407  {
408  typedef typename GridBinary1Traits<E1, E2,
410 
411  return ExpressionType(e1(), e2());
412  }
413 
414  template <typename E>
417  {
418  return GridElementSum<E>::apply(e);
419  }
420  } // namespace Math
421 } // namespace CDPL
422 
423 #endif // CDPL_MATH_GRIDEXPRESSION_HPP
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK_SIZE_EQUALITY(size1, size2, e)
Definition: Check.hpp:62
Common type deduction.
Definition of basic expression types.
Definition of various functors.
Definition of type traits.
Thrown to indicate that the size of a (multidimensional) array is not correct.
Definition: Base/Exceptions.hpp:133
E ExpressionType
Definition: Expression.hpp:42
Definition: GridExpression.hpp:104
F::ResultType ValueType
Definition: GridExpression.hpp:114
CommonType< typename E1::SizeType, typename E2::SizeType >::Type SizeType
Definition: GridExpression.hpp:119
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Definition: GridExpression.hpp:140
GridBinary1(const Expression1Type &e1, const Expression2Type &e2)
Definition: GridExpression.hpp:122
SizeType getSize1() const
Definition: GridExpression.hpp:125
const ValueType ConstReference
Definition: GridExpression.hpp:115
SelfType ClosureType
Definition: GridExpression.hpp:118
CommonType< typename E1::DifferenceType, typename E2::DifferenceType >::Type DifferenceType
Definition: GridExpression.hpp:120
const SelfType ConstClosureType
Definition: GridExpression.hpp:117
SizeType getSize3() const
Definition: GridExpression.hpp:135
const ValueType Reference
Definition: GridExpression.hpp:116
SizeType getSize2() const
Definition: GridExpression.hpp:130
Definition: Expression.hpp:120
Definition: GridExpression.hpp:51
E::SizeType SizeType
Definition: GridExpression.hpp:64
SizeType getSize2() const
Definition: GridExpression.hpp:75
const SelfType ConstClosureType
Definition: GridExpression.hpp:62
SizeType getSize1() const
Definition: GridExpression.hpp:70
SizeType getSize3() const
Definition: GridExpression.hpp:80
SelfType ClosureType
Definition: GridExpression.hpp:63
E::DifferenceType DifferenceType
Definition: GridExpression.hpp:65
GridUnary(const ExpressionType &e)
Definition: GridExpression.hpp:67
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Definition: GridExpression.hpp:85
const ValueType ConstReference
Definition: GridExpression.hpp:60
const ValueType Reference
Definition: GridExpression.hpp:61
F::ResultType ValueType
Definition: GridExpression.hpp:59
Definition: GridExpression.hpp:160
SizeType getSize2() const
Definition: GridExpression.hpp:186
const SelfType ConstClosureType
Definition: GridExpression.hpp:173
F::ResultType ValueType
Definition: GridExpression.hpp:170
SizeType getSize3() const
Definition: GridExpression.hpp:191
Scalar1GridBinary(const Expression1Type &e1, const Expression2Type &e2)
Definition: GridExpression.hpp:178
E2::DifferenceType DifferenceType
Definition: GridExpression.hpp:176
const ValueType ConstReference
Definition: GridExpression.hpp:171
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Definition: GridExpression.hpp:196
E2::SizeType SizeType
Definition: GridExpression.hpp:175
SizeType getSize1() const
Definition: GridExpression.hpp:181
SelfType ClosureType
Definition: GridExpression.hpp:174
const ValueType Reference
Definition: GridExpression.hpp:172
Definition: GridExpression.hpp:216
E1::SizeType SizeType
Definition: GridExpression.hpp:231
const ValueType Reference
Definition: GridExpression.hpp:228
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Definition: GridExpression.hpp:252
SizeType getSize2() const
Definition: GridExpression.hpp:242
const SelfType ConstClosureType
Definition: GridExpression.hpp:229
SizeType getSize3() const
Definition: GridExpression.hpp:247
SizeType getSize1() const
Definition: GridExpression.hpp:237
Scalar2GridBinary(const Expression1Type &e1, const Expression2Type &e2)
Definition: GridExpression.hpp:234
SelfType ClosureType
Definition: GridExpression.hpp:230
F::ResultType ValueType
Definition: GridExpression.hpp:226
const ValueType ConstReference
Definition: GridExpression.hpp:227
E1::DifferenceType DifferenceType
Definition: GridExpression.hpp:232
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int F
Specifies Fluorine.
Definition: AtomType.hpp:107
constexpr unsigned int E
Specifies that the stereocenter has E configuration.
Definition: CIPDescriptor.hpp:96
GridBinary1Traits< E1, E2, ScalarMultiplication< typename E1::ValueType, typename E2::ValueType > >::ResultType elemProd(const GridExpression< E1 > &e1, const GridExpression< E2 > &e2)
Definition: GridExpression.hpp:406
GridUnaryTraits< E, ScalarConjugation< typename E::ValueType > >::ResultType herm(const GridExpression< E > &e)
Definition: GridExpression.hpp:369
GridUnaryTraits< E, ScalarConjugation< typename E::ValueType > >::ResultType conj(const GridExpression< E > &e)
Definition: GridExpression.hpp:360
std::enable_if< std::is_arithmetic< T >::value, typename GridToleranceEquality< E1, E2, T >::ResultType >::type equals(const GridExpression< E1 > &e1, const GridExpression< E2 > &e2, const T &eps)
Definition: GridExpression.hpp:353
std::enable_if< IsScalar< T >::value, typename Scalar2GridBinaryTraits< E, T, ScalarDivision< typename E::ValueType, T > >::ResultType >::type operator/(const GridExpression< E > &e, const T &t)
Definition: GridExpression.hpp:329
GridUnaryTraits< E, ScalarReal< typename E::ValueType > >::ResultType real(const GridExpression< E > &e)
Definition: GridExpression.hpp:378
GridEquality< E1, E2 >::ResultType operator!=(const GridExpression< E1 > &e1, const GridExpression< E2 > &e2)
Definition: GridExpression.hpp:346
GridEquality< E1, E2 >::ResultType operator==(const GridExpression< E1 > &e1, const GridExpression< E2 > &e2)
Definition: GridExpression.hpp:339
std::enable_if< IsScalar< T >::value, typename Scalar2GridBinaryTraits< E, T, ScalarMultiplication< typename E::ValueType, T > >::ResultType >::type operator*(const GridExpression< E > &e, const T &t)
Definition: GridExpression.hpp:309
GridBinary1Traits< E1, E2, ScalarDivision< typename E1::ValueType, typename E2::ValueType > >::ResultType elemDiv(const GridExpression< E1 > &e1, const GridExpression< E2 > &e2)
Definition: GridExpression.hpp:396
GridUnaryTraits< E, ScalarImaginary< typename E::ValueType > >::ResultType imag(const GridExpression< E > &e)
Definition: GridExpression.hpp:387
GridUnaryTraits< E, ScalarNegation< typename E::ValueType > >::ResultType operator-(const GridExpression< E > &e)
Definition: GridExpression.hpp:272
GridElementSum< E >::ResultType sum(const GridExpression< E > &e)
Definition: GridExpression.hpp:416
const E & operator+(const GridExpression< E > &e)
Definition: GridExpression.hpp:282
The namespace of the Chemical Data Processing Library.
std::common_type< T1, T2 >::type Type
Definition: CommonType.hpp:43
Definition: GridExpression.hpp:152
GridBinary1< E1, E2, F > ExpressionType
Definition: GridExpression.hpp:154
ExpressionType ResultType
Definition: GridExpression.hpp:155
GridScalarUnaryFunctor< G >::ResultType ResultType
Definition: Functional.hpp:1579
static ResultType apply(const GridExpression< G > &e)
Definition: Functional.hpp:1581
static ResultType apply(const GridExpression< G1 > &e1, const GridExpression< G2 > &e2)
Definition: Functional.hpp:1503
GridBooleanBinaryFunctor< G1, G2 >::ResultType ResultType
Definition: Functional.hpp:1501
Scalar3GridBooleanTernaryFunctor< G1, G2, T >::ResultType ResultType
Definition: Functional.hpp:1540
static ResultType apply(const GridExpression< G1 > &e1, const GridExpression< G2 > &e2, Argument3Type epsilon)
Definition: Functional.hpp:1543
Definition: GridExpression.hpp:96
ExpressionType ResultType
Definition: GridExpression.hpp:99
GridUnary< E, F > ExpressionType
Definition: GridExpression.hpp:98
Definition: GridExpression.hpp:208
ExpressionType ResultType
Definition: GridExpression.hpp:211
Scalar1GridBinary< E1, E2, F > ExpressionType
Definition: GridExpression.hpp:210
Definition: GridExpression.hpp:264
ExpressionType ResultType
Definition: GridExpression.hpp:267
Scalar2GridBinary< E1, E2, F > ExpressionType
Definition: GridExpression.hpp:266
Definition: Functional.hpp:211
Definition: Functional.hpp:253
Definition: Functional.hpp:239
Definition: Functional.hpp:225