MIDAS  0.75
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
mathlib.cpp
Go to the documentation of this file.
1 #include "mathlib.h"
2 
3 #include "gelib.h"
4 
5 #include <stdio.h>
6 #include <math.h>
7 //#include <stdio.h>
8 #include <stdlib.h>
9 //#include <string.h>
10 //#include <math.h>
11 
12 
13 namespace midaspace {
14 
19 void shaker (long &n,long *a)
20 {
21  long i,j,k;
22 
23  j=0;
24  for (i=0;i<n;i++) {
25  if (a[i]==-1) continue;
26  a[j]=a[i];
27  if (i!=j) a[i]=-1;
28  for (k=i+1;k<n;k++)
29  if (a[j]==a[k]) a[k]=-1;
30 
31  j++;
32  }
33  n=j;
34 }
35 
36 
44 void sort_4 (long *x)
45 {
46  long a;
47 
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; }
51  else { 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; }
55 
56  if (x[0]>x[1]) { a=x[0]; x[0]=x[1]; x[1]=a; }
57 }
58 
59 
67 void sort_3 (long *x)
68 {
69  long a;
70 
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; } } }
76 }
77 
78 
86 void sort_2 (long *x)
87 {
88  if (x[0] < x[1])
89  return;
90  long a = x[0]; x[0] = x[1]; x[1] = a;
91 }
92 
93 
103 void solve_3 (const double *m,const double *r,double *l)
104 {
105  double detall,sd[6];
106 
107  // subdeterminants
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];
111 
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];
115 
116  // total determinant
117  detall = m[0] * sd[0] - m[1] * sd[1] + m[2] * sd[2] ;
118 
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 ;
122 }
123 
124 
134 void solve_3 (const double **m,const double *r,double *l)
135 {
136  double detall,sd[6];
137 
138  // subdeterminants
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];
142 
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] ;
146 
147  // total determinant
148  detall = m[0][0] * sd[0] - m[0][1] * sd[1] + m[0][2] * sd[2] ;
149 
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 ;
153 }
154 
155 
169 long solv_2nle (double zero,const double *a,const double *b,const double *c,const double *d,double *x,double *y)
170 {
171  long ii,jj,nr;
172 
173  if ( nillret(zero,a[0])==0.0 && nillret(zero,a[1])==0.0 )
174  return (solv_2le (zero,b,c,d,x[0],y[0]));
175 
176  if ( nillret(zero,a[0])==0.0 || nillret(zero,a[1])==0.0 ){
177  if (nillret(zero,a[1])) { ii=0; jj=1; }
178  else { ii=1; jj=0; }
179 
180  if (!nillret(zero,b[ii]))
181  if (!nillret(zero,c[ii]))
182  if (!nillret(zero,d[ii]))
183  return -1;
184  else
185  return 0;
186  else{
187  y[0] = -d[ii]/c[ii];
188  return solv_1le (zero, a[jj]*y[0]+b[jj] , c[jj]*y[0]+d[jj] ,x[0]);
189  }
190 
191  else
192  if (!nillret(zero,c[ii])){
193  x[0] = -d[ii]/b[ii];
194  return solv_1le (zero, a[jj]*x[0]+c[jj] , b[jj]*x[0]+d[jj] ,y[0]);
195  }
196  else{
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]);
198  if (nr>0)
199  y[0] = (-b[ii]*x[0]-d[ii]) / c[ii];
200  if (nr>1)
201  y[1] = (-b[ii]*x[1]-d[ii]) / c[ii];
202  return nr;
203  }
204 
205  }
206 
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]);
208 
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] ;
212  else
213  if (nillret(zero,b[0]*x[i]+d[0]))
215  //errol;
216  return -2;
217  else
218  return -1;
219 
220  return nr;
221 }
222 
236 long solv_2le (double zero,const double *a,const double *b,const double *c,double &x,double &y)
237 {
238  if (nillret(zero,a[0])){
239  // test of linear dependence
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] ))
242  return 0;
243  else
244  return -1;
245  }
246  }
247  else{
248  // test of linear dependence
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] ) ))
251  return 0;
252  else
253  if (nillret(zero,b[0])==0.0 && nillret(zero,b[1])==0.0 && nillret(zero,c[0])!=0.0)
254  return 0;
255  else
256  return -1;
257  }
258  }
259 
260  if (nillret(zero,a[0])){
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];
263  }
264  else{
265  y = -c[0]/b[0];
266  x = (-c[1]-b[1]*y) / a[1];
267  }
268 
269  return 1;
270 }
271 
284 long solv_1le (double zero,double a,double b,double &x)
285 {
286  if (!nillret(zero,a)) {
287  if (!nillret(zero,b))
288  return -1;
289  else
290  return 0;
291  }
292  x = -b/a;
293 
294  return 1;
295 }
296 
297 void nilling (double zero, double &a)
298 {
299  if (-zero<a && a<zero) a = 0.0;
300 }
301 
302 double nillret (double zero, double a)
303 {
304  if (-zero<a && a<zero) return 0.0;
305  else return a;
306 }
307 
308 bool isZero (double zero, double a)
309 {
310  if (a == 0.0) return true;
311  if (-zero<a && a<zero) return true;
312  else return false;
313 }
314 bool isZero (double abszero, double relzero, double a)
315 {
316  return isZero (relzero > abszero ? relzero : abszero, a);
317 }
318 
331 long solv_polynom_2 (double zero,double a,double b,double c,double &r1,double &r2)
332 {
333  if (!nillret(zero,a)) {
334  if (!nillret(zero,b))
335  if (!nillret(zero,c))
336  return -1;
337  else
338  return 0;
339  else {
340  r1 = -c/b;
341  return 1;
342  }
343  }
344 
345  double D = nillret ( zero*zero , b*b - 4*a*c );
346 
347  if (D < 0.0)
348  return 0;
349 
350  if (D == 0.0){
351  r1 = -b/2.0/a;
352  return 1;
353  }
354 
355  r1 = (-b + sqrt(D))/2.0/a;
356  r2 = (-b - sqrt(D))/2.0/a;
357  return 2;
358 }
359 
360 long div_dd (double d1, double d2, const char* s1, const char* s2, const long line)
361 {
362  long answer = lround (d1/d2);
363  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 (); }
364  return answer;
365 }
366 
367 long div_dd (long &answer, double d1, double d2)
368 {
369  answer = lround (d1/d2);
370  if ( fabs(answer-d1/d2) > 0.00001 ) return 1;
371  return 0;
372 }
373 
374 long div_dd (int &answer, double d1, double d2)
375 {
376  long aux;
377  int ret = div_dd (aux, d1, d2);
378  answer = (int) aux;
379 
380  return ret;
381 }
382 
383 
384 int decomp_int (int answer[], int n, int l, int rad)
385 {
386  for (int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
387  return l;
388 }
389 long decomp_int (int answer[], int n, long l, long rad)
390 {
391  for (int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
392  return l;
393 }
394 long long decomp_int (int answer[], int n, long long l, long long rad)
395 {
396  for (int i=n; i>0; i--) { answer[i-1] = l%rad; l = l/rad; }
397  return l;
398 }
399 
400 
401 bool isNonZero (double x, double tolerance)
402 {
403  if ( fabs(x) > tolerance ) return 1;
404  else return 0;
405 }
406 
407 
408 } // namespace midaspace
void shaker(long &n, long *a)
zlikviduje vicenasobne cifry pro long in <0;..>
Definition: mathlib.cpp:19
void sort_3(long *x)
Function sorts first three members of array with ascending order.
Definition: mathlib.cpp:67
General functions.
void sort_4(long *x)
Function sorts first four members of array with ascending order.
Definition: mathlib.cpp:44
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 + ...
Definition: mathlib.cpp:169
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...
Definition: mathlib.cpp:284
bool isZero(double zero, double a)
Definition: mathlib.cpp:308
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...
Definition: mathlib.cpp:236
void solve_3(const double *m, const double *r, double *l)
Function solves the system of linear equations.
Definition: mathlib.cpp:103
double nillret(double zero, double a)
Definition: mathlib.cpp:302
bool isNonZero(double x, double tolerance)
Definition: mathlib.cpp:401
long div_dd(double d1, double d2, const char *s1, const char *s2, const long line)
Definition: mathlib.cpp:360
Mathematic functions.
void sort_2(long *x)
Function sorts first two members of array with ascending order.
Definition: mathlib.cpp:86
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 = ...
Definition: mathlib.cpp:331
void nilling(double zero, double &a)
Definition: mathlib.cpp:297
int decomp_int(int answer[], int n, int l, int rad)
Definition: mathlib.cpp:384