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

Solution of a quadratic equation. More...

#include <parabola.h>

Public Member Functions

double a () const
 
double b () const
 
double c () const
 
void put_a (const double fa)
 
void put_b (const double fb)
 
void put_c (const double fc)
 
 Parabola ()=default
 Default constructor.
 
 Parabola (double fa, double fb, double fc)
 Constructor from coefficients.
 
 Parabola (double x[3], double y[3])
 Constructor from three points.
 
 Parabola (double x[3], double y[3], int)
 
 Parabola (double x1, double x2, double x3, double y1, double y2, double y3)
 Constructor from three points.
 
 Parabola (const Parabola &f)
 Copy constructor.
 
Parabolaoperator= (const Parabola &p)=default
 Copy assignment operator.
 
double eval (const double x) const
 Evaluate the function.
 
int find_zero (double xzero[2]) const
 
double find_maxmin ()
 
double determinant () const
 

Detailed Description

Solution of a quadratic equation.

Definition at line 19 of file parabola.h.

Constructor & Destructor Documentation

◆ Parabola() [1/6]

Heed::Parabola::Parabola ( )
default

Default constructor.

◆ Parabola() [2/6]

Heed::Parabola::Parabola ( double  fa,
double  fb,
double  fc 
)
inline

Constructor from coefficients.

Definition at line 43 of file parabola.h.

44 : da(fa), db(fb), dc(fc) {}

◆ Parabola() [3/6]

Heed::Parabola::Parabola ( double  x[3],
double  y[3] 
)

Constructor from three points.

Definition at line 22 of file parabola.cpp.

22 {
23 mfunname("Parabola::Parabola(double x[3], double y[3])");
24
25 check_econd12a(x[0], ==, x[1], "x[2]=" << x[2] << " y[0]=" << y[0] << " y[1]="
26 << y[1] << " y[2]=" << y[2] << '\n',
27 mcerr);
28 check_econd12a(x[0], ==, x[2], "x[1]=" << x[1] << " y[0]=" << y[0] << " y[1]="
29 << y[1] << " y[2]=" << y[2] << '\n',
30 mcerr);
31 check_econd12a(x[1], ==, x[2], "x[0]=" << x[0] << " y[0]=" << y[0] << " y[1]="
32 << y[1] << " y[2]=" << y[2] << '\n',
33 mcerr);
34 DynArr<DoubleAc> mat(3, 3);
35 DynLinArr<DoubleAc> par(3);
36 DynLinArr<DoubleAc> f(3);
37 for (int i = 0; i < 3; ++i) {
38 f[i] = y[i];
39 mat.ac(i, 2) = 1.0;
40 mat.ac(i, 1) = x[i];
41 mat.ac(i, 0) = x[i] * x[i];
42 }
43 int ierr;
44 int szero;
45 DynArr<DoubleAc> mat_inv;
46 inverse_DynArr_prot(mat, mat_inv, szero, ierr);
47 // check_econd11a( ierr, != 0 , "should never happen\n", mcerr );
48 if (ierr == 0) {
49 par = mat_inv * f;
50 da = par[0];
51 db = par[1];
52 dc = par[2];
53 } else {
54 da = 0.0;
55 DynLinArr<int> s_var(3);
56 s_var[0] = 0;
57 s_var[1] = 1;
58 s_var[2] = 1;
59 DynArr<DoubleAc> mat_inv1(3, 3);
60 // int ierr1;
61 // inverse_DynArr(mat, s_var, mat_inv, ierr, mat_inv1, ierr1);
62 inverse_DynArr_prot(mat, s_var, mat_inv, szero, ierr);
63 if (ierr != 0) {
64 // what if x1 and x2 are the same but the both differ from x0
65 // Then the following should help:
66 mat.ac(1, 1) = mat.ac(0, 1);
67 mat.ac(1, 2) = mat.ac(0, 2);
68 f[1] = f[0];
69 s_var[0] = 0;
70 s_var[1] = 1;
71 s_var[2] = 1;
72 inverse_DynArr_prot(mat, s_var, mat_inv, szero, ierr);
73 check_econd11a(ierr, != 0,
74 "should never happen\nmat=" << mat << "\ns_var=" << s_var
75 << "\nmat_inv=" << mat_inv,
76 mcerr);
77 }
78 par = mat_inv * f;
79 db = par[1];
80 dc = par[2];
81 }
82}
#define check_econd11a(a, signb, add, stream)
Definition: FunNameStack.h:172
#define check_econd12a(a, sign, b, add, stream)
Definition: FunNameStack.h:181
#define mfunname(string)
Definition: FunNameStack.h:45
void inverse_DynArr_prot(const DynArr< DoubleAc > &mi, DynArr< DoubleAc > &mr, int &szero, int &serr, int s_stop)
Definition: inverse.cpp:17
#define mcerr
Definition: prstream.h:128

