OpenWalnut  1.4.0
WTensorMeta.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WTENSORMETA_H
26 #define WTENSORMETA_H
27 
28 #include <vector> // for size_t
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/mpl/bool.hpp>
32 #endif
33 
34 // forward declaration
35 template< typename Data_T, std::size_t k, std::size_t n, std::size_t N, std::size_t m >
37 
38 /**
39  * Multiplies gradient components and divides by multiplicities.
40  *
41  * This essentailly calls WRecursiveTensorEvaluation< ... >s evaluate function N times.
42  *
43  * If IterEnd equals boost::mpl::bool_< false >, iteration is aborted.
44  */
45 template< typename IterEnd, typename Data_T, std::size_t k, std::size_t i, std::size_t N >
47 {
48  /**
49  * Multiply gradient components and divide by multiplicities.
50  *
51  * \param tens A pointer to the tensor components.
52  * \param grad The gradient to evaluate the function at.
53  * \param w The result up to now.
54  *
55  * \return The result.
56  */
57  static inline Data_T evaluate( Data_T const*& tens, Data_T const* grad, Data_T w )
58  {
59  return WRecursiveTensorEvaluation< Data_T, k - 1, i, N, 1 >::evaluate( tens, grad, w * grad[ i ] )
61  }
62 };
63 
64 /**
65  * Specialization for boost::mpl::bool_< false >, aborts iteration.
66  */
67 template< typename Data_T, std::size_t k, std::size_t i, std::size_t N >
68 struct WRecursiveTensorEvaluationLoop< boost::mpl::bool_< false >, Data_T, k, i, N >
69 {
70  /**
71  * Does nothing.
72  *
73  * \return 0
74  */
75  static inline Data_T evaluate( Data_T const*&, Data_T const*, Data_T )
76  {
77  return 0.0;
78  }
79 };
80 
81 /**
82  * Recursive evaluation of a spherical function for a gradient.
83  */
84 template< typename Data_T, std::size_t k, std::size_t n, std::size_t N, std::size_t m >
86 {
87  /**
88  * Multiply gradient components and divide by multiplicities.
89  *
90  * \param tens A pointer to the tensor components.
91  * \param grad The gradient to evaluate the function at.
92  * \param w The result up to now.
93  *
94  * \return The result.
95  */
96  static inline Data_T evaluate( Data_T const*& tens, Data_T const* grad, Data_T w )
97  {
98  Data_T ret = WRecursiveTensorEvaluation< Data_T, k - 1, n, N, m + 1 >::evaluate( tens, grad, w * grad[ n ] / ( m + 1 ) );
99  return ret + WRecursiveTensorEvaluationLoop< boost::mpl::bool_< ( n + 1 < N ) >, Data_T, k, n + 1, N >::evaluate( tens, grad, w );
100  }
101 };
102 
103 /**
104  * Recursive evaluation of a spherical function for a gradient.
105  */
106 template< typename Data_T, std::size_t n, std::size_t N, std::size_t m >
107 struct WRecursiveTensorEvaluation< Data_T, 0, n, N, m >
108 {
109  /**
110  * Multiply the accumulated weight by the tensor component.
111  *
112  * \param tens A pointer to the tensor components.
113  * \param w The result up to now.
114  *
115  * \return The result.
116  */
117  static inline Data_T evaluate( Data_T const*& tens, Data_T const*, Data_T w )
118  {
119  return w * *( tens++ );
120  }
121 };
122 
123 #endif // WTENSORMETA_H
static Data_T evaluate(Data_T const *&tens, Data_T const *grad, Data_T w)
Multiply gradient components and divide by multiplicities.
Definition: WTensorMeta.h:57
static Data_T evaluate(Data_T const *&tens, Data_T const *, Data_T w)
Multiply the accumulated weight by the tensor component.
Definition: WTensorMeta.h:117
static Data_T evaluate(Data_T const *&, Data_T const *, Data_T)
Does nothing.
Definition: WTensorMeta.h:75
Multiplies gradient components and divides by multiplicities.
Definition: WTensorMeta.h:46
static Data_T evaluate(Data_T const *&tens, Data_T const *grad, Data_T w)
Multiply gradient components and divide by multiplicities.
Definition: WTensorMeta.h:96
Recursive evaluation of a spherical function for a gradient.
Definition: WTensorMeta.h:36