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

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

Introduction

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

Prototype

public double[] nextVector() 

Source Link

Usage

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

/**
 * Sample from an Airy distribution//from w  w  w . java 2s.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 };
}