◆ Parabola() [4/6]

Heed::Parabola::Parabola ( double  x[3],
double  y[3],
int   
)

Constructor from three points. At the third one, the derivative of the function is supplied instead of the function.

Definition at line 155 of file parabola.cpp.

155 {
156 mfunname("Parabola::Parabola(double x[3], double y[3], int)");
157
158 check_econd12(x[0], ==, x[1], mcerr);
159 // check_econd12( x[0] , == , x[2] , mcerr);
160 // check_econd12( x[1] , == , x[2] , mcerr);
161
162 DynArr<DoubleAc> mat(3, 3);
163 DynLinArr<DoubleAc> par(3);
164 DynLinArr<DoubleAc> f(3);
165 for (int i = 0; i < 3; ++i) f[i] = y[i];
166 for (int i = 0; i < 2; ++i) {
167 mat.ac(i, 2) = 1.0;
168 mat.ac(i, 1) = x[i];
169 mat.ac(i, 0) = x[i] * x[i];
170 }
171 mat.ac(2, 2) = 0.0;
172 mat.ac(2, 1) = 1.0;
173 mat.ac(2, 0) = 2.0 * x[2];
174 int ierr;
175 int szero;
176 DynArr<DoubleAc> mat_inv;
177 inverse_DynArr_prot(mat, mat_inv, szero, ierr);
178 check_econd11a(ierr, != 0, "should never happen\n", mcerr);
179 par = mat_inv * f;
180 if (fabs(par[0]) == 0.0) {
181 da = 0.0;
182 } else {
183 da = par[0];
184 db = par[1];
185 dc = par[2];
186 }
187}
#define check_econd12(a, sign, b, stream)
Definition: FunNameStack.h:163
DoubleAc fabs(const DoubleAc &f)
Definition: DoubleAc.h:615

◆ Parabola() [5/6]

Heed::Parabola::Parabola ( double  x1,
double  x2,
double  x3,
double  y1,
double  y2,
double  y3 
)

Constructor from three points.

Definition at line 84 of file parabola.cpp.

86 : s_det(0), s_dxzero(0) {
87 mfunname("Parabola::Parabola(double x[3], double y[3])");
88
89 check_econd12a(x1, ==, x2, "x3=" << x3 << " y1=" << y1 << " y2=" << y2
90 << " y3=" << y3 << '\n',
91 mcerr);
92 check_econd12a(x1, ==, x3, "x2=" << x2 << " y1=" << y1 << " y2=" << y2
93 << " y3=" << y3 << '\n',
94 mcerr);
95 check_econd12a(x2, ==, x3, "x1=" << x1 << " y1=" << y1 << " y2=" << y2
96 << " y3=" << y3 << '\n',
97 mcerr);
98 DynArr<DoubleAc> mat(3, 3);
99 DynLinArr<DoubleAc> par(3);
100 DynLinArr<DoubleAc> f(3);
101 f[0] = y1;
102 mat.ac(0, 2) = 1.0;
103 mat.ac(0, 1) = x1;
104 mat.ac(0, 0) = x1 * x1;
105 f[1] = y2;
106 mat.ac(1, 2) = 1.0;
107 mat.ac(1, 1) = x2;
108 mat.ac(1, 0) = x2 * x2;
109 f[2] = y3;
110 mat.ac(2, 2) = 1.0;
111 mat.ac(2, 1) = x3;
112 mat.ac(2, 0) = x3 * x3;
113
114 int ierr;
115 int szero;
116 DynArr<DoubleAc> mat_inv;
117 inverse_DynArr_prot(mat, mat_inv, szero, ierr);
118 // check_econd11a( ierr, != 0 , "should never happen\n", mcerr );
119 if (ierr == 0) {
120 par = mat_inv * f;
121 da = par[0];
122 db = par[1];
123 dc = par[2];
124 } else {
125 da = 0.0;
126 DynLinArr<int> s_var(3);
127 s_var[0] = 0;
128 s_var[1] = 1;
129 s_var[2] = 1;
130 DynArr<DoubleAc> mat_inv1(3, 3);
131 // int ierr1;
132 // inverse_DynArr(mat, s_var, mat_inv, ierr, mat_inv1, ierr1);
133 inverse_DynArr_prot(mat, s_var, mat_inv, szero, ierr);
134 if (ierr != 0) {
135 // what if x1 and x2 are the same but the both differ from x0
136 // Then the following should help:
137 mat.ac(1, 1) = mat.ac(0, 1);
138 mat.ac(1, 2) = mat.ac(0, 2);
139 f[1] = f[0];
140 s_var[0] = 0;
141 s_var[1] = 1;
142 s_var[2] = 1;
143 inverse_DynArr_prot(mat, s_var, mat_inv, szero, ierr);
144 check_econd11a(ierr, != 0,
145 "should never happen\nmat=" << mat << "\ns_var=" << s_var
146 << "\nmat_inv=" << mat_inv,
147 mcerr);
148 }
149 par = mat_inv * f;
150 db = par[1];
151 dc = par[2];
152 }
153}

