Example usage for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setAbsoluteAccuracy

List of usage examples for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setAbsoluteAccuracy

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setAbsoluteAccuracy.

Prototype

void setAbsoluteAccuracy(double accuracy);

Source Link

Document

Set the absolute accuracy.

Usage

From source file:dr.math.distributions.ReflectedNormalDistribution.java

public static void main(String[] args) {

    //        final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 0.5, 2, 1e-6);
    final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 1, 2, 1e-6);

    rnd.pdf(1);/*from  w  w  w. ja v  a 2 s .co m*/

    UnivariateRealFunction f = new UnivariateRealFunction() {
        public double value(double v) throws FunctionEvaluationException {
            return rnd.pdf(v);
        }
    };

    final UnivariateRealIntegrator integrator = new RombergIntegrator();
    integrator.setAbsoluteAccuracy(1e-14);
    integrator.setMaximalIterationCount(16); // fail if it takes too much time

    double x;
    try {
        x = integrator.integrate(f, rnd.lower, rnd.upper);
        //                      ptotErr += cdf != 0.0 ? Math.abs(x-cdf)/cdf : x;
        //                      np += 1;
        //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6);
        System.out.println("Integrated pdf = " + x);

        //System.out.println(shape + ","  + scale + " " + value);
    } catch (ConvergenceException e) {
        // can't integrate , skip test
        //  System.out.println(shape + ","  + scale + " skipped");
    } catch (FunctionEvaluationException e) {

        throw new RuntimeException("I have no idea why I am here.");

    }

}

From source file:sphericalGeo.SphericalDiffusionModel.java

/** anglerange[0] = lower bound, anglerange[1] = upper bound of range for angle2
 * **///  w w  w  . j a  va  2s.c o  m
public double[] sample(double[] start, double time, double precision, double[] angleRange)
        throws ConvergenceException, FunctionEvaluationException, IllegalArgumentException {

    // first, sample an angle from the spherical diffusion density
    final double inverseVariance = precision / time;

    UnivariateRealFunction function = new UnivariateRealFunction() {
        @Override
        public double value(double x) throws FunctionEvaluationException {
            double logR = -x * x * inverseVariance / 2.0 + 0.5 * Math.log(x * Math.sin(x) * inverseVariance);

            return Math.exp(logR);
        }
    };

    UnivariateRealIntegrator integrator = new TrapezoidIntegrator();
    integrator.setAbsoluteAccuracy(1.0e-10);
    integrator.setRelativeAccuracy(1.0e-14);
    integrator.setMinimalIterationCount(2);
    integrator.setMaximalIterationCount(25);

    double[] cumulative = new double[NR_OF_STEPS];
    for (int i = 1; i < NR_OF_STEPS; i++) {
        cumulative[i] = cumulative[i - 1]
                + integrator.integrate(function, (i - 1) * Math.PI / NR_OF_STEPS, i * Math.PI / NR_OF_STEPS);
    }

    // normalise 
    double sum = cumulative[NR_OF_STEPS - 1];
    for (int i = 0; i < NR_OF_STEPS; i++) {
        cumulative[i] /= sum;
    }

    int i = Randomizer.randomChoice(cumulative);
    double angle = i * Math.PI / NR_OF_STEPS;

    // now we have an angle, use this to rotate the point [0,0] over
    // this angle in a random direction angle2
    double angle2 = angleRange[0] + Randomizer.nextDouble() * (angleRange[1] - angleRange[0]);
    //angleRange[0] = angle2;

    double[] xC = new double[] { Math.cos(angle), Math.sin(angle) * Math.cos(angle2),
            Math.sin(angle) * Math.sin(angle2) };

    // convert back to latitude, longitude relative to (lat=0, long=0)
    double[] xL = cartesian2Sperical(xC, true);
    //double [] sC = spherical2Cartesian(start[0], start[1]);
    double[] position = reverseMap(xL[0], xL[1], start[0], start[1]);
    return position;
}