OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TpsWerrf.cpp
Go to the documentation of this file.
1 #include "Utilities/TpsWerrf.h"
2 #include "FixedAlgebra/FTps.h"
4 #include <cmath>
5 
6 // Complex error function
7 // The algorithms is based on:
8 // Walter Gautschi,
9 // Efficient Computation of the Complex Error Function,
10 // SIAM J. Numer. Anal., Vol 7, No. 1, March 1970, pp. 187-198.
11 // ------------------------------------------------------------------------
12 
13 
14 void TpsWerrf(const FTps<double, 6> &xx, const FTps<double, 6> &yy,
16  static const double cc = 1.12837916709551; // 2 / sqrt(pi)
17  static const double xlim = 5.33;
18  static const double ylim = 4.29;
19  FTps<double, 6> x = (xx[0] < 0.0) ? -xx : xx;
20  FTps<double, 6> y = (yy[0] < 0.0) ? -yy : yy;
21 
22  if(y[0] < ylim && x[0] < xlim) {
23  // Inside limit rectangle: equation (3.8): q = s(z);
24  const FTps<double, 6> q =
25  (1.0 - y / ylim) * sqrt(1.0 - (x * x) / (xlim * xlim));
26 
27  // Equations (3.11).
28  const int nc = 6 + int(23.0 * q[0]);
29  const int nu = 9 + int(21.0 * q[0]);
30  const FTps<double, 6> h = 1.0 / (3.2 * q);
31  const FTps<double, 6> zzx = 1.6 * q + y; // h - i*z
32  const FTps<double, 6> zzy = x; // h - i*z
33  FTps<double, 6> xl = pow(h, 1 - nc);
34 
35  // Equations (3.12) for n = nu, nu - 1, ..., N.
36 
37  FTps<double, 6> rx[33];
38  FTps<double, 6> ry[33];
39  rx[nu+1] = 0.0; // r_{nu}
40  ry[nu+1] = 0.0; // r_{nu}
41  for(int n = nu; n >= 0; n--) { // for n = nu, nu-1, ..., 0
42  FTps<double, 6> tx = zzx + double(n + 1) * rx[n+1];
43  FTps<double, 6> ty = zzy - double(n + 1) * ry[n+1];
44  FTps<double, 6> tn = tx * tx + ty * ty;
45  rx[n] = 0.5 * tx / tn; // r_{n-1}
46  ry[n] = 0.5 * ty / tn; // r_{n-1}
47  }
48 
49  // Equations (3.12) for n = N, N - 1, ..., 0.
50  FTps<double, 6> sx; // s_{N}
51  FTps<double, 6> sy; // s_{N}
52  for(int n = nc; n >= 0; n--) { // for n = N, N-1, ..., 0
53  FTps<double, 6> saux = sx + xl;
54  sx = rx[n] * saux - ry[n] * sy; // s_{n-1}
55  sy = rx[n] * sy + ry[n] * saux; // s_{n-1}
56  xl *= h;
57  }
58 
59  // Equation (3.13).
60  wx = cc * sx;
61  wy = cc * sy;
62  } else {
63  // Outside limit rectangle: equations (3.12) for h = 0.
64  const FTps<double, 6> zzx = y; // - i*z
65  const FTps<double, 6> zzy = x; // - i*z
66  FTps<double, 6> rx; // r_{nu}
67  FTps<double, 6> ry; // r_{nu}
68  for(int n = 9; n >= 1; n--) { // for n = nu, nu-1, ..., 0
69  FTps<double, 6> tx = zzx + double(n) * rx;
70  FTps<double, 6> ty = zzy - double(n) * ry;
71  FTps<double, 6> tn = tx * tx + ty * ty;
72  rx = 0.5 * tx / tn; // r_{n-1}
73  ry = 0.5 * ty / tn; // r_{n-1}
74  }
75 
76  // Equation (3.13).
77  wx = cc * rx;
78  wy = cc * ry;
79  }
80 
81  // Equations (3.1).
82  if(yy[0] < 0.0) {
83  wx = 2.0 * exp(y * y - x * x) * cos(2.0 * x * y) - wx;
84  wy = -2.0 * exp(y * y - x * x) * sin(2.0 * x * y) - wy;
85  if(xx[0] > 0.0) wy = -wy;
86  } else {
87  if(xx[0] < 0.0) wy = -wy;
88  }
89 }
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
Tps< T > exp(const Tps< T > &x)
Exponential.
Definition: TpsMath.h:165
void TpsWerrf(const FTps< double, 6 > &xx, const FTps< double, 6 > &yy, FTps< double, 6 > &wx, FTps< double, 6 > &wy)
Complex error function.
Definition: TpsWerrf.cpp:14
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129