◆ Parabola() [6/6]

Heed::Parabola::Parabola ( const Parabola f)

Copy constructor.

Definition at line 20 of file parabola.cpp.

20{ *this = f; }

Member Function Documentation

◆ a()

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

Definition at line 21 of file parabola.h.

21{ return da; }

Referenced by Heed::operator<<().

◆ b()

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

Definition at line 22 of file parabola.h.

22{ return db; }

Referenced by Heed::operator<<().

◆ c()

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

Definition at line 23 of file parabola.h.

23{ return dc; }

Referenced by Heed::operator<<().

◆ determinant()

double Heed::Parabola::determinant ( ) const
inline

Definition at line 66 of file parabola.h.

66 {
67 const Parabola& t = (*this);
68 if (s_det == 0) {
69 t.s_det = 1;
70 t.det = db * db - 4 * da * dc;
71 }
72 return det;
73 }
Parabola()=default
Default constructor.

Referenced by find_zero().

◆ eval()

double Heed::Parabola::eval ( const double  x) const
inline

Evaluate the function.

Definition at line 60 of file parabola.h.

60{ return da * x * x + db * x + dc; }

◆ find_maxmin()

double Heed::Parabola::find_maxmin ( )

Definition at line 234 of file parabola.cpp.

234 {
235 mfunname("double Parabola::find_maxmin(void)");
236 check_econd11(da, == 0, mcerr);
237 return -db / (2.0 * da);
238}
#define check_econd11(a, signb, stream)
Definition: FunNameStack.h:155

◆ find_zero()

int Heed::Parabola::find_zero ( double  xzero[2]) const

Definition at line 189 of file parabola.cpp.

189 {
190 mfunnamep("int Parabola::find_zero(double xzero[2]) const");
191 const Parabola& t = (*this);
192 if (s_dxzero == 0) {
193 // mcout<<"Parabola::find_zero: s_dxzero == 0\n";
194 t.s_dxzero = 1;
195 if (da == 0.0) {
196 if (db == 0.0) {
197 funnw.ehdr(mcerr);
198 mcerr << "can not find zero\n";
199 spexit(mcerr);
200 } else {
201 t.qdxzero = 1;
202 t.dxzero[0] = -dc / db;
203 }
204 } else {
205 if (determinant() < 0.0) {
206 t.qdxzero = 0;
207 t.dxzero[0] = 0;
208 t.dxzero[1] = 0;
209 } else if (determinant() == 0.0) {
210 t.qdxzero = 1;
211 t.dxzero[0] = -db / (2.0 * da);
212 } else {
213 const double sq = sqrt(determinant());
214 t.qdxzero = 2;
215 if (da > 0.0) {
216 t.dxzero[0] = (-db - sq) / (2.0 * da);
217 t.dxzero[1] = (-db + sq) / (2.0 * da);
218 } else {
219 t.dxzero[1] = (-db - sq) / (2.0 * da);
220 t.dxzero[0] = (-db + sq) / (2.0 * da);
221 }
222 // mcout<<"Parabola::find_zero: t.dxzero[0]="<<t.dxzero[0]
223 // <<" dxzero[0]="<<dxzero[0]<<'\n';
224 // mcout<<"Parabola::find_zero: t.dxzero[1]="<<t.dxzero[1]
225 // <<" dxzero[1]="<<dxzero[1]<<'\n';
226 }
227 }
228 }
229 xzero[0] = dxzero[0];
230 xzero[1] = dxzero[1];
231 return qdxzero;
232}
#define mfunnamep(string)
Definition: FunNameStack.h:49
#define spexit(stream)
Definition: FunNameStack.h:256
double determinant() const
Definition: parabola.h:66
DoubleAc sqrt(const DoubleAc &f)
Definition: DoubleAc.cpp:314

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

◆ operator=()

Parabola & Heed::Parabola::operator= ( const Parabola p)
default

Copy assignment operator.

◆ put_a()

void Heed::Parabola::put_a ( const double  fa)
inline

Definition at line 24 of file parabola.h.

24 {
25 da = fa;
26 s_det = 0;
27 s_dxzero = 0;
28 }

◆ put_b()

void Heed::Parabola::put_b ( const double  fb)
inline

Definition at line 29 of file parabola.h.

29 {
30 db = fb;
31 s_det = 0;
32 s_dxzero = 0;
33 }

◆ put_c()

void Heed::Parabola::put_c ( const double  fc)
inline

Definition at line 34 of file parabola.h.

34 {
35 dc = fc;
36 s_det = 0;
37 s_dxzero = 0;
38 }

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