cgv
Loading...
Searching...
No Matches
functions.cxx
1#include "constants.h"
2#include "functions.h"
3
4namespace cgv{
5 namespace math {
6
8double compute_unit_ball_volume(unsigned n)
9{
10 static std::vector<double> cache;
11 if (cache.empty()) {
12 cache.reserve(10);
13 cache.push_back(2.0);
14 cache.push_back(PI);
15 }
16 if (n == 0)
17 return 0;
18 while (n > cache.size())
19 cache.push_back(*(cache.end()-2) * TWO_PI/cache.size());
20 return cache[n-1];
21}
23double compute_ball_volume(unsigned n, double R)
24{
25 return compute_unit_ball_volume(n)*pow(R,(double)n);
26}
28double compute_unit_sphere_area(unsigned n)
29{
30 if (n < 2)
31 return 0;
32 return TWO_PI*compute_unit_ball_volume(n-1);
33}
35double compute_sphere_area(unsigned n, double R)
36{
37 return compute_unit_sphere_area(n)*pow(R,(double)(n-1));
38}
39
40
41
42//ln(gamma(x))
43double gamma_ln(const double xx)
44{
45 int j;
46 double x,tmp,y,ser;
47 static const double cof[14]={57.1562356658629235,-59.5979603554754912,
48 14.1360979747417471,-0.491913816097620199,.339946499848118887e-4,
49 .465236289270485756e-4,-.983744753048795646e-4,.158088703224912494e-3,
50 -.210264441724104883e-3,.217439618115212643e-3,-.164318106536763890e-3,
51 .844182239838527433e-4,-.261908384015814087e-4,.368991826595316234e-5};
52 assert(xx >0);
53 y=x=xx;
54 tmp = x+5.24218750000000000;
55 tmp = (x+0.5)*log(tmp)-tmp;
56 ser = 0.999999999999997092;
57 for (j=0;j<14;j++) ser += cof[j]/++y;
58 return tmp+log(2.5066282746310005*ser/x);
59}
60
61
62
63//factorial of n (n!)
64double fac(const int n)
65{
66 double a;
67 assert(n >=0 && n <171);
68
69 a=1.;
70 for(int i = 1; i <= n;i++)
71 a=(double)i * a;
72
73 return a;
74}
75
76//returns ln(n!)
77double fac_ln(const int n)
78{
79 assert(n >=0 && n <171);
80
81 double a=1.;
82 for(int i = 1; i <=n;i++)
83 a=i*a;
84
85 return a;
86}
87
88//beta function
89double beta(const double z, const double w)
90{
91 return std::exp(gamma_ln(z)+gamma_ln(w)-gamma_ln(z+w));
92}
93
94
95//binomial coefficient n over k
96double nchoosek(const int n, const int k)
97{
98 assert (n>=0 && k>=0 && k<=n);
99 if (n<171)
100 return ::floor(0.5+fac(n)/(fac(k)*fac(n-k)));
101 return ::floor(0.5+exp(fac_ln(n)-fac_ln(k)-fac_ln(n-k)));
102}
103
104 }
105}
the cgv namespace
Definition print.h:11