25 if (a[i]==-1)
continue;
29 if (a[j]==a[k]) a[k]=-1;
48 if (x[0] < x[1])
if (x[1] > x[2])
if (x[1] > x[3]) { a=x[1]; x[1]=x[3]; x[3]=a;
sort_3(x);
return; }
49 else { a=x[1]; x[1]=x[2]; x[2]=a; }
50 else if (x[2] > x[3]) { a=x[2]; x[2]=x[3]; x[3]=a;
sort_3(x);
return; }
52 else if (x[0] > x[2])
if (x[0] > x[3]) { a=x[0]; x[0]=x[3]; x[3]=a;
sort_3(x);
return; }
53 else { a=x[0]; x[0]=x[2]; x[2]=a; }
54 else if (x[2] > x[3]) { a=x[2]; x[2]=x[3]; x[3]=a;
sort_3(x);
return; }
56 if (x[0]>x[1]) { a=x[0]; x[0]=x[1]; x[1]=a; }
71 if (x[0] > x[1]) {
if (x[0] > x[2])
if (x[1] > x[2]) { a=x[0]; x[0]=x[2]; x[2]=a; }
72 else { a=x[0]; x[0]=x[1]; x[1]=x[2]; x[2]=a; }
73 else { a=x[0]; x[0]=x[1]; x[1]=a; } }
74 else {
if (x[1] > x[2]) {
if (x[0] > x[2]) { a=x[0]; x[0]=x[2]; x[2]=x[1]; x[1]=a; }
75 else { a=x[1]; x[1]=x[2]; x[2]=a; } } }
90 long a = x[0]; x[0] = x[1]; x[1] = a;
103 void solve_3 (
const double *m,
const double *r,
double *l)
108 sd[0] = m[4]*m[8] - m[7]*m[5];
109 sd[1] = m[3]*m[8] - m[6]*m[5];
110 sd[2] = m[3]*m[7] - m[6]*m[4];
112 sd[3] = m[4]*r[2] - m[7]*r[1];
113 sd[4] = r[1]*m[8] - r[2]*m[5];
114 sd[5] = m[3]*r[2] - m[6]*r[1];
117 detall = m[0] * sd[0] - m[1] * sd[1] + m[2] * sd[2] ;
119 l[0] = ( r[0] * sd[0] - m[1] * sd[4] - m[2] * sd[3] ) / detall ;
120 l[1] = ( m[0] * sd[4] - r[0] * sd[1] + m[2] * sd[5] ) / detall ;
121 l[2] = ( m[0] * sd[3] - m[1] * sd[5] + r[0] * sd[2] ) / detall ;
134 void solve_3 (
const double **m,
const double *r,
double *l)
139 sd[0] = m[1][1]*m[2][2] - m[2][1]*m[1][2];
140 sd[1] = m[1][0]*m[2][2] - m[2][0]*m[1][2];
141 sd[2] = m[1][0]*m[2][1] - m[2][0]*m[1][1];
143 sd[3] = m[1][1]*r[2] - m[2][1]*r[1] ;
144 sd[4] = r[1] *m[2][2] - r[2] *m[1][2];
145 sd[5] = m[1][0]*r[2] - m[2][0]*r[1] ;
148 detall = m[0][0] * sd[0] - m[0][1] * sd[1] + m[0][2] * sd[2] ;
150 l[0] = ( r[0] * sd[0] - m[0][1] * sd[4] - m[0][2] * sd[3] ) / detall ;
151 l[1] = ( m[0][0] * sd[4] - r[0] * sd[1] + m[0][2] * sd[5] ) / detall ;
152 l[2] = ( m[0][0] * sd[3] - m[0][1] * sd[5] + r[0] * sd[2] ) / detall ;
169 long solv_2nle (
double zero,
const double *a,
const double *b,
const double *c,
const double *d,
double *x,
double *y)
174 return (
solv_2le (zero,b,c,d,x[0],y[0]));
177 if (
nillret(zero,a[1])) { ii=0; jj=1; }
188 return solv_1le (zero, a[jj]*y[0]+b[jj] , c[jj]*y[0]+d[jj] ,x[0]);
194 return solv_1le (zero, a[jj]*x[0]+c[jj] , b[jj]*x[0]+d[jj] ,y[0]);
197 nr =
solv_polynom_2 ( zero*zero , -a[jj]*b[ii] , -a[jj]*d[ii] + c[ii]*b[jj]-c[jj]*b[ii] , c[ii]*d[jj]-c[jj]*d[ii] ,x[0],x[1]);
199 y[0] = (-b[ii]*x[0]-d[ii]) / c[ii];
201 y[1] = (-b[ii]*x[1]-d[ii]) / c[ii];
207 nr =
solv_polynom_2 ( zero*zero , a[0]*b[1]-a[1]*b[0] , a[0]*d[1]-a[1]*d[0] + c[0]*b[1]-c[1]*b[0] , c[0]*d[1]-c[1]*d[0] ,x[0],x[1]);
209 for (
long i=0;i<nr;i++)
210 if ((y[i] =
nillret(zero,a[0]*x[i]+c[0])))
211 y[i] = (-b[0]*x[i]-d[0]) / y[i] ;
213 if (
nillret(zero,b[0]*x[i]+d[0]))
236 long solv_2le (
double zero,
const double *a,
const double *b,
const double *c,
double &x,
double &y)
240 if (!
nillret ( zero*zero , a[1]*(a[0]+b[0])-a[0]*(a[1]+b[1]) )) {
241 if (
nillret ( zero*zero , a[1]*c[0]-a[0]*c[1] ))
249 if (!
nillret ( zero*zero , b[1]*(a[0]+b[0])-b[0]*(a[1]+b[1]) )) {
250 if (
nillret ( zero*zero , b[1]*c[0]-b[0]*c[1] ) || (
nillret ( zero*zero , b[1]*c[0]-b[0]*c[1] ) ))
261 y = (c[0]*a[1]-c[1]*a[0]) / (a[0]*b[1]-a[1]*b[0]);
262 x = (-c[0]-b[0]*y) / a[0];
266 x = (-c[1]-b[1]*y) / a[1];
284 long solv_1le (
double zero,
double a,
double b,
double &x)
299 if (-zero<a && a<zero) a = 0.0;
304 if (-zero<a && a<zero)
return 0.0;
310 if (a == 0.0)
return true;
311 if (-zero<a && a<zero)
return true;
314 bool isZero (
double abszero,
double relzero,
double a)
316 return isZero (relzero > abszero ? relzero : abszero, a);
319 bool areSame (
double abszero,
double relzero,
double a,
double b)
321 return isZero (abszero, fabs(relzero*a), a-b);
335 long solv_polynom_2 (
double zero,
double a,
double b,
double c,
double &r1,
double &r2)
349 double D =
nillret ( zero*zero , b*b - 4*a*c );
359 r1 = (-b + sqrt(D))/2.0/a;
360 r2 = (-b - sqrt(D))/2.0/a;
364 long div_dd (
double d1,
double d2,
const char* s1,
const char* s2,
const long line)
366 long answer = lround (d1/d2);
367 if ( fabs(answer-d1/d2) > 0.00001 ) { fprintf (stderr,
"%s = %f is not dividable by %s = %f (%f), line %ld\n",s1,d1,s2,d2,d1/d2,line); abort (); }
371 long div_dd (
long &answer,
double d1,
double d2)
373 answer = lround (d1/d2);
374 if ( fabs(answer-d1/d2) > 0.00001 )
return 1;
378 long div_dd (
int &answer,
double d1,
double d2)
381 int ret =
div_dd (aux, d1, d2);
390 for (
int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
395 for (
int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
398 long long decomp_int (
int answer[],
int n,
long long l,
long long rad)
400 for (
int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
407 if ( fabs(x) > tolerance )
return 1;
bool isNonZero(double x, double tolerance)
long solv_1le(double zero, double a, double b, double &x)
function solves linear equation: a*x + b = 0 answer: -1 = infinite number of results 0...
void solve_3(const double *m, const double *r, double *l)
Function solves the system of linear equations.
long solv_2nle(double zero, const double *a, const double *b, const double *c, const double *d, double *x, double *y)
function solves system of two non-linear equations: a[0]*x*y + b[0]*x + c[0]*y + d[0] = 0 a[1]*x*y + ...
double nillret(double zero, double a)
void shaker(long &n, long *a)
zlikviduje vicenasobne cifry pro long in <0;..>
long div_dd(double d1, double d2, const char *s1, const char *s2, const long line)
void sort_2(long *x)
Function sorts first two members of array with ascending order.
long solv_2le(double zero, const double *a, const double *b, const double *c, double &x, double &y)
function solves system of two linear equations: a[0]*x + b[0]*y + c[0] = 0 a[1]*x + b[1]*y + c[1] = 0...
int decomp_int(int answer[], int n, int l, int rad)
long solv_polynom_2(double zero, double a, double b, double c, double &r1, double &r2)
function searchs roots of polynom of 2nd order = quadratic equation a*x^2 + b*x + c = 0 answer: -1 = ...
void sort_4(long *x)
Function sorts first four members of array with ascending order.
void nilling(double zero, double &a)
bool areSame(double abszero, double relzero, double a, double b)
bool isZero(double zero, double a)
void sort_3(long *x)
Function sorts first three members of array with ascending order.