Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <gsl/gsl_poly.h>
  7.  
  8. unsigned long long cur_root;
  9. unsigned long long capacity;
  10.  
  11. void polynomial_recursive(double **roots, double min_x, double max_x, int min_degree, int max_degree, int cur_degree, double *coeff)
  12. {
  13.     for (double c = cur_degree==max_degree?1:min_x; c <= max_x; c++)
  14.     {
  15.             double *new_coeff;
  16.  
  17.             new_coeff = (double *) malloc (sizeof (double) * (cur_degree + 1));
  18.             if (new_coeff == NULL)
  19.             {
  20.                     perror ("malloc failed");
  21.             }
  22.             memcpy (new_coeff, coeff, cur_degree * sizeof (double));
  23.             new_coeff[cur_degree] = c;
  24.  
  25.             if ( cur_degree == max_degree )
  26.             {
  27.                     double z[cur_degree*2];
  28.  
  29.                     gsl_poly_complex_workspace *w = gsl_poly_complex_workspace_alloc (cur_degree + 1);
  30.                     gsl_poly_complex_solve (new_coeff, cur_degree + 1, w, z);
  31.                     gsl_poly_complex_workspace_free (w);
  32.  
  33.                     for (int i = 0; i < cur_degree; i++)
  34.                     {
  35.                             if (cur_root + 2 >= capacity)
  36.                             {
  37.                                     capacity *= 2;
  38.                                     double *tmp = (double *) realloc (*roots, capacity * sizeof (double));
  39.                                     if (tmp == NULL)
  40.                                     {
  41.                                             perror ("realloc failed");
  42.                                     }
  43.                                     else
  44.                                     {
  45.                                             *roots = tmp;
  46.                                     }
  47.                             }
  48.                             (*roots) [cur_root++] = z[2*i];
  49.                             (*roots) [cur_root++] = z[2*i+1];
  50.                     }
  51.             } else {
  52.                     #pragma omp task shared(new_coeff)
  53.                     polynomial_recursive (roots, min_x, max_x, min_degree, max_degree, cur_degree + 1, new_coeff);
  54.                     #pragma omp taskwait
  55.             }
  56.             free (new_coeff);
  57.     }
  58. }
  59.  
  60. unsigned long long getDensityForPoint(double x, double y, double *points, int size, double radius)
  61. {
  62.     unsigned long long count = 0;
  63.     for (int i = 0; i < size; i += 2)
  64.       {
  65.         double x2 = points[i];
  66.         double y2 = points[i+1];
  67.         if (x != x2 || y != y2)
  68.           {
  69.             double dist = fabs(x2 - x) + fabs(y2 - y);;
  70.             if (dist <= radius)
  71.               {
  72.                 count++;
  73.               }
  74.           }
  75.       }
  76.     return count;
  77. }
  78.  
  79. double
  80. calculateDensityScalarFromDensity(unsigned long min, unsigned long max, unsigned long density)
  81. {
  82.     return 0.0 + ((1.5 - 0.0) / (double)(max - min)) * (density - min);
  83. }
  84.  
  85. int
  86. main(void)
  87. {
  88.     double *roots;
  89.  
  90.     capacity = 100;
  91.     cur_root = 0;
  92.     roots = (double *) malloc (capacity * sizeof (double));
  93.     if (roots == NULL)
  94.       {
  95.         perror ("malloc failed");
  96.       }
  97.  
  98.     #pragma omp parallel
  99.     {
  100.         #pragma omp single
  101.         {
  102.             polynomial_recursive (&roots, -4, 4, 2, 5, 0, NULL);
  103.         }
  104.     }
  105.  
  106.     printf("%llu\n", cur_root);
  107.  
  108.     int w=1920,h=1080;
  109.  
  110.     uint64_t *buckets = (uint64_t*)calloc(w*h,sizeof(buckets));
  111.     double min_x,max_x,min_y,max_y;
  112.     min_x = max_x = roots[0];
  113.     min_y = max_y = roots[1];
  114.  
  115.     for ( int i = 0; i < cur_root; i+=2 )
  116.     {
  117.             min_x = roots[i]<min_x?roots[i]:min_x;
  118.             max_x = roots[i]>max_x?roots[i]:max_x;
  119.             min_y = roots[i+1]<min_y?roots[i+1]:min_y;
  120.             max_y = roots[i+1]>max_y?roots[i+1]:max_y;
  121.     }
  122.  
  123.     printf("range: (%f,%f)-(%f,%f)\n",min_x,min_y,max_x,max_y);
  124.  
  125.     double pad_x = (max_x-min_x)/100;
  126.     double pad_y = (max_y-min_y)/100;
  127.     min_x -= pad_x;
  128.     max_x += pad_x;
  129.     min_y -= pad_y;
  130.     max_y += pad_y;
  131.  
  132.     for ( int i = 0; i < cur_root; i+=2 )
  133.     {
  134.             int x = (roots[i]-min_x)/(max_x-min_x)*w;
  135.             int y = (roots[i+1]-min_y)/(max_y-min_y)*h;
  136.             buckets[x+w*y]++;
  137.     }
  138.  
  139.     uint64_t v_min,v_max;
  140.     v_max = v_min = buckets[0];
  141.     for ( int i = 0; i < w*h; i++ )
  142.     {
  143.             v_min = buckets[i]<v_min?buckets[i]:v_min;
  144.             v_max = buckets[i]>v_max?buckets[i]:v_max;
  145.     }
  146.  
  147.     printf("max %llu, min %llu\n", v_max, v_min);
  148.     FILE *gnuplotPipe = popen ("gnuplot -persistent", "w");
  149.     FILE *temp = fopen("data.temp", "w");
  150.  
  151.     for ( int i = 0; i < w*h; i++ )
  152.     {
  153.             uint64_t val = buckets[i];
  154.             if ( !val ) continue;
  155.             int x = i%w;
  156.             int y = i/w;
  157.             double fx = min_x + (max_x-min_x) * x / w;
  158.             double fy = min_y + (max_y-min_y) * y / h;
  159.             fprintf (temp, "%lf %lf %lf\n", fx, fy, calculateDensityScalarFromDensity(v_min, v_max, val));
  160.     }
  161.  
  162.     free(buckets);
  163.     free(roots);
  164.  
  165.     fprintf (gnuplotPipe, "set zrange [0.0:1.5]\nset cbrange [0.0:1.5]\nset term pngcairo\nset terminal png size %d,%d\nset output \"plot.png\"\nset palette defined (0 \"black\", 0.5 \"red\", 1 \"yellow\", 1.5 \"white\")\nplot 'data.temp' with points pointtype 5 pointsize 0.001 palette\n",w,h);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement