Skip to main content

sample_sphere_volume

Function sample_sphere_volume 

Source
pub fn sample_sphere_volume(
    dim: usize,
    n: usize,
    radius: f64,
    positive: bool,
) -> Result<Vec<Vec<f64>>>
Expand description

Samples n points uniformly from the volume (the ball) of the dim-dimensional sphere of the given radius, centered at the origin, using the thread’s default random number generator.

With positive set, the points are restricted to the positive orthant. Each returned point has dim coordinates and Euclidean norm at most radius.

See also Graph::grg_game, which samples points uniformly in the unit square and connects nearby ones, and Graph::nearest_neighbor_graph to build a graph from sampled points in any dimension.

Binds igraph_rng_sample_sphere_volume. Time complexity: O(n · dim).

§Errors

ErrorKind::InvalidValue if dim < 2, or radius is not positive and finite.

§Examples

use igraph::{misc, prelude::*};

rng::seed(42)?;
let pts = misc::sample_sphere_volume(2, 4000, 1.0, false)?;
assert!(pts.iter().all(|p| p[0].hypot(p[1]) <= 1.0));
// Uniform in the disk: about a quarter of the points within radius 1/2.
let inner = pts.iter().filter(|p| p[0].hypot(p[1]) < 0.5).count() as f64 / 4000.0;
assert!((inner - 0.25).abs() < 0.03);