Tools.h 5.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/** \file Tools.h */

#ifndef TOOLS_H
#define TOOLS_H

#include <boost/array.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/boost_array.hpp>

#include "MetaTools.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
12
#include "Helpers.h"
13
14

namespace tools {
Praetorius, Simon's avatar
Praetorius, Simon committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  
  struct Random : AbstractFunction<double,WorldVector<double> >
  {
    Random(double mean_, double amplitude_) : mean(mean_), amplitude(amplitude_)
    {
      #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      mpi::startRand();
      #else
      std::srand(Helpers::getRandomSeed());
      #endif
    }
    double operator()(const WorldVector<double> &x) const
    {
      return mean + amplitude * (std::rand() / static_cast<double>(RAND_MAX) - 0.5);
    }
    
  private:
    double mean;
    double amplitude;
  };
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

/// Quadrature Rules
///____________________________________________________________________________________

  struct SimpsonQuad
  {
    template<typename Functor, typename T> /// requires Callable1<Functor, T>, Addable<T>, Multiplicable<T>
    T quad(Functor& f, T a, T b)
    {
      return (b - a) * (f(a) * (1.0 / 6.0) + f((a + b) * 0.5) * (4.0 / 6.0) + f(b) * (1.0 / 6.0));
    }
  };


  template<typename Container>
  struct GeneralQuad
  {
    GeneralQuad(Container& xi_, Container& ci_) : xi(xi_), ci(ci_) {}
    
    template<typename Functor, typename T>
    struct IntegralValue {
      IntegralValue(Functor& f_, T a_, T b_) : f(f_), a(a_), b(b_)
      {
	h = b - a;
      }
      void operator()(double& x, double& c, T& value)
      {
	value += f(a + x * h) * c;
      }
      Functor& f;
      T a, b, h;
    };

    template<typename Functor, typename T> /// requires Callable1<Functor, T>, Addable<T>, Multiplicable<T>, Nullifyable<T>
    T quad(Functor& f, T a, T b)
    {
      T I; nullify(I);

      IntegralValue<Functor, T> integralValue(f,a,b);
      for_each<boost::fusion::result_of::size<Container>::value - 1>::accumulate(xi, ci, I, integralValue);
      
      return (b - a) * I;
    }

  private:
    Container& xi; // intermediate points
    Container& ci; // weights
  };


  template<typename Quad>
  struct CompositeQuad
  {
    CompositeQuad() : q(*(new SimpsonQuad)), n(1) {}
    CompositeQuad(Quad& q_, unsigned int n_) : q(q_), n(n_) {}

    template<typename Functor, typename T> /// requires Callable1<Functor, T>, Addable<T>, Multiplicable<T>, Nullifyable<T>
    T quad(Functor& f, T a, T b)
    {
      T h = (b - a) * (1.0 / (n - 1.0));
      T I; nullify(I);
      for (unsigned int i = 1; i < n; i++)
	I += q.quad(f, a + (i - 1.0) * h, a + i * h);
      return h * I;
    }

    template<typename Functor, typename Vector, typename T> /// requires Callable1<Functor, T>, Addable<T>, Multiplicable<T>, Nullifyable<T>
    T quad(Functor& f, Vector& x)
    {
      typename Vector::iterator xIter;
      T I; nullify(I);
      for (xIter = x.begin(); xIter+1 != x.end(); xIter++)
	I += (*(xIter+1) - *xIter) * q.quad(f, *xIter, *(xIter+1));
      return I;
    }
    
  private:
    Quad& q; 		// quadrature rule
    unsigned int n; 	// number of points
  };


  struct SequenceQuad
  {
    // integration zwischen zwei Punkten mittels Trapezregel
    template<typename Vector1, typename Vector2, typename T> /// requires Callable1<Functor, T>, Addable<T>, Multiplicable<T>, Nullifyable<T>
    T quad(Vector1& y, Vector2& x)
    {
      T I; nullify(I);
      for (size_t i = 0; i < x.size()-1; i++)
	I += (x[i+1] - x[i]) * 0.5*(y[i] + y[i+1]);
      return I;
    }
  };  

  
/// Regression, Interpolation and Approximation
///____________________________________________________________________________________


  struct Regression
  {
    Regression(int DOW_) : DOW(DOW_)
    {
      coefficients = std::vector<double>(DOW+1, 0.0);
    }
    
    template<typename Vector1, typename Vector2>
    void apply(Vector1& points, Vector2& values) {
      int row = points.size();
      int col = DOW + 1;
      mtl::dense2D<double> A(row, col);
      mtl::dense_vector<double> b(row);
      for (size_t i = 0; i < row; i++) {
	for (size_t j = 0; j < col-1; j++)
	  A(i,j) = points[i][j];
	A(i,DOW) = 1.0;
	b[i] = values[i];
      }
      mtl::dense2D<double> Q(row, row), Qt(row, row), R(row, col);
      boost::tie(Q, R) = mtl::matrix::qr_factors(A);
      
      mtl::dense_vector<double> bQ(col), x(col);
      using mtl::iall;
      using mtl::irange;
      irange cols(0, col);
      bQ = trans(Q[iall][cols])*b;
      x = mtl::matrix::upper_trisolve(R[cols][cols], bQ[cols]);
      for (int i = 0; i < DOW+1; i++)
	coefficients[i] = x[i];
    }
    
    template<typename Vector1, typename Vector2, typename Vector3>
    void apply(Vector1& points, Vector2& values, Vector3& weights) {
      int row = points.size();
      int col = DOW + 1;
      mtl::dense2D<double> A(row, col);
      mtl::dense_vector<double> b(row);
      for (size_t i = 0; i < row; i++) {
	for (size_t j = 0; j < col-1; j++)
	  A(i,j) = weights[i] * points[i][j];
	A(i,DOW) = weights[i];
	b[i] = weights[i] * values[i];
      }
      mtl::dense2D<double> Q(row, row), Qt(row, row), R(row, col);
      boost::tie(Q, R) = mtl::matrix::qr_factors(A);
      
      mtl::dense_vector<double> bQ(col), x(col);
      using mtl::iall;
      using mtl::irange;
      irange cols(0, col);
      bQ = trans(Q[iall][cols])*b;
      x = mtl::matrix::upper_trisolve(R[cols][cols], bQ[cols]);
      for (int i = 0; i < DOW+1; i++)
	coefficients[i] = x[i];
    }
    
    template<typename Coords>
    double value(Coords& x) {
      double f = coefficients[DOW];
      for (int i = 0; i < DOW; i++)
	f += coefficients[i]*x[i];
      return f;
    }
    
  private:
    int DOW;
    std::vector<double> coefficients; // c[0]*x[0] + c[1]*x[1] + c[2]
  };

} // end namespace

#endif // TOOLS_H