1 | /****************************************************************************/ |
---|
2 | /* MATHFNS.C: utility routines for various sorts of math operations. Most */ |
---|
3 | /* these functions work with real values, meaning that they can handle */ |
---|
4 | /* either floats or doubles, depending on compiler switches. */ |
---|
5 | /* Copyright (c) 2001 by Joshua E. Barnes, Honolulu, Hawai`i. */ |
---|
6 | /****************************************************************************/ |
---|
7 | |
---|
8 | #include "stdinc.h" |
---|
9 | #include "mathfns.h" |
---|
10 | |
---|
11 | #if !defined(LINUX) |
---|
12 | long random(void); |
---|
13 | #endif |
---|
14 | |
---|
15 | /* |
---|
16 | * RSQR, RQBE: compute x*x and x*x*x. |
---|
17 | */ |
---|
18 | |
---|
19 | real rsqr(real x) |
---|
20 | { |
---|
21 | return (x * x); |
---|
22 | } |
---|
23 | |
---|
24 | real rqbe(real x) |
---|
25 | { |
---|
26 | return (x * x * x); |
---|
27 | } |
---|
28 | |
---|
29 | /* |
---|
30 | * RLOG2, REXP2: log, inverse log to base two. |
---|
31 | */ |
---|
32 | |
---|
33 | real rlog2(real x) |
---|
34 | { |
---|
35 | return (rlog(x) / M_LN2); |
---|
36 | } |
---|
37 | |
---|
38 | real rexp2(real x) |
---|
39 | { |
---|
40 | return (rexp(M_LN2 * x)); |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * RDEX: inverse log base ten. |
---|
45 | */ |
---|
46 | |
---|
47 | real rdex(real x) |
---|
48 | { |
---|
49 | return (rexp(M_LN10 * x)); |
---|
50 | } |
---|
51 | |
---|
52 | #if defined(SINGLEPREC) |
---|
53 | |
---|
54 | /* |
---|
55 | * FCBRT: floating cube root. |
---|
56 | */ |
---|
57 | |
---|
58 | float fcbrt(float x) |
---|
59 | { |
---|
60 | return ((float) cbrt((double) x)); |
---|
61 | } |
---|
62 | |
---|
63 | #endif |
---|
64 | |
---|
65 | /* |
---|
66 | * XRANDOM: floating-point random number routine. |
---|
67 | */ |
---|
68 | |
---|
69 | double xrandom(double xl, double xh) |
---|
70 | { |
---|
71 | |
---|
72 | return (xl + (xh - xl) * ((double) random()) / 2147483647.0); |
---|
73 | } |
---|
74 | |
---|
75 | /* |
---|
76 | * GRANDOM: normally distributed random number (polar method). |
---|
77 | * Reference: Knuth, vol. 2, p. 104. |
---|
78 | */ |
---|
79 | |
---|
80 | double grandom(double mean, double sdev) |
---|
81 | { |
---|
82 | double v1, v2, s; |
---|
83 | |
---|
84 | do { |
---|
85 | v1 = xrandom(-1.0, 1.0); |
---|
86 | v2 = xrandom(-1.0, 1.0); |
---|
87 | s = v1*v1 + v2*v2; |
---|
88 | } while (s >= 1.0); |
---|
89 | return (mean + sdev * v1 * sqrt(-2.0 * log(s) / s)); |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | * PICKSHELL: pick point on shell. |
---|
94 | */ |
---|
95 | |
---|
96 | void pickshell(real vec[], int ndim, real rad) |
---|
97 | { |
---|
98 | real rsq, rscale; |
---|
99 | int i; |
---|
100 | |
---|
101 | do { |
---|
102 | rsq = 0.0; |
---|
103 | for (i = 0; i < ndim; i++) { |
---|
104 | vec[i] = xrandom(-1.0, 1.0); |
---|
105 | rsq = rsq + vec[i] * vec[i]; |
---|
106 | } |
---|
107 | } while (rsq > 1.0); |
---|
108 | rscale = rad / rsqrt(rsq); |
---|
109 | for (i = 0; i < ndim; i++) |
---|
110 | vec[i] = vec[i] * rscale; |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | * PICKBALL: pick point within ball. |
---|
115 | */ |
---|
116 | |
---|
117 | void pickball(real vec[], int ndim, real rad) |
---|
118 | { |
---|
119 | real rsq; |
---|
120 | int i; |
---|
121 | |
---|
122 | do { |
---|
123 | rsq = 0.0; |
---|
124 | for (i = 0; i < ndim; i++) { |
---|
125 | vec[i] = xrandom(-1.0, 1.0); |
---|
126 | rsq = rsq + vec[i] * vec[i]; |
---|
127 | } |
---|
128 | } while (rsq > 1.0); |
---|
129 | for (i = 0; i < ndim; i++) |
---|
130 | vec[i] = vec[i] * rad; |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * PICKBOX: pick point within box. |
---|
135 | */ |
---|
136 | |
---|
137 | void pickbox(real vec[], int ndim, real size) |
---|
138 | { |
---|
139 | int i; |
---|
140 | |
---|
141 | for (i = 0; i < ndim; i++) |
---|
142 | vec[i] = xrandom(- size, size); |
---|
143 | } |
---|