Package org.apache.commons.rng.sampling
Class UnitSphereSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.UnitSphereSampler
-
- All Implemented Interfaces:
ObjectSampler<double[]>
,SharedStateObjectSampler<double[]>
,SharedStateSampler<SharedStateObjectSampler<double[]>>
- Direct Known Subclasses:
UnitSphereSampler.UnitSphereSampler1D
,UnitSphereSampler.UnitSphereSampler2D
,UnitSphereSampler.UnitSphereSampler3D
,UnitSphereSampler.UnitSphereSamplerND
public class UnitSphereSampler extends java.lang.Object implements SharedStateObjectSampler<double[]>
Generate vectors isotropically located on the surface of a sphere.Sampling in 2 or more dimensions uses:
Sampling in 1D uses the sign bit from
UniformRandomProvider.nextInt()
to set the direction of the vector.- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
UnitSphereSampler.UnitSphereSampler1D
Sample uniformly from the ends of a 1D unit line.private static class
UnitSphereSampler.UnitSphereSampler2D
Sample uniformly from a 2D unit circle.private static class
UnitSphereSampler.UnitSphereSampler3D
Sample uniformly from a 3D unit sphere.private static class
UnitSphereSampler.UnitSphereSamplerND
Sample uniformly from a ND unit sphere.
-
Field Summary
Fields Modifier and Type Field Description private UnitSphereSampler
delegate
The internal sampler optimised for the dimension.private static int
MASK_BIT_2
The mask to extract the second bit from an integer (naming starts at bit 1 for the least significant bit).private static int
ONE_D
The dimension for 1D sampling.private static int
THREE_D
The dimension for 3D sampling.private static int
TWO_D
The dimension for 2D sampling.
-
Constructor Summary
Constructors Modifier Constructor Description private
UnitSphereSampler()
Private constructor used by sub-class specialisations.UnitSphereSampler(int dimension, UniformRandomProvider rng)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description double[]
nextVector()
Deprecated.Usesample()
.static UnitSphereSampler
of(UniformRandomProvider rng, int dimension)
Create a unit sphere sampler for the given dimension.double[]
sample()
Create an object sample.UnitSphereSampler
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
-
-
-
Field Detail
-
ONE_D
private static final int ONE_D
The dimension for 1D sampling.- See Also:
- Constant Field Values
-
TWO_D
private static final int TWO_D
The dimension for 2D sampling.- See Also:
- Constant Field Values
-
THREE_D
private static final int THREE_D
The dimension for 3D sampling.- See Also:
- Constant Field Values
-
MASK_BIT_2
private static final int MASK_BIT_2
The mask to extract the second bit from an integer (naming starts at bit 1 for the least significant bit). The masked integer will have a value 0 or 2.- See Also:
- Constant Field Values
-
delegate
private final UnitSphereSampler delegate
The internal sampler optimised for the dimension.
-
-
Constructor Detail
-
UnitSphereSampler
@Deprecated public UnitSphereSampler(int dimension, UniformRandomProvider rng)
Deprecated.This instance delegates sampling. Use the factory methodof(UniformRandomProvider, int)
to create an optimal sampler.- Parameters:
dimension
- Space dimension.rng
- Generator for the individual components of the vectors. A shallow copy will be stored in this instance.- Throws:
java.lang.IllegalArgumentException
- Ifdimension <= 0
-
UnitSphereSampler
private UnitSphereSampler()
Private constructor used by sub-class specialisations. In future versions the public constructor should be removed and the class made abstract.
-
-
Method Detail
-
sample
public double[] sample()
Description copied from interface:ObjectSampler
Create an object sample.- Specified by:
sample
in interfaceObjectSampler<double[]>
- Returns:
- a random normalized Cartesian vector.
- Since:
- 1.4
-
nextVector
@Deprecated public double[] nextVector()
Deprecated.Usesample()
.- Returns:
- a random normalized Cartesian vector.
-
withUniformRandomProvider
public UnitSphereSampler withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateObjectSampler<double[]>>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static UnitSphereSampler of(UniformRandomProvider rng, int dimension)
Create a unit sphere sampler for the given dimension.- Parameters:
rng
- Generator for the individual components of the vectors. A shallow copy will be stored in the sampler.dimension
- Space dimension.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- Ifdimension <= 0
- Since:
- 1.4
-
-