GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sphvol.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3 * MODULE: R-Tree library
4 *
5 * AUTHOR(S): Antonin Guttman - original code
6 * Daniel Green (green@superliminal.com) - major clean-up
7 * and implementation of bounding spheres
8 *
9 * PURPOSE: Multidimensional index
10 *
11 * COPYRIGHT: (C) 2001 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 
18 /*
19  * SPHERE VOLUME
20  * by Daniel Green
21  * dgreen@superliminal.com
22  *
23  * Calculates and prints the volumes of the unit hyperspheres for
24  * dimensions zero through the given value, or 9 by default.
25  * Prints in the form of a C array of double called sphere_volumes.
26  *
27  * From formule in "Regular Polytopes" by H.S.M Coxeter, the volume
28  * of a hypersphere of dimension d is:
29  * Pi^(d/2) / gamma(d/2 + 1)
30  *
31  * This implementation works by first computing the log of the above
32  * function and then returning the exp of that value in order to avoid
33  * instabilities due to the huge values that the real gamma function
34  * would return.
35  *
36  * Multiply the output volumes by R^n to get the volume of an n
37  * dimensional sphere of radius R.
38  */
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <math.h>
43 #include <grass/gis.h>
44 
45 static void print_volume(int dimension, double volume)
46 {
47  fprintf(stdout, "\t%.6f, /* dimension %3d */\n", volume, dimension);
48 }
49 
50 static double sphere_volume(double dimension)
51 {
52  double log_gamma, log_volume;
53 
54  log_gamma = gamma(dimension / 2.0 + 1);
55  log_volume = dimension / 2.0 * log(M_PI) - log_gamma;
56  return exp(log_volume);
57 }
58 
59 extern int main(int argc, char *argv[])
60 {
61  int dim, max_dims = 9;
62 
63  if (2 == argc)
64  max_dims = atoi(argv[1]);
65 
66  fprintf(stdout, "static const double sphere_volumes[] = {\n");
67  for (dim = 0; dim < max_dims + 1; dim++)
68  print_volume(dim, sphere_volume(dim));
69  fprintf(stdout, "};\n");
70  return 0;
71 }
double sphere_volume(double dimension)
Definition: gammavol.c:27
log
Definition: wxnviz.py:54
int main(int argc, char *argv[])
Definition: gem/main.c:302