gammavol.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. * Markus Metz - R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *****************************************************************************/
  17. #include <stdio.h>
  18. #include <math.h>
  19. #ifndef ABS
  20. # define ABS(a) ((a) > 0 ? (a) : -(a))
  21. #endif
  22. #define EP .0000000001
  23. double sphere_volume(double dimension)
  24. {
  25. double log_gamma, log_volume;
  26. log_gamma = lgamma(dimension / 2.0 + 1);
  27. log_volume = dimension / 2.0 * log(M_PI) - log_gamma;
  28. return exp(log_volume);
  29. }
  30. int main()
  31. {
  32. double dim = 0, delta = 1;
  33. while (ABS(delta) > EP)
  34. if (sphere_volume(dim + delta) > sphere_volume(dim))
  35. dim += delta;
  36. else
  37. delta /= -2;
  38. fprintf(stdout, "max volume = %.10f at dimension %.10f\n",
  39. sphere_volume(dim), dim);
  40. return 0;
  41. }