use of org.apache.commons.math3.random.RandomDataGenerator in project mastering-java by Kingminghuang.
the class RandomNumberTest method boundedRandomDoubleWithApache.
@Test
public void boundedRandomDoubleWithApache() {
double leftLimit = 1;
double rightLimit = 10;
double generatedDouble1 = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);
double generatedDouble2 = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);
assertNotEquals(generatedDouble1, generatedDouble2);
}
use of org.apache.commons.math3.random.RandomDataGenerator in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysis method simulateActivations.
private int simulateActivations(RandomDataGenerator rdg, BinomialDistribution bd, float[][] molecules, MemoryPeakResults results, int t, double precision, int id) {
if (bd == null)
return 0;
int n = molecules.length;
int k = bd.sample();
// Sample
RandomGenerator rand = rdg.getRandomGenerator();
int[] sample = Random.sample(k, n, rand);
while (k-- > 0) {
float[] xy = molecules[sample[k]];
float x, y;
do {
x = (float) (xy[0] + rand.nextGaussian() * precision);
} while (outOfBounds(x));
do {
y = (float) (xy[1] + rand.nextGaussian() * precision);
} while (outOfBounds(y));
results.add(createResult(t, x, y));
}
return sample.length;
}
use of org.apache.commons.math3.random.RandomDataGenerator in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysis method simulateMolecules.
private float[][] simulateMolecules(RandomDataGenerator rdg, int c) {
int n = sim_nMolecules[c];
float[][] molecules = new float[n][];
if (n == 0)
return molecules;
// Draw the shapes
Shape[] shapes = createShapes(rdg, c);
// Sample positions from within the shapes
boolean canSample = shapes[0].canSample();
RandomGenerator rand = rdg.getRandomGenerator();
while (n-- > 0) {
float[] coords;
if (canSample) {
int next = rand.nextInt(shapes.length);
coords = shapes[next].sample(rand);
} else {
coords = shapes[n % shapes.length].getPosition();
}
// Avoid out-of-bounds positions
if (outOfBounds(coords[0]) || outOfBounds(coords[1]))
n++;
else
molecules[n] = coords;
}
return molecules;
}
use of org.apache.commons.math3.random.RandomDataGenerator in project GDSC-SMLM by aherbert.
the class ImageModel method setRandomGenerator.
/**
* Set the random generator for creating the image
*
* @param random
*/
public void setRandomGenerator(RandomGenerator random) {
if (random == null)
throw new NullPointerException("Random generator must not be null");
this.random = random;
this.randomGenerator = new RandomDataGenerator(random);
}
use of org.apache.commons.math3.random.RandomDataGenerator in project GDSC-SMLM by aherbert.
the class FastMLEGradient2ProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedure.
private void gradientProcedureLinearIsFasterThanGradientProcedure(final int nparams) {
org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
final int iter = 100;
rdg = new RandomDataGenerator(new Well19937c(30051977));
final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
createData(1, iter, paramsList, yList);
// Remove the timing of the function call by creating a dummy function
final Gradient2Function func = new FakeGradientFunction(blockWidth, nparams);
for (int i = 0; i < paramsList.size(); i++) {
FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
p1.computeSecondDerivative(paramsList.get(i));
p1.computeSecondDerivative(paramsList.get(i));
FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
p2.computeSecondDerivative(paramsList.get(i));
p2.computeSecondDerivative(paramsList.get(i));
// Check they are the same
Assert.assertArrayEquals("D1 " + i, p1.d1, p2.d1, 0);
Assert.assertArrayEquals("D2 " + i, p1.d2, p2.d2, 0);
}
// Realistic loops for an optimisation
final int loops = 15;
// Run till stable timing
Timer t1 = new Timer() {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
for (int j = loops; j-- > 0; ) p1.computeSecondDerivative(paramsList.get(k++ % iter));
}
}
};
long time1 = t1.getTime();
Timer t2 = new Timer(t1.loops) {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
for (int j = loops; j-- > 0; ) p2.computeSecondDerivative(paramsList.get(k++ % iter));
}
}
};
long time2 = t2.getTime();
log("Standard = %d : Unrolled %d = %d : %fx\n", time1, nparams, time2, (1.0 * time1) / time2);
Assert.assertTrue(time2 < time1 * 1.5);
}
Aggregations