MATH-1335

Use new RNG API.
This commit is contained in:
Gilles 2016-05-11 15:03:03 +02:00
parent 63a9488f29
commit 829a4e9ce3
9 changed files with 59 additions and 42 deletions

View File

@ -17,6 +17,10 @@
package org.apache.commons.math4.random;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.NormalDistribution;
import org.apache.commons.math4.util.FastMath;
@ -28,9 +32,9 @@ import org.apache.commons.math4.util.FastMath;
public class UnitSphereRandomVectorGenerator
implements RandomVectorGenerator {
/**
* RNG used for generating the individual components of the vectors.
* Sampler used for generating the individual components of the vectors.
*/
private final RandomGenerator rand;
private final RealDistribution.Sampler rand;
/**
* Space dimension.
*/
@ -38,12 +42,12 @@ public class UnitSphereRandomVectorGenerator
/**
* @param dimension Space dimension.
* @param rand RNG for the individual components of the vectors.
* @param rng RNG for the individual components of the vectors.
*/
public UnitSphereRandomVectorGenerator(final int dimension,
final RandomGenerator rand) {
final UniformRandomProvider rng) {
this.dimension = dimension;
this.rand = rand;
this.rand = new NormalDistribution().createSampler(rng);
}
/**
* Create an object that will use a default RNG ({@link MersenneTwister}),
@ -52,7 +56,7 @@ public class UnitSphereRandomVectorGenerator
* @param dimension Space dimension.
*/
public UnitSphereRandomVectorGenerator(final int dimension) {
this(dimension, new MersenneTwister());
this(dimension, RandomSource.create(RandomSource.MT_64));
}
/** {@inheritDoc} */
@ -65,7 +69,7 @@ public class UnitSphereRandomVectorGenerator
// normalizing to unit length.
double normSq = 0;
for (int i = 0; i < dimension; i++) {
final double comp = rand.nextGaussian();
final double comp = rand.sample();
v[i] = comp;
normSq += comp * comp;
}

View File

@ -26,9 +26,9 @@ import org.apache.commons.math4.geometry.enclosing.WelzlEncloser;
import org.apache.commons.math4.geometry.euclidean.threed.Euclidean3D;
import org.apache.commons.math4.geometry.euclidean.threed.SphereGenerator;
import org.apache.commons.math4.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.junit.Assert;
import org.junit.Test;
@ -104,7 +104,8 @@ public class WelzlEncloser3DTest {
@Test
public void testLargeSamples() throws IOException {
RandomGenerator random = new Well1024a(0x35ddecfc78131e1dl);
final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x35ddecfc78131e1dl);
final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(3, random);
for (int k = 0; k < 50; ++k) {

View File

@ -30,7 +30,8 @@ import org.apache.commons.math4.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math4.linear.MatrixUtils;
import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
import org.junit.Assert;
@ -959,8 +960,8 @@ public class FieldRotationDSTest {
@Test
public void testDoubleVectors() throws MathIllegalArgumentException {
Well1024a random = new Well1024a(0x180b41cfeeffaf67l);
UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x180b41cfeeffaf67l);
UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 10; ++i) {
double[] unit = g.nextVector();
@ -994,8 +995,8 @@ public class FieldRotationDSTest {
@Test
public void testDoubleRotations() throws MathIllegalArgumentException {
Well1024a random = new Well1024a(0x180b41cfeeffaf67l);
UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x180b41cfeeffaf67l);
UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 10; ++i) {
double[] unit1 = g.nextVector();

View File

@ -29,7 +29,8 @@ import org.apache.commons.math4.geometry.euclidean.threed.Rotation;
import org.apache.commons.math4.geometry.euclidean.threed.RotationOrder;
import org.apache.commons.math4.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
import org.junit.Assert;
@ -800,8 +801,8 @@ public class FieldRotationDfpTest {
@Test
public void testDoubleVectors() throws MathIllegalArgumentException {
Well1024a random = new Well1024a(0x180b41cfeeffaf67l);
UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x180b41cfeeffaf67l);
UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 10; ++i) {
double[] unit = g.nextVector();
@ -835,9 +836,9 @@ public class FieldRotationDfpTest {
@Test
public void testDoubleRotations() throws MathIllegalArgumentException {
UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x180b41cfeeffaf67l);
DfpField field = new DfpField(20);
Well1024a random = new Well1024a(0x180b41cfeeffaf67l);
UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 10; ++i) {
double[] unit1 = g.nextVector();

View File

@ -24,9 +24,9 @@ import org.apache.commons.math4.geometry.enclosing.EnclosingBall;
import org.apache.commons.math4.geometry.euclidean.threed.Euclidean3D;
import org.apache.commons.math4.geometry.euclidean.threed.SphereGenerator;
import org.apache.commons.math4.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -137,7 +137,8 @@ public class SphereGeneratorTest {
@Test
public void testRandom() {
final RandomGenerator random = new Well1024a(0xd015982e9f31ee04l);
final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0xd015982e9f31ee04l);
final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 100; ++i) {
double d = 25 * random.nextDouble();
@ -151,7 +152,6 @@ public class SphereGeneratorTest {
Assert.assertEquals(0.0, refCenter.distance(sphere.getCenter()), 4e-7 * refRadius);
Assert.assertEquals(refRadius, sphere.getRadius(), 1e-7 * refRadius);
}
}
@Test

View File

@ -26,7 +26,8 @@ import org.apache.commons.math4.geometry.euclidean.twod.Euclidean2D;
import org.apache.commons.math4.geometry.euclidean.twod.Vector2D;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.junit.Assert;
import org.junit.Test;
@ -102,7 +103,8 @@ public class DiskGeneratorTest {
@Test
public void testRandom() {
final RandomGenerator random = new Well1024a(0x12faa818373ffe90l);
final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x12faa818373ffe90l);
final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(2, random);
for (int i = 0; i < 500; ++i) {
double d = 25 * random.nextDouble();

View File

@ -30,7 +30,8 @@ import org.apache.commons.math4.geometry.spherical.twod.S2Point;
import org.apache.commons.math4.geometry.spherical.twod.Sphere2D;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
import org.junit.Assert;
@ -135,8 +136,8 @@ public class CircleTest {
@Test
public void testInsideArc() {
RandomGenerator random = new Well1024a(0xbfd34e92231bbcfel);
UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, random);
UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0xbfd34e92231bbcfel));
for (int i = 0; i < 100; ++i) {
Circle c1 = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
Circle c2 = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
@ -158,7 +159,8 @@ public class CircleTest {
@Test
public void testTransform() {
RandomGenerator random = new Well1024a(0x16992fc4294bf2f1l);
UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
0x16992fc4294bf2f1l);
UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 100; ++i) {

View File

@ -35,7 +35,7 @@ import org.apache.commons.math4.geometry.spherical.twod.SphericalPolygonsSet;
import org.apache.commons.math4.geometry.spherical.twod.SubCircle;
import org.apache.commons.math4.geometry.spherical.twod.Vertex;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.random.Well1024a;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
import org.junit.Assert;
@ -47,7 +47,8 @@ public class SphericalPolygonsSetTest {
public void testFullSphere() {
SphericalPolygonsSet full = new SphericalPolygonsSet(1.0e-10);
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0x852fd2a0ed8d2f6dl));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0x852fd2a0ed8d2f6dl));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
Assert.assertEquals(Location.INSIDE, full.checkPoint(new S2Point(v)));
@ -64,7 +65,8 @@ public class SphericalPolygonsSetTest {
SphericalPolygonsSet empty =
(SphericalPolygonsSet) new RegionFactory<Sphere2D>().getComplement(new SphericalPolygonsSet(1.0e-10));
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0x76d9205d6167b6ddl));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0x76d9205d6167b6ddl));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
Assert.assertEquals(Location.OUTSIDE, empty.checkPoint(new S2Point(v)));
@ -82,7 +84,8 @@ public class SphericalPolygonsSetTest {
double sinTol = FastMath.sin(tol);
SphericalPolygonsSet south = new SphericalPolygonsSet(Vector3D.MINUS_K, tol);
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0x6b9d4a6ad90d7b0bl));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0x6b9d4a6ad90d7b0bl));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
if (v.getZ() < -sinTol) {
@ -117,7 +120,8 @@ public class SphericalPolygonsSetTest {
SphericalPolygonsSet octant =
(SphericalPolygonsSet) factory.intersection(factory.intersection(plusX, plusY), plusZ);
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0x9c9802fde3cbcf25l));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0x9c9802fde3cbcf25l));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
if ((v.getX() > sinTol) && (v.getY() > sinTol) && (v.getZ() > sinTol)) {
@ -181,7 +185,8 @@ public class SphericalPolygonsSetTest {
double sinTol = FastMath.sin(tol);
SphericalPolygonsSet octant = new SphericalPolygonsSet(tol, S2Point.PLUS_I, S2Point.PLUS_J, S2Point.PLUS_K);
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0xb8fc5acc91044308l));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0xb8fc5acc91044308l));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
if ((v.getX() > sinTol) && (v.getY() > sinTol) && (v.getZ() > sinTol)) {
@ -206,7 +211,8 @@ public class SphericalPolygonsSetTest {
(SphericalPolygonsSet) factory.difference(plusZ, factory.intersection(plusX, plusY));
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0x9c9802fde3cbcf25l));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0x9c9802fde3cbcf25l));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
if (((v.getX() < -sinTol) || (v.getY() < -sinTol)) && (v.getZ() > sinTol)) {
@ -339,7 +345,8 @@ public class SphericalPolygonsSetTest {
SphericalPolygonsSet polygon = new SphericalPolygonsSet(boundary, tol);
UnitSphereRandomVectorGenerator random =
new UnitSphereRandomVectorGenerator(3, new Well1024a(0xcc5ce49949e0d3ecl));
new UnitSphereRandomVectorGenerator(3, RandomSource.create(RandomSource.WELL_1024_A,
0xcc5ce49949e0d3ecl));
for (int i = 0; i < 1000; ++i) {
Vector3D v = new Vector3D(random.nextVector());
if ((v.getX() < -sinTol) && (v.getY() < -sinTol) && (v.getZ() < -sinTol)) {

View File

@ -16,8 +16,8 @@
*/
package org.apache.commons.math4.random;
import org.apache.commons.math4.random.JDKRandomGenerator;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
@ -31,8 +31,7 @@ public class UnitSphereRandomVectorGeneratorTest {
@Test
public void test2DDistribution() {
RandomGenerator rg = new JDKRandomGenerator();
rg.setSeed(17399225432l);
UniformRandomProvider rg = RandomSource.create(RandomSource.XOR_SHIFT_1024_S, 17399225432L);
UnitSphereRandomVectorGenerator generator = new UnitSphereRandomVectorGenerator(2, rg);
// In 2D, angles with a given vector should be uniformly distributed