Description
The UnitSphereSampler accepts a dimension argument and creates the sample using iteration over an array of the given dimension creating n Gaussian samples. This can be optimised for low order dimensions to remove the use of array iteration, e.g.
final double[] v = new double[dimension]; double sum = 0; for (int i = 0; i < dimension; i++) { final double x = sampler.sample(); v[i] = x; sum += x * x; }
becomes for 3D:
final double x = sampler.sample(); final double y = sampler.sample(); final double z = sampler.sample(); final double sum = x * x + y * y + z * z;
The special case of 1D sampling can be handled by returning either 1 or -1 in a vector based on a single bit of the random source.
Optimised versions can be created by adding a factory method to the class:
public static UnitSphereSampler of(int dimension, UniformRandomProvider rng) { // ... }