Garfield++ v2r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
Heed::Cubic Class Reference

Find solution to cubic equation. More...

#include <cubic.h>

Public Types

typedef std::complex< double > double_complex
 

Public Member Functions

double a () const
 
double b () const
 
double c () const
 
double d () const
 
double s_xzero () const
 
void put_a (double fa)
 
void put_b (double fb)
 
void put_c (double fc)
 
void put_d (double fd)
 
 Cubic ()
 
 Cubic (double fa, double fb, double fc, double fd)
 
double y (double x) const
 
void find_zero (double_complex &z1, double_complex &z2, double_complex &z3) const
 
int find_real_zero (double z[3]) const
 
int find_maxmin (double xmm[2], double ymm[2], int s_mm[2]) const
 

Detailed Description

Find solution to cubic equation.

Definition at line 22 of file cubic.h.

Member Typedef Documentation

◆ double_complex

typedef std::complex<double> Heed::Cubic::double_complex

Definition at line 24 of file cubic.h.

Constructor & Destructor Documentation

◆ Cubic() [1/2]

Heed::Cubic::Cubic ( )
inline

Definition at line 47 of file cubic.h.

47: da(0.0), db(0.0), dc(0.0), dd(0.0), s_dxzero(0) {}

◆ Cubic() [2/2]

Heed::Cubic::Cubic ( double  fa,
double  fb,
double  fc,
double  fd 
)
inline

Definition at line 48 of file cubic.h.

49 : da(fa), db(fb), dc(fc), dd(fd), s_dxzero(0) {}

Member Function Documentation

◆ a()

double Heed::Cubic::a ( ) const
inline

Definition at line 25 of file cubic.h.

25{ return da; }

Referenced by find_maxmin(), and Heed::operator<<().

◆ b()

double Heed::Cubic::b ( ) const
inline

Definition at line 26 of file cubic.h.

26{ return db; }

Referenced by Heed::operator<<().

◆ c()

double Heed::Cubic::c ( ) const
inline

Definition at line 27 of file cubic.h.

27{ return dc; }

Referenced by Heed::operator<<().

◆ d()

double Heed::Cubic::d ( ) const
inline

Definition at line 28 of file cubic.h.

28{ return dd; }

Referenced by Heed::operator<<().

◆ find_maxmin()

int Heed::Cubic::find_maxmin ( double  xmm[2],
double  ymm[2],
int  s_mm[2] 
) const

Definition at line 121 of file cubic.cpp.

121 {
122 mfunname(
123 "int Cubic::find_maxmin(double xmm[2], double ymm[2], int s_mm[2]) "
124 "const");
125 double ap = 3 * da;
126 double bp = 2 * db;
127 double cp = dc;
128 Parabol par(ap, bp, cp);
129 s_mm[0] = 0;
130 s_mm[1] = 0;
131 int qz = par.find_zero(xmm);
132 if (qz == 1) {
133 s_mm[0] = 0;
134 }
135 if (qz == 2) {
136 if (a() > 0) {
137 s_mm[0] = 1;
138 s_mm[1] = -1;
139 } else {
140 s_mm[0] = -1;
141 s_mm[1] = 1;
142 }
143 }
144 for (int n = 0; n < qz; ++n) {
145 ymm[n] = y(xmm[n]);
146 }
147 return qz;
148}
#define mfunname(string)
Definition: FunNameStack.h:45
double a() const
Definition: cubic.h:25
double y(double x) const
Definition: cubic.h:51

Referenced by Heed::operator<<().

◆ find_real_zero()

int Heed::Cubic::find_real_zero ( double  z[3]) const

Definition at line 74 of file cubic.cpp.

74 {
75 mfunname("int Cubic::find_real_zero(double z[3]) const");
79 find_zero(zc1, zc2, zc3);
80 double thresh = 10.0 * DBL_MIN;
81 int q = 0;
82 if (fabs(zc1.imag()) < thresh ||
83 (zc1.real() != 0.0 && fabs(zc1.imag() / zc1.real()) < thresh)) {
84 z[q] = zc1.real();
85 q++;
86 }
87 if (fabs(zc2.imag()) < thresh ||
88 (zc2.real() != 0.0 && fabs(zc2.imag() / zc2.real()) < thresh)) {
89 z[q] = zc2.real();
90 q++;
91 }
92 if (fabs(zc3.imag()) < thresh ||
93 (zc3.real() != 0.0 && fabs(zc3.imag() / zc3.real()) < thresh)) {
94 z[q] = zc3.real();
95 q++;
96 }
97 int n2 = 0;
98 for (int n1 = 0; n1 < q - 1; n1++) {
99 for (n2 = n1; n2 < q; n2++) {
100 if (z[n1] > z[n2]) {
101 double t = z[n1];
102 z[n1] = z[n2];
103 z[n2] = t;
104 }
105 }
106 }
107 for (int n1 = 0; n1 < q - 1; n1++) {
108 // TODO: debug.
109 if ((fabs(z[n1]) < thresh && fabs(z[n2]) < thresh) ||
110 fabs((z[n1] - z[n1 + 1]) / (z[n1] + z[n1 + 1])) < thresh) {
111 for (n2 = n1 + 1; n2 < q - 1; n2++) {
112 z[n2] = z[n2 + 1];
113 }
114 q--;
115 n1--;
116 }
117 }
118 return q;
119}
void find_zero(double_complex &z1, double_complex &z2, double_complex &z3) const
Definition: cubic.cpp:23
std::complex< double > double_complex
Definition: cubic.h:24
DoubleAc fabs(const DoubleAc &f)
Definition: DoubleAc.h:615

