void binning::init_limits( std::size_t n_bins, double d_max, double d_min, double relative_tolerance) { CCTBX_ASSERT(n_bins > 0); CCTBX_ASSERT(d_max >= 0); CCTBX_ASSERT(d_min > 0); CCTBX_ASSERT(d_max == 0 || d_min < d_max); double d_star_sq_min = 0; if (d_max) d_star_sq_min = 1 / (d_max * d_max); double d_star_sq_max = 1 / (d_min * d_min); double span = d_star_sq_max - d_star_sq_min; d_star_sq_max += span * relative_tolerance; d_star_sq_min -= span * relative_tolerance; if (d_star_sq_min < 0) d_star_sq_min = 0; double r_low = std::sqrt(d_star_sq_min); double r_high = std::sqrt(d_star_sq_max); double volume_low = sphere_volume(r_low); double volume_per_bin = (sphere_volume(r_high) - volume_low) / n_bins; limits_.push_back(d_star_sq_min); for(std::size_t i_bin=1;i_bin<n_bins;i_bin++) { double r_sq_i = std::pow( (volume_low + i_bin * volume_per_bin) * 3 / scitbx::constants::four_pi, 2/3.); limits_.push_back(r_sq_i); } limits_.push_back(d_star_sq_max); }
int main() { double dim = 0, delta = 1; while (ABS(delta) > EP) if (sphere_volume(dim + delta) > sphere_volume(dim)) dim += delta; else delta /= -2; fprintf(stdout, "max volume = %.10f at dimension %.10f\n", sphere_volume(dim), dim); return 0; }
extern int main(int argc, char *argv[]) { int dim, max_dims=9; if(2 == argc) max_dims = atoi(argv[1]); printf("static const double sphere_volumes[] = {\n"); for(dim=0; dim<max_dims+1; dim++) print_volume(dim, sphere_volume(dim)); printf("};\n"); return 0; }
int main(){ double length,area,volume,r,h; printf("input circle radius\n"); scanf("%lf", &r); length = circle_length(r); printf("length: %.2f\n",length); area = circle_area(r); printf("circle_area: %.2f\n",area); area = sphere_area(r); printf("sphere_area: %.2f\n", area); volume = sphere_volume(r); printf("sphere_volume: %.2f\n", volume); printf("input height\n"); scanf("%lf", &h); volume = circle_column_volume(r,h); printf("circle_column_volume: %.2f\n",volume); }
/** * Estimates the cost of the enumeration for SVP. */ void cost_estimate(Float &cost, const Float &bound, const Matrix<Float> &r, int dimMax) { Float det, level_cost, tmp1; det = 1.0; cost = 0.0; for (int i = dimMax - 1; i >= 0; i--) { tmp1.div(bound, r(i, i)); det.mul(det, tmp1); level_cost.sqrt(det); sphere_volume(tmp1, dimMax - i); level_cost.mul(level_cost, tmp1); cost.add(cost, level_cost); } }
static char * test_prism2sphere() { double expect, res; SPHERE sphere; int i; PRISM prisms[4] = { {1,0,1000,0,2000,100,2000,0,0,0}, {1,-500,200,300,500,-1000,4000,0,0,0}, {1,-10000000,5000000,5000000,8000000,0,3000000,0,0,0}, {1,-1000000,50000,500000,800000,0,300000,0,0,0}}; for(i = 0; i < 4; i++) { prism2sphere(prisms[i], 0., 0., 0., &sphere); res = sphere_volume(sphere); expect = prism_volume(prisms[i]); sprintf(msg, "(prism %d) expected volume %g, got %g", i, expect, res); mu_assert_almost_equals(res/expect, 1., 0.001, msg); } return 0; }
static char * test_tess2sphere() { double expect, res; SPHERE sphere; int i; TESSEROID tesses[4] = { {1,0,1,0,1,6000000,6001000}, {1,180,190,80,85,6300000,6301000}, {1,160,200,-90,-70,5500000,6000000}, {1,-10,5,-7,15,6500000,6505000}}; for(i = 0; i < 4; i++) { tess2sphere(tesses[i], &sphere); res = sphere_volume(sphere); expect = tess_volume(tesses[i]); sprintf(msg, "(tess %d) expected volume %g, got %g", i, expect, res); mu_assert_almost_equals(res/expect, 1., 0.01, msg); } return 0; }
#ifdef gamma /* Computes the volume of an N-dimensional sphere. Derived from formula in "Regular Polytopes" by H.S.M Coxeter. */ static double sphere_volume( double dimension ) { static const double log_pi = log( 3.1415926535 ); double log_gamma, log_volume; log_gamma = gamma( dimension / 2.0 + 1 ); log_volume = dimension / 2.0 * log_pi - log_gamma; return exp( log_volume ); } static const double UNIT_SPHERE_VOLUME = sphere_volume( RTREE_DIMENSIONS ); #else /* Precomputed volumes of the unit spheres for the first few dimensions */ static const double UNIT_SPHERE_VOLUMES[] = { 0.000000, /* dimension 0 */ 2.000000, /* dimension 1 */ 3.141593, /* dimension 2 */ 4.188790, /* dimension 3 */ 4.934802, /* dimension 4 */ 5.263789, /* dimension 5 */ 5.167713, /* dimension 6 */ 4.724766, /* dimension 7 */ 4.058712, /* dimension 8 */ 3.298509, /* dimension 9 */
| easily computed for any dimension. If not, the value can be precomputed and | taken from a table. The following code can do it either way. -----------------------------------------------------------------------------*/ #ifdef gamma /* computes the volume of an N-dimensional sphere. */ /* derived from formule in "Regular Polytopes" by H.S.M Coxeter */ static double sphere_volume(double dimension) { double log_gamma, log_volume; log_gamma = gamma(dimension/2.0 + 1); log_volume = dimension/2.0 * log(M_PI) - log_gamma; return exp(log_volume); } static const double UnitSphereVolume = sphere_volume(NUMDIMS); #else /* Precomputed volumes of the unit spheres for the first few dimensions */ const double UnitSphereVolumes[] = { 0.000000, /* dimension 0 */ 2.000000, /* dimension 1 */ 3.141593, /* dimension 2 */ 4.188790, /* dimension 3 */ 4.934802, /* dimension 4 */ 5.263789, /* dimension 5 */ 5.167713, /* dimension 6 */ 4.724766, /* dimension 7 */ 4.058712, /* dimension 8 */ 3.298509, /* dimension 9 */