OpenWalnut  1.5.0dev
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 #include <boost/mpl/bool.hpp>
31 
32 // forward declaration
33 template< typename Data_T, std::size_t k, std::size_t n, std::size_t N, std::size_t m >
35 
36 /**
37  * Multiplies gradient components and divides by multiplicities.
38  *
39  * This essentailly calls WRecursiveTensorEvaluation< ... >s evaluate function N times.
40  *
41  * If IterEnd equals boost::mpl::bool_< false >, iteration is aborted.
42  */
43 template< typename IterEnd, typename Data_T, std::size_t k, std::size_t i, std::size_t N >
45 {
46  /**
47  * Multiply gradient components and divide by multiplicities.
48  *
49  * \param tens A pointer to the tensor components.
50  * \param grad The gradient to evaluate the function at.
51  * \param w The result up to now.
52  *
53  * \return The result.
54  */
55  static inline Data_T evaluate( Data_T const*& tens, Data_T const* grad, Data_T w )
56  {
57  return WRecursiveTensorEvaluation< Data_T, k - 1, i, N, 1 >::evaluate( tens, grad, w * grad[ i ] )
58  + WRecursiveTensorEvaluationLoop< boost::mpl::bool_< ( i + 1 < N ) >, Data_T, k, i + 1, N >::evaluate( tens, grad, w );
59  }
60 };
61 
62 /**
63  * Specialization for boost::mpl::bool_< false >, aborts iteration.
64  */
65 template< typename Data_T, std::size_t k, std::size_t i, std::size_t N >
66 struct WRecursiveTensorEvaluationLoop< boost::mpl::bool_< false >, Data_T, k, i, N >
67 {
68  /**
69  * Does nothing.
70  *
71  * \return 0
72  */
73  static inline Data_T evaluate( Data_T const*&, Data_T const*, Data_T )
74  {
75  return 0.0;
76  }
77 };
78 
79 /**
80  * Recursive evaluation of a spherical function for a gradient.
81  */
82 template< typename Data_T, std::size_t k, std::size_t n, std::size_t N, std::size_t m >
84 {
85  /**
86  * Multiply gradient components and divide by multiplicities.
87  *
88  * \param tens A pointer to the tensor components.
89  * \param grad The gradient to evaluate the function at.
90  * \param w The result up to now.
91  *
92  * \return The result.
93  */
94  static inline Data_T evaluate( Data_T const*& tens, Data_T const* grad, Data_T w )
95  {
96  Data_T ret = WRecursiveTensorEvaluation< Data_T, k - 1, n, N, m + 1 >::evaluate( tens, grad, w * grad[ n ] / ( m + 1 ) );
97  return ret + WRecursiveTensorEvaluationLoop< boost::mpl::bool_< ( n + 1 < N ) >, Data_T, k, n + 1, N >::evaluate( tens, grad, w );
98  }
99 };
100 
101 /**
102  * Recursive evaluation of a spherical function for a gradient.
103  */
104 template< typename Data_T, std::size_t n, std::size_t N, std::size_t m >
105 struct WRecursiveTensorEvaluation< Data_T, 0, n, N, m >
106 {
107  /**
108  * Multiply the accumulated weight by the tensor component.
109  *
110  * \param tens A pointer to the tensor components.
111  * \param w The result up to now.
112  *
113  * \return The result.
114  */
115  static inline Data_T evaluate( Data_T const*& tens, Data_T const*, Data_T w )
116  {
117  return w * *( tens++ );
118  }
119 };
120 
121 #endif // WTENSORMETA_H
static Data_T evaluate(Data_T const *&, Data_T const *, Data_T)
Does nothing.
Definition: WTensorMeta.h:73
Multiplies gradient components and divides by multiplicities.
Definition: WTensorMeta.h:45
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:55
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:115
Recursive evaluation of a spherical function for a gradient.
Definition: WTensorMeta.h:84
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:94