Example usage for org.apache.commons.math3.random UnitSphereRandomVectorGenerator UnitSphereRandomVectorGenerator

List of usage examples for org.apache.commons.math3.random UnitSphereRandomVectorGenerator UnitSphereRandomVectorGenerator

Introduction

In this page you can find the example usage for org.apache.commons.math3.random UnitSphereRandomVectorGenerator UnitSphereRandomVectorGenerator.

Prototype

public UnitSphereRandomVectorGenerator(final int dimension, final RandomGenerator rand) 

Source Link

Usage

From source file:gdsc.core.clustering.optics.ProjectedMoleculeSpace.java

/**
 * Create random projections, project points and put points into sets of size
 * about minSplitSize/2./*www. ja  v  a2  s . co m*/
 *
 * @param minSplitSize
 *            minimum size for which a point set is further
 *            partitioned (roughly corresponds to minPts in OPTICS)
 */
public void computeSets(int minSplitSize) {
    splitSets = new TurboList<Split>();

    // Edge cases
    if (minSplitSize < 2 || size <= 1)
        return;

    if (size == 2) {
        // No point performing projections and splits
        splitSets.add(new Split(0, new int[] { 0, 1 }));
        return;
    }

    final int dim = 2;

    // FastOPTICS paper states you can use c0*log(N) sets and c1*log(N) projections.
    // The ELKI framework increase this for the number of dimensions. However I have stuck
    // with the original (as it is less so will be faster).
    // Note: In most computer science contexts log is in base 2.
    int nPointSetSplits, nProject1d;

    nPointSetSplits = getNumberOfSplitSets(nSplits, size);
    nProject1d = getNumberOfProjections(nProjections, size);

    // perform O(log N+log dim) splits of the entire point sets projections
    //nPointSetSplits = (int) (logOProjectionConst * log2(size * dim + 1));
    // perform O(log N+log dim) projections of the point set onto a random line
    //nProject1d = (int) (logOProjectionConst * log2(size * dim + 1));

    if (nPointSetSplits < 1 || nProject1d < 1)
        return; // Nothing to do

    // perform projections of points
    float[][] projectedPoints = new float[nProject1d][];

    long time = System.currentTimeMillis();
    setUpProgress(nProject1d);
    if (tracker != null) {
        tracker.log("Computing projections ...");
    }

    // Multi-thread this for speed
    int nThreads = Math.min(this.nThreads, nPointSetSplits);
    final TurboList<Thread> threads = new TurboList<Thread>(nThreads);

    final BlockingQueue<ProjectionJob> projectionJobs = new ArrayBlockingQueue<ProjectionJob>(nThreads * 2);
    final TurboList<ProjectionWorker> projectionWorkers = new TurboList<ProjectionWorker>(nThreads);
    for (int i = 0; i < nThreads; i++) {
        final ProjectionWorker worker = new ProjectionWorker(projectionJobs, projectedPoints);
        final Thread t = new Thread(worker);
        projectionWorkers.addf(worker);
        threads.addf(t);
        t.start();
    }

    // Create random vectors or uniform distribution
    RandomVectorGenerator vectorGen = (useRandomVectors) ? new UnitSphereRandomVectorGenerator(2, rand) : null;
    final double increment = Math.PI / nProject1d;
    for (int i = 0; i < nProject1d; i++) {
        // Create a random unit vector
        double[] currRp;
        if (useRandomVectors) {
            currRp = vectorGen.nextVector();
        } else {
            // For a 2D vector we can just uniformly distribute them around a semi-circle
            currRp = new double[dim];
            double a = i * increment;
            currRp[0] = Math.sin(a);
            currRp[1] = Math.cos(a);
        }
        put(projectionJobs, new ProjectionJob(i, currRp));
    }
    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < nThreads; i++) {
        put(projectionJobs, new ProjectionJob(-1, null));
    }

    // Wait for all to finish
    for (int i = 0; i < nThreads; i++) {
        try {
            threads.get(i).join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    threads.clear();

    if (tracker != null) {
        tracker.progress(1);
        long time2 = System.currentTimeMillis();
        tracker.log("Computed projections ... " + Utils.timeToString(time2 - time));
        time = time2;
        tracker.log("Splitting data ...");
    }

    // split entire point set, reuse projections by shuffling them
    int[] proind = Utils.newArray(nProject1d, 0, 1);
    setUpProgress(nPointSetSplits);

    // The splits do not have to be that random so we can use a pseudo random sequence.
    // The sets will be randomly sized between 1 and minSplitSize. Ensure we have enough 
    // numbers for all the splits.
    double expectedSetSize = (1 + minSplitSize) * 0.5;
    int expectedSets = (int) Math.round(size / expectedSetSize);
    pseudoRandom = new TurboRandomGenerator(Math.max(200, minSplitSize + 2 * expectedSets), rand);

    // Multi-thread this for speed
    final BlockingQueue<SplitJob> splitJobs = new ArrayBlockingQueue<SplitJob>(nThreads * 2);
    final TurboList<SplitWorker> splitWorkers = new TurboList<SplitWorker>(nThreads);
    for (int i = 0; i < nThreads; i++) {
        final SplitWorker worker = new SplitWorker(splitJobs, minSplitSize);
        final Thread t = new Thread(worker);
        splitWorkers.addf(worker);
        threads.addf(t);
        t.start();
    }

    for (int i = 0; i < nPointSetSplits; i++) {
        // shuffle projections
        float[][] shuffledProjectedPoints = new float[nProject1d][];
        pseudoRandom.shuffle(proind);
        for (int j = 0; j < nProject1d; j++) {
            shuffledProjectedPoints[j] = projectedPoints[proind[j]];
        }

        // New random generator
        TurboRandomGenerator rand = (TurboRandomGenerator) pseudoRandom.clone();
        rand.setSeed(i);

        put(splitJobs, new SplitJob(i, shuffledProjectedPoints, rand));
    }

    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < nThreads; i++) {
        put(splitJobs, new SplitJob(-1, null, null));
    }

    // Wait for all to finish
    int total = 0;
    for (int i = 0; i < nThreads; i++) {
        try {
            threads.get(i).join();
            total += splitWorkers.get(i).splitSets.size();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    threads.clear();

    // Merge the split-sets
    splitSets = splitWorkers.get(0).splitSets;
    splitSets.ensureCapacity(total);
    for (int i = 1; i < nThreads; i++)
        splitSets.addAll(splitWorkers.get(i).splitSets);

    if (tracker != null) {
        time = System.currentTimeMillis() - time;
        tracker.log("Split data ... " + Utils.timeToString(time));
        tracker.progress(1);
    }
}

From source file:gdsc.smlm.model.AiryPSFModel.java

/**
 * Sample from an Airy distribution//from ww w  .  j  a v a2s .c  o m
 * 
 * @param n
 *            The number of samples
 * @param x0
 *            The centre in dimension 0
 * @param x1
 *            The centre in dimension 1
 * @param w0
 *            The Airy width for dimension 0
 * @param w1
 *            The Airy width for dimension 1
 * @return The sample x and y values
 */
public double[][] sample(final int n, final double x0, final double x1, final double w0, final double w1) {
    this.w0 = w0;
    this.w1 = w1;
    if (spline == null)
        createAiryDistribution();
    double[] x = new double[n];
    double[] y = new double[n];

    final RandomGenerator random = rand.getRandomGenerator();
    UnitSphereRandomVectorGenerator vg = new UnitSphereRandomVectorGenerator(2, random);
    int c = 0;
    for (int i = 0; i < n; i++) {
        final double p = random.nextDouble();
        if (p > POWER[SAMPLE_RINGS]) {
            // TODO - We could add a simple interpolation here using a spline from AiryPattern.power()
            continue;
        }
        final double r = spline.value(p);

        // Convert to xy using a random vector generator
        final double[] v = vg.nextVector();
        x[c] = v[0] * r * w0 + x0;
        y[c] = v[1] * r * w1 + x1;
        c++;
    }

    if (c < n) {
        x = Arrays.copyOf(x, c);
        y = Arrays.copyOf(y, c);
    }
    return new double[][] { x, y };
}

From source file:org.bonej.ops.ellipsoid.EllipsoidPoints.java

/**
 * Sets the seed of the random generators used in point creation.
 * <p>//from   w ww. ja  v  a2  s .  c o m
 * Setting a constant seed makes testing easier.
 * </p>
 *
 * @param seed the seed number.
 * @see Random#setSeed(long)
 * @see MersenneTwister#MersenneTwister(long)
 */
static void setSeed(final long seed) {
    rng.setSeed(seed);
    sphereRng = new UnitSphereRandomVectorGenerator(3, new MersenneTwister(seed));
}