kogmo_fob/trackerServer/polynomial.h
Go to the documentation of this file.
1 
18 #ifndef __POLYNOMIAL_H
19 #define __POLYNOMIAL_H
20 #include "math.h"
21 #include "complex.h"
22 double cbrt(double x){//Kubikwurzel
23  if (x<0) return -pow(-x,1.0/3.0);
24  return pow(x,1.0/3.0);
25 }
26 
27 // double epsilon=0.0000001;
28 
29 //x1/2=(-b+-sqrt(b^2-4ac))/(2a)
30 int solve_quadratic(double a,double b,double c,double* x1,double* x2)
31 {
32  if (a==0)//Lineares Gleichungssystem
33  {
34  if (b==0) return 0;//c ist konstant
35  *x1=-c/b;
36  return 1;
37  }
38  double d=b*b-4*a*c;
39  if (d<0) return 0;
40  *x1=(-b+sqrt(d))/2*a;
41  *x2=(-b-sqrt(d))/2*a;
42  return 2;
43 
44 
45 }
46 
47 //Solve cubic nach http://www.montgelas-gymnasium.de/mathe/kubfa/leitkubgleich.html
48 int solve_cubic(double a,double b,double c,double d,double* x1,double* x2,double* x3)
49 { if (a==0) return solve_quadratic(b,c,d,x1,x2);
50  double pi=3.141592653589793;
51  double p=(3*a*c-b*b)/(3*a*a);
52  double q=(2*b*b*b)/(27*a*a*a)-(b*c)/(3*a*a)+d/a;
53  double D=(q*q/4)+(p*p*p/27);
54  if (D>epsilon)
55  { //Nur eine reelle L�ung
56  *x1=cbrt(-q/2+sqrt(D))+cbrt(-q/2-sqrt(D))-b/(3*a);
57  *x2=HUGE_VAL;
58  *x3=HUGE_VAL;
59  return 1;
60  }
61  else if (D<-epsilon)
62  {
63  double alpha=acos(-q/(2*sqrt(-p*p*p/27)));
64  *x1=2*sqrt(-p/3)*cos(alpha/3+0.0/3.0*pi)-b/(3*a);
65  *x2=2*sqrt(-p/3)*cos(alpha/3+2.0/3.0*pi)-b/(3*a);
66  *x3=2*sqrt(-p/3)*cos(alpha/3+4.0/3.0*pi)-b/(3*a);
67  return 3;
68  }
69  else //D is nearby zero
70  {
71  if (abs(p)<epsilon)
72  {
73  *x1=*x2=*x3=-b/(3*a);
74  return 3;
75  } else
76  { *x1=cbrt(-4*q)-b/(3*a);
77  *x2=*x3=cbrt(q/2)-b/(3*a);
78  return 3;
79  }
80  }
81 }
82 
83 double sqrt2(double x)
84 {
85  return sqrt(abs(x));
86 }
87 
88 
89 void setpos(int p,complex a,double* x1,double* x2,double* x3,double* x4)
90 {
91 switch (p)
92 {
93 case 1:*x1=a.r;break;
94 case 2:*x2=a.r;break;
95 case 3:*x3=a.r;break;
96 case 4:*x4=a.r;
97 }
98 }
99 
100 //Nach http://de.wikipedia.org/wiki/Biquadratische_Gleichung
101 int solve_biquadratic(double a4,double a3,double a2,double a1,double a0,double* x1,double* x2,double* x3,double* x4)
102 {
103  if (a4==0) return solve_cubic(a3,a2,a1,a0,x1,x2,x3);
104  if ((a3==0) && (a1==0))
105  {
106  if (solve_quadratic(a4,a2,a0,x1,x3)==0) return 0;
107  int s=0;
108  if (*x1>=0)
109  {
110  *x1=sqrt(*x1);
111  *x2=-*x1;
112  s+=2;
113  }
114  if (*x3>=0)
115  {
116  if (s==0)
117  {
118  *x1=sqrt(*x3);
119  *x2=-*x1;
120  s+=2;
121  } else
122  {
123  *x3=sqrt(*x3);
124  *x4=-*x3;
125  s+=2;
126  }
127  }
128  return s;
129  }
130  complex A=r2c(a4);
131  complex B=r2c(a3);
132  complex C=r2c(a2);
133  complex D=r2c(a1);
134  complex E=r2c(a0);
135  complex a = cadd(cdiv (cmul(cmul(r2c(-3),B),B) , cmul(cmul(r2c(8),A),A)) , cdiv(C,A));
136 
137  complex b = cadd(csub(cdiv(cpow(B,3),cmul(r2c(8),cpow(A,3))),
138  cdiv(cmul(B,C),cmul(cmul(r2c(2),A),A))),
139  cdiv(D,A));
140 
141  complex c = cadd(csub(cadd(cdiv(cmul(r2c(-3),cpow(B,4)),cmul(r2c(256),cpow(A,4))),
142  cdiv(cmul(C,cmul(B,B)),cmul(r2c(16),cpow(A,3)))),
143  cdiv(cmul(B,D),cmul(cmul(r2c(4),A),A))),
144  cdiv(E,A));
145 
146  if (isEqualE(b,czero))
147  { if (solve_quadratic(1,a.r,c.r,x1,x3)==0) return 0;
148  int s=0;
149  if (*x1>=0)
150  {
151  *x1=sqrt(*x1);
152  *x2=-*x1-a3/(4*a4);
153  *x1-=a3/(4*a4);
154  s+=2;
155  }
156  if (*x3>=0)
157  {
158  if (s==0)
159  {
160  *x1=sqrt(*x3);
161  *x2=-*x1-a3/(4*a4);
162  *x1-=a3/(4*a4);
163  s+=2;
164  } else
165  {
166  *x3=sqrt(*x3);
167  *x4=-*x3-a3/(4*a4);
168  *x3-=a3/(4*a4);
169  s+=2;
170  }
171  }
172  return s;
173  }
174  complex P = csub(cdiv(cmul(a,a),r2c(-12)),c);
175  complex Q = csub(cadd(cdiv(cpow(a,3),r2c(-108)),cdiv(cmul(a,c),r2c(3))),cdiv(cmul(b,b),r2c(8)));
176  complex R = cadd(cdiv(Q,r2c(2)),csqrt( cadd(cdiv(cmul(Q,Q),r2c(4)),cdiv(cpow(P,3),r2c(27))) ));
177  complex U = czero;
178  if (isEqualE(R,czero))
179  {
180  U=czero;
181  } else
182  {
183  U = cexp(cdiv(cln(R),r2c(3)));
184  }
185  complex y=csub(cmul(r2c(-5.0/6.0),a),U);
186  if (!isEqualE(U,czero))
187  {
188  y=cadd(y,cdiv(P,cmul(r2c(3),U)));
189  }
190  // -B/(4*A) + (sqrt(a+2*y) + sqrt(-(a+2*y) - 2*(a + b/sqrt(a+2*y) )))/2;
191  // L1 L2 L5/L7 L3 | L4/L6
192  complex a2y = cadd(a,cmul(r2c(2),y));
193  complex L1 = cneg(cdiv(B,cmul(r2c(4),A)));
194  complex L2 = csqrt(a2y);
195  complex L3 = cneg(a2y);
196  complex L4 = cmul(r2c(2),cadd(a,cdiv(b,L2)));
197  complex L5 = csqrt(csub(L3,L4));
198  complex L6 = cmul(r2c(2),csub(a,cdiv(b,L2)));
199  complex L7 = csqrt(csub(L3,L6));
200  complex X1 = cadd(L1,cdiv(cadd( L2 , L5),r2c(2)));
201  complex X2 = cadd(L1,cdiv(cadd(cneg(L2), L7),r2c(2)));
202  complex X3 = cadd(L1,cdiv(cadd( L2 ,cneg(L5)),r2c(2)));
203  complex X4 = cadd(L1,cdiv(cadd(cneg(L2),cneg(L7)),r2c(2)));
204  int s=0;
205  if (isReal(X1)) {s++;setpos(s,X1,x1,x2,x3,x4);}
206  if (isReal(X2)) {s++;setpos(s,X2,x1,x2,x3,x4);}
207  if (isReal(X3)) {s++;setpos(s,X3,x1,x2,x3,x4);}
208  if (isReal(X4)) {s++;setpos(s,X4,x1,x2,x3,x4);}
209  return s;
210 }
211 #endif
d
complex csqrt(complex a)
double r
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
complex cexp(complex a)
double sqrt2(double x)
complex czero
complex csub(complex a, complex b)
complex cneg(complex a)
double cbrt(double x)
complex cadd(complex a, complex b)
void setpos(int p, complex a, double *x1, double *x2, double *x3, double *x4)
int solve_biquadratic(double a4, double a3, double a2, double a1, double a0, double *x1, double *x2, double *x3, double *x4)
complex cpow(complex a, int b)
int solve_cubic(double a, double b, double c, double d, double *x1, double *x2, double *x3)
complex cmul(complex a, complex b)
bool isEqualE(complex a, complex b)
complex cdiv(complex a, complex b)
bool isReal(complex x)
int solve_quadratic(double a, double b, double c, double *x1, double *x2)
complex r2c(double x)
double abs(quaternion a)
double epsilon
complex cln(complex a)


asr_flock_of_birds
Author(s): Bernhardt Andre, Engelmann Stephan, Giesler Björn, Heller Florian, Jäkel Rainer, Nguyen Trung, Pardowitz Michael, Weckesser Peter, Yi Xie, Zöllner Raoul
autogenerated on Mon Jun 10 2019 12:44:40