math_utils_test.c
Go to the documentation of this file.
1 #include "csm_all.h"
2 #include <time.h>
3 
4 #include "math_utils.h"
5 
6 
7 inline float myasin(float x) {
8  const float threshold = .52;
9  if(x > threshold) {
10  static float z, der0, der1,der2,der3;
11  static int first = 1;
12  if(first) {
13  z = 0.5* (threshold + 0.707);
14  der0 = asin(z);
15  float M = 1-z*z;
16  der1 = 1 / sqrt(M);
17  der2 = (sqrt(M) * z) / (M*M);
18  der3 = (sqrt(M) / ( M * M ) + (3 * (z*z) * sqrt(M) ) / (M * M * M) ) ;
19  first = 0;
20  printf("%f %f %f %f \n", der0, der1, der2, der3);
21  }
22  float m = x-z;
23  return der0 + m * ( der1 + m * ( (der2/2) + m * (der3/6) ) );
24  }
25  else{
26  float x2 = x * x;
27  float der1 = 1;
28  float der3 = 1.0f/6;
29  float der5 = 3.0f/40;
30  float der7 = 15.0f/ (48*7);
31  return x * ( der1 + x2 *( der3 + x2 * (der5 + der7 * x2 )));
32  }
33 }
34 
35 #define myacos(x) ( ((float) (M_PI/2) ) - myasin(x))
36 inline void polar(float x, float y, double* restrict rho, double* restrict theta) {
37  *rho = sqrtf(x*x+y*y);
38  if(x > 0) {
39  if(y > 0) {
40  if(x < y)
41  *theta = myacos(x / *rho);
42  else
43  *theta = myasin(y / *rho);
44  } else {
45  if( x < -y )
46  *theta = - myacos(x / *rho);
47  else
48  *theta = - myasin( (-y) / *rho) ;
49  }
50  } else {
51  if(y > 0) {
52  if( (-x) < y)
53  *theta = ((float)M_PI) - myacos(-x / *rho);
54  else
55  *theta = ((float)M_PI) - myasin( y / *rho);
56  } else {
57  if ( (-x) < (-y) )
58  *theta = ((float)M_PI) + myacos(-x / *rho);
59  else
60  *theta = ((float)M_PI) + myasin(-y / *rho);
61  }
62  }
63 }
64 
65 inline void polar2(double x, double y, double* restrict rho, double* restrict theta) {
66  *rho = sqrt(x*x+y*y);
67  *theta = atan2(y,x);
68 }
69 
70 int main() {
71  double a[2] = {0, 0};
72  double b[2] = {5, 5};
73  double x[5][2] = {
74  {0, -10},
75  { 2, 1},
76  {100, 1},
77  {0,0},{5,5}};
78 
79  gsl_vector * A = vector_from_array(2,a);
80  gsl_vector * B = vector_from_array(2,b);
81 
82  gsl_vector * res = gsl_vector_alloc(2);
83 
84  int i;
85  #if 0
86  for(i=0;i<5;i++){
87  gsl_vector * X = vector_from_array(2,x[i]);
88 
89 /* projection_on_segment(A,B,X,res);*/
90 
91  printf("Projection of %f %f is %f %f \n", gvg(X,0),gvg(X,1),
92  gvg(res,0),gvg(res,1));
93  }
94  #endif
95 
96  int errors = 0;
97  double should_be_nan[2] = { 0.0 / 0.0, GSL_NAN };
98  for(i=0;i<2;i++) {
99  if(!isnan(should_be_nan[i])) {
100  printf("#%d: isnan(%f) failed \n", i, should_be_nan[i]);
101  errors++;
102  }
103  if(!is_nan(should_be_nan[i])) {
104  printf("#%d: is_nan(%f) failed \n", i, should_be_nan[i]);
105  errors++;
106  }
107  }
108 
109  double max_error = 0;
110  int num = 10000;
111  for(i=0;i<num;i++) {
112  double theta = (i * 2 * M_PI) / (num-1);
113  double x = cos(theta), y = sin(theta);
114 
115  double theta_est, rho;
116  polar(x,y,&rho,&theta_est);
117  double error = fabs( angleDiff(theta_est,theta) );
118  if(error > max_error)
119  printf("%d x %f y %f theta %f theta_est %f error %f \n", i, x, y, theta, theta_est, error);
120  max_error = GSL_MAX(error, max_error);
121  }
122 
123  int NUM2 = 1000; int k;
124  float c[num], s[num];
125  for(i=0;i<num;i++) {
126  double theta = (i * 2 * M_PI) / (num-1);
127  c[i]= cos(theta); s[i] = sin(theta);
128  }
129 
130  clock_t start1 = clock();
131  double thetaf,rhof;
132  for(k=0;k<NUM2;k++)
133  for(i=0;i<num;i++) {
134  polar(c[i],s[i],&rhof, &thetaf);
135  }
136  clock_t end1 = clock();
137 
138  clock_t start2 = clock();
139  double rho,theta;
140  for(k=0;k<NUM2;k++)
141  for(i=0;i<num;i++) {
142  polar2(c[i],s[i],&rho, &theta);
143  }
144  clock_t end2 = clock();
145 
146  float seconds1 = (end1-start1)/((float)CLOCKS_PER_SEC);
147  float seconds2 = (end2-start2)/((float)CLOCKS_PER_SEC);
148 
149  printf(" polar: %f polar2: %f\n", seconds1, seconds2);
150 
151  printf("Maximum error for polar(): %lf rad = %lf deg\n", max_error, rad2deg(max_error));
152 /* printf("Acos() called for %f %f\n", min_acos, max_acos);
153  printf("Asin() called for %f %f\n", min_asin, max_asin);
154 */ return errors;
155 }
gsl_vector * vector_from_array(unsigned int n, double *x)
int main()
#define gvg
Definition: math_utils_gsl.h:9
#define myacos(x)
double angleDiff(double a, double b)
Definition: math_utils.c:128
float myasin(float x)
#define M_PI
Definition: math_utils.h:7
#define m(v1, v2)
Definition: egsl_macros.h:13
#define restrict
Definition: restrict.h:17
#define M(matrix, rows, col)
Definition: gpc.c:25
int is_nan(double v)
Definition: math_utils.c:60
void polar(float x, float y, double *restrict rho, double *restrict theta)
void polar2(double x, double y, double *restrict rho, double *restrict theta)
double rad2deg(double rad)
Definition: math_utils.c:79


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23