Referenced by Heed::operator<<(), and Heed::VanDerWaals::volume_of_mole().

◆ find_zero()

void Heed::Cubic::find_zero ( double_complex z1,
double_complex z2,
double_complex z3 
) const

Definition at line 23 of file cubic.cpp.

24 {
25 mfunname("void Cubic::find_zero(...) const");
26 const Cubic& t = (*this);
27 if (s_dxzero != 0) {
28 z1 = dz1;
29 z2 = dz2;
30 z3 = dz3;
31 return;
32 }
33
34 check_econd11a(da, == 0.0, "this is not cubic polynomial!", mcerr);
35 double a2 = db / da;
36 double a1 = dc / da;
37 double a0 = dd / da;
38 double Q = (3.0 * a1 - a2 * a2) / 9.0;
39 double R = (9.0 * a2 * a1 - 27.0 * a0 - 2.0 * a2 * a2 * a2) / 54.0;
40 double D = Q * Q * Q + R * R;
41 double sD = sqrt(fabs(D));
44 if (D >= 0.0) {
45 double tt = R + sD;
46 if (tt > 0.0) {
47 S = pow(tt, 1 / 3.0);
48 } else if (tt < 0.0) {
49 S = -pow(-tt, 1 / 3.0);
50 } else {
51 S = 0.0;
52 }
53 tt = R - sD;
54 if (tt > 0.0) {
55 T = pow(tt, 1 / 3.0);
56 } else if (tt < 0.0) {
57 T = -pow(-tt, 1 / 3.0);
58 } else {
59 T = 0.0;
60 }
61 } else {
62 S = pow(R + iu * sD, 1 / 3.0);
63 T = pow(R - iu * sD, 1 / 3.0);
64 }
65 z1 = -a2 / 3.0 + (S + T);
66 z2 = -a2 / 3.0 - (S + T) / 2.0 + 0.5 * iu * std::sqrt(3.0) * (S - T);
67 z3 = -a2 / 3.0 - (S + T) / 2.0 - 0.5 * iu * std::sqrt(3.0) * (S - T);
68 t.dz1 = z1;
69 t.dz2 = z2;
70 t.dz3 = z3;
71 t.s_dxzero = 3;
72}
#define check_econd11a(a, signb, add, stream)
Definition: FunNameStack.h:172
Cubic()
Definition: cubic.h:47
DoubleAc pow(const DoubleAc &f, double p)
Definition: DoubleAc.cpp:337
DoubleAc sqrt(const DoubleAc &f)
Definition: DoubleAc.cpp:314
#define mcerr
Definition: prstream.h:128

Referenced by find_real_zero(), and Heed::operator<<().

◆ put_a()

void Heed::Cubic::put_a ( double  fa)
inline

Definition at line 30 of file cubic.h.

30 {
31 da = fa;
32 s_dxzero = 0;
33 }

◆ put_b()

void Heed::Cubic::put_b ( double  fb)
inline

Definition at line 34 of file cubic.h.

34 {
35 db = fb;
36 s_dxzero = 0;
37 }

◆ put_c()

void Heed::Cubic::put_c ( double  fc)
inline

Definition at line 38 of file cubic.h.

38 {
39 dc = fc;
40 s_dxzero = 0;
41 }

◆ put_d()

void Heed::Cubic::put_d ( double  fd)
inline

Definition at line 42 of file cubic.h.

42 {
43 dd = fd;
44 s_dxzero = 0;
45 }

◆ s_xzero()

double Heed::Cubic::s_xzero ( ) const
inline

Definition at line 29 of file cubic.h.

29{ return s_dxzero; } // for debug

Referenced by Heed::operator<<().

◆ y()

double Heed::Cubic::y ( double  x) const
inline

Definition at line 51 of file cubic.h.

51 {
52 return da * x * x * x + db * x * x + dc * x + dd;
53 }

Referenced by find_maxmin().


The documentation for this class was generated from the following files: