Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
compact.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * *
3  * / / / / __ \ \ / / *
4  * / /__ / / / _ \ \ \/ / *
5  * / ___ / | |/_/ / /\ \ *
6  * / / / / \_\ / / \ \ *
7  * *
8  * Jakub Benda (c) 2014 *
9  * Charles University in Prague *
10  * *
11 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HEX_COMPACT
14 #define HEX_COMPACT
15 
16 #include <cassert>
17 #include <cmath>
18 #include <iostream>
19 
20 #include "misc.h"
21 #include "special.h"
22 
33 template <class Functor, typename FType> FType lim (Functor F, double x, int * n = nullptr)
34 {
35  // initial position
36  double x0;
37  if (x > 0.) x0 = std::isfinite(x) ? 0.5 * x : 1.;
38  if (x < 0.) x0 = std::isfinite(x) ? 0.5 * x : -1.;
39 
40  // function value
41  FType f0 = F(x0), f;
42 
43  // main loop
44  int i;
45  for (i = 0; n == nullptr or i <= *n; i++)
46  {
47  // advance x0
48  if (x == special::constant::Inf)
49  x0 *= 2.;
50  else if (x == -special::constant::Inf)
51  x0 *= 2.;
52  else
53  x0 = 0.5 * (x + x0);
54 
55  // evaluate function
56  f = F(x0);
57 
58  // check convergence
59  if (f == f0)
60  break;
61  else
62  f0 = f;
63  }
64 
65  // return
66  if (n != nullptr) *n = i;
67  return f;
68 }
69 
76 template <typename FType> class ICompactification
77 {
78 public:
79  virtual ~ICompactification() {}
80  virtual double scale (double x) const = 0;
81  virtual double unscale (double t) const = 0;
82  virtual double Jacobian (double t) const = 0;
83  virtual FType operator() (double t) const = 0;
84 };
85 
99 template <class Functor, typename FType> class CompactificationF : public ICompactification<FType>
100 {
101 public:
108  Functor f,
109  double a,
110  double b
111  ) : F(f), A(a), B(b), M(0.5*(b+a)), D(0.5*(b-a))
112  {
113  if (not std::isfinite(a) or not std::isfinite(b))
114  throw exception("[CompactificationF] Interval has to be finite!");
115  }
116 
118  double scale (double x) const
119  {
120  assert(A <= x and x <= B);
121  return (x - M) / D;
122  }
123 
125  double unscale (double t) const
126  {
127  assert(std::abs(t) <= 1.);
128  return M + t * D;
129  }
130 
132  double Jacobian(double t) const
133  {
134  assert(std::abs(t) <= 1.);
135  return D;
136  }
137 
142  FType operator() (double t) const
143  {
144  return F(M + t * D);
145  }
146 
147 private:
148  Functor F;
149  double A, B, M, D;
150 };
151 
166 template <class Functor, typename FType> class CompactificationL : public ICompactification<FType>
167 {
168 public:
169 
175  Functor f,
176  double b = 0.,
177  bool limit = true,
178  double L = 1.0
179  ) : F(f), B(b), L(L), Limit(limit) {}
180 
182  double scale (double x) const
183  {
184  assert (x <= B);
185  return std::isfinite(x) ? (x - B + L) / (x - B - L) : 1.;
186  }
187 
189  double unscale (double t) const
190  {
191  assert(std::abs(t) <= 1.);
192  return (t == 1.) ? special::constant::Inf : B - L * (1. + t) / (1. - t);
193  }
194 
196  double Jacobian(double t) const
197  {
198  assert(std::abs(t) <= 1.);
199  return -2 * L / ((1. - t) * (1. - t));
200  }
201 
206  FType operator() (double t) const
207  {
208  if (t == 1.)
209  {
210  if (Limit)
211  return lim<decltype(F),FType>(F, special::constant::Inf);
212  else
213  return F(special::constant::Inf);
214  }
215  else
216  {
217  return F(B - L * (1. + t) / (1. - t));
218  }
219  }
220 
221 private:
222  Functor F;
223  double B, L;
224  bool Limit;
225 };
226 
242 template <class Functor, typename FType> class CompactificationR : public ICompactification<FType>
243 {
244 public:
245 
251  Functor f,
252  double a = 0.,
253  bool limit = true,
254  double L = 1.0
255  ) : F(f), A(a), L(L), Limit(limit) {}
256 
258  double scale (double x) const
259  {
260  assert (x >= A);
261  return std::isfinite(x) ? (x - A - L) / (x - A + L) : 1.;
262  }
263 
265  double unscale (double t) const
266  {
267  assert(std::abs(t) <= 1.);
268  return (t == 1.) ? special::constant::Inf : A + L * (1. + t) / (1. - t);
269  }
270 
272  double Jacobian(double t) const
273  {
274  assert(std::abs(t) <= 1.);
275  return 2 * L / ((1. - t) * (1. - t));
276  }
277 
282  FType operator() (double t) const
283  {
284  if (t == 1.)
285  {
286  if (Limit)
287  return lim<decltype(F),FType>(F,special::constant::Inf);
288  else
289  return F(special::constant::Inf);
290  }
291  else
292  {
293  return F(A + L * (1. + t) / (1. - t));
294  }
295  }
296 
297 private:
298  Functor F;
299  double A, L;
300  bool Limit;
301 };
302 
311 template <class Functor, typename FType> class CompactIntegrand
312 {
313 public:
314 
321  Functor f,
322  double a = 0.,
323  double b = special::constant::Inf,
324  bool limit = true,
325  double L = 1.0
326  ) : Compactification(nullptr) {
327  if (std::isfinite(a) and std::isfinite(b))
328  Compactification = new CompactificationF<Functor,FType> (f, a, b);
329  else if (std::isfinite(a) and not std::isfinite(b))
330  Compactification = new CompactificationR<Functor,FType> (f, a, limit, L);
331  else if (not std::isfinite(a) and std::isfinite(b))
332  Compactification = new CompactificationL<Functor,FType> (f, b, limit, L);
333  else
334  throw exception("[CompactIntegrand] Compactification of (-∞,∞) interval is not implemeted.");
335  }
336 
338  {
339  delete Compactification;
340  }
341 
349  inline FType operator() (double t) const
350  {
351  // evaluate function
352  FType ft = Compactification->operator()(t);
353 
354  if (ft == FType(0.))
355  {
356  // return zero
357  return 0.;
358  }
359  else
360  {
361  // multiply by the Jacobian
362  return ft * Compactification->Jacobian(t);
363  }
364  }
365 
367  inline double scale (double x) const
368  {
369  return Compactification->scale(x);
370  }
371 
373  inline double unscale (double t) const
374  {
375  return Compactification->unscale(t);
376  }
377 
378 private:
379 
380  ICompactification<FType> * Compactification;
381 };
382 
383 #endif
double scale(double x) const
Scale value from the original interval [a,b] into compactified interval [-1,1].
Definition: compact.h:258
FType operator()(double t) const
Evaluate the compactified function.
Definition: compact.h:142
FType operator()(double t) const
Definition: compact.h:282
Compactification of a function from (-∞,b] to (-1,1].
Definition: compact.h:166
CompactificationL(Functor f, double b=0., bool limit=true, double L=1.0)
Definition: compact.h:174
virtual double unscale(double t) const =0
double scale(double x) const
Scale value from the original interval [a,b] into compactified interval [-1,1].
Definition: compact.h:182
virtual FType operator()(double t) const =0
double unscale(double t) const
Unscale value from the compactified interval [-1,1] into the original interval [a,b].
Definition: compact.h:189
virtual ~ICompactification()
Definition: compact.h:79
CompactificationR(Functor f, double a=0., bool limit=true, double L=1.0)
Definition: compact.h:250
Compactification of a function from finite interval.
Definition: compact.h:99
CompactificationF(Functor f, double a, double b)
Constructor of the class. The parameters &quot;a&quot; and &quot;b&quot; specify original definition interval [a...
Definition: compact.h:107
double unscale(double t) const
Unscale value from the compactified interval [-1,1] into the original interval [a,b].
Definition: compact.h:373
~CompactIntegrand()
Definition: compact.h:337
Compactification of a function from [a,+∞) to [-1,1).
Definition: compact.h:242
FType operator()(double t) const
Definition: compact.h:349
Compactification.
Definition: compact.h:76
double Jacobian(double t) const
Evaluate Jacobian of the transformation.
Definition: compact.h:196
const double Inf
Definition: special.h:67
double unscale(double t) const
Unscale value from the compactified interval [-1,1] into the original interval [a,b].
Definition: compact.h:265
double Jacobian(double t) const
Evaluate Jacobian of the transformation.
Definition: compact.h:272
CLArrayView exception
double scale(double x) const
Scale value from the original interval [a,b] into compactified interval [-1,1].
Definition: compact.h:118
virtual double scale(double x) const =0
CompactIntegrand(Functor f, double a=0., double b=special::constant::Inf, bool limit=true, double L=1.0)
Definition: compact.h:320
FType lim(Functor F, double x, int *n=nullptr)
Compute limit.
Definition: compact.h:33
virtual double Jacobian(double t) const =0
rArray abs(const cArrayView u)
Definition: arrays.cpp:19
double Jacobian(double t) const
Evaluate Jacobian of the transformation.
Definition: compact.h:132
FType operator()(double t) const
Definition: compact.h:206
Compactification multiplied by its jacobian.
Definition: compact.h:311
double scale(double x) const
Scale value from the original interval [a,b] into compactified interval [-1,1].
Definition: compact.h:367
double unscale(double t) const
Unscale value from the compactified interval [-1,1] into the original interval [a,b].
Definition: compact.h:125