Java Code Examples for org.apache.commons.math3.util.MathUtils#TWO_PI

The following examples show how to use org.apache.commons.math3.util.MathUtils#TWO_PI . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SaddlePointExpansion.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute the logarithm of the PMF for a binomial distribution
 * using the saddle point expansion.
 *
 * @param x the value at which the probability is evaluated.
 * @param n the number of trials.
 * @param p the probability of success.
 * @param q the probability of failure (1 - p).
 * @return log(p(x)).
 */
static double logBinomialProbability(int x, int n, double p, double q) {
    double ret;
    if (x == 0) {
        if (p < 0.1) {
            ret = -getDeviancePart(n, n * q) - n * p;
        } else {
            ret = n * FastMath.log(q);
        }
    } else if (x == n) {
        if (q < 0.1) {
            ret = -getDeviancePart(n, n * p) - n * q;
        } else {
            ret = n * FastMath.log(p);
        }
    } else {
        ret = getStirlingError(n) - getStirlingError(x) -
              getStirlingError(n - x) - getDeviancePart(x, n * p) -
              getDeviancePart(n - x, n * q);
        double f = (MathUtils.TWO_PI * x * (n - x)) / n;
        ret = -0.5 * FastMath.log(f) + ret;
    }
    return ret;
}
 
Example 2
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example 3
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example 4
Source File: ArcsSetTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testShiftedAngles() {
    for (int k = -2; k < 3; ++k) {
        SubLimitAngle l1  = new LimitAngle(new S1Point(1.0 + k * MathUtils.TWO_PI), false, 1.0e-10).wholeHyperplane();
        SubLimitAngle l2  = new LimitAngle(new S1Point(1.5 + k * MathUtils.TWO_PI), true,  1.0e-10).wholeHyperplane();
        ArcsSet set = new ArcsSet(new BSPTree<Sphere1D>(l1,
                                                        new BSPTree<Sphere1D>(Boolean.FALSE),
                                                        new BSPTree<Sphere1D>(l2,
                                                                              new BSPTree<Sphere1D>(Boolean.FALSE),
                                                                              new BSPTree<Sphere1D>(Boolean.TRUE),
                                                                              null),
                                                        null),
                                  1.0e-10);
        for (double alpha = 1.0e-6; alpha < MathUtils.TWO_PI; alpha += 0.001) {
            if (alpha < 1 || alpha > 1.5) {
                Assert.assertEquals(Location.OUTSIDE, set.checkPoint(new S1Point(alpha)));
            } else {
                Assert.assertEquals(Location.INSIDE,  set.checkPoint(new S1Point(alpha)));
            }
        }
    }

}
 
Example 5
Source File: ArcsSetTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSplitOver2Pi() {
    ArcsSet set = new ArcsSet(1.0e-10);
    Arc     arc = new Arc(1.5 * FastMath.PI, 2.5 * FastMath.PI, 1.0e-10);
    ArcsSet.Split split = set.split(arc);
    for (double alpha = 0.0; alpha <= MathUtils.TWO_PI; alpha += 0.01) {
        S1Point p = new S1Point(alpha);
        if (alpha < 0.5 * FastMath.PI || alpha > 1.5 * FastMath.PI) {
            Assert.assertEquals(Location.OUTSIDE, split.getPlus().checkPoint(p));
            Assert.assertEquals(Location.INSIDE,  split.getMinus().checkPoint(p));
        } else {
            Assert.assertEquals(Location.INSIDE,  split.getPlus().checkPoint(p));
            Assert.assertEquals(Location.OUTSIDE, split.getMinus().checkPoint(p));
        }
    }
}
 
Example 6
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example 7
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example 8
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example 9
Source File: ArcsSetTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testShiftedAngles() {
    for (int k = -2; k < 3; ++k) {
        SubLimitAngle l1  = new LimitAngle(new S1Point(1.0 + k * MathUtils.TWO_PI), false, 1.0e-10).wholeHyperplane();
        SubLimitAngle l2  = new LimitAngle(new S1Point(1.5 + k * MathUtils.TWO_PI), true,  1.0e-10).wholeHyperplane();
        ArcsSet set = new ArcsSet(new BSPTree<Sphere1D>(l1,
                                                        new BSPTree<Sphere1D>(Boolean.FALSE),
                                                        new BSPTree<Sphere1D>(l2,
                                                                              new BSPTree<Sphere1D>(Boolean.FALSE),
                                                                              new BSPTree<Sphere1D>(Boolean.TRUE),
                                                                              null),
                                                        null),
                                  1.0e-10);
        for (double alpha = 1.0e-6; alpha < MathUtils.TWO_PI; alpha += 0.001) {
            if (alpha < 1 || alpha > 1.5) {
                Assert.assertEquals(Location.OUTSIDE, set.checkPoint(new S1Point(alpha)));
            } else {
                Assert.assertEquals(Location.INSIDE,  set.checkPoint(new S1Point(alpha)));
            }
        }
    }

}
 
Example 10
Source File: LimitAngleTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReversedLimit() {
    for (int k = -2; k < 3; ++k) {
        LimitAngle l  = new LimitAngle(new S1Point(1.0 + k * MathUtils.TWO_PI), false, 1.0e-10);
        Assert.assertEquals(l.getLocation().getAlpha(), l.getReverse().getLocation().getAlpha(), 1.0e-10);
        Assert.assertEquals(l.getTolerance(), l.getReverse().getTolerance(), 1.0e-10);
        Assert.assertTrue(l.sameOrientationAs(l));
        Assert.assertFalse(l.sameOrientationAs(l.getReverse()));
        Assert.assertEquals(MathUtils.TWO_PI, l.wholeSpace().getSize(), 1.0e-10);
        Assert.assertEquals(MathUtils.TWO_PI, l.getReverse().wholeSpace().getSize(), 1.0e-10);
    }
}
 
Example 11
Source File: Arc.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Check a point with respect to the arc.
 * @param point point to check
 * @return a code representing the point status: either {@link
 * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
 */
public Location checkPoint(final double point) {
    final double normalizedPoint = MathUtils.normalizeAngle(point, middle);
    if (normalizedPoint < lower - tolerance || normalizedPoint > upper + tolerance) {
        return Location.OUTSIDE;
    } else if (normalizedPoint > lower + tolerance && normalizedPoint < upper - tolerance) {
        return Location.INSIDE;
    } else {
        return (getSize() >= MathUtils.TWO_PI - tolerance) ? Location.INSIDE : Location.BOUNDARY;
    }
}
 
Example 12
Source File: CircleTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkArcIsInside(final Circle arcCircle, final Circle otherCircle) {
    Arc arc = arcCircle.getInsideArc(otherCircle);
    Assert.assertEquals(FastMath.PI, arc.getSize(), 1.0e-10);
    for (double alpha = arc.getInf(); alpha < arc.getSup(); alpha += 0.1) {
        Assert.assertTrue(otherCircle.getOffset(arcCircle.getPointAt(alpha)) <= 2.0e-15);
    }
    for (double alpha = arc.getSup(); alpha < arc.getInf() + MathUtils.TWO_PI; alpha += 0.1) {
        Assert.assertTrue(otherCircle.getOffset(arcCircle.getPointAt(alpha)) >= -2.0e-15);
    }
}
 
Example 13
Source File: S1PointTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testS1Point() {
    for (int k = -2; k < 3; ++k) {
        S1Point p = new S1Point(1.0 + k * MathUtils.TWO_PI);
        Assert.assertEquals(FastMath.cos(1.0), p.getVector().getX(), 1.0e-10);
        Assert.assertEquals(FastMath.sin(1.0), p.getVector().getY(), 1.0e-10);
        Assert.assertFalse(p.isNaN());
    }
}
 
Example 14
Source File: ArcsSetTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testWrapAround2PiArc() {
    ArcsSet set = new ArcsSet(5.7 - MathUtils.TWO_PI, 2.3, 1.0e-10);
    Assert.assertEquals(MathUtils.TWO_PI - 3.4, set.getSize(), 1.0e-10);
    Assert.assertEquals(1.0e-10, set.getTolerance(), 1.0e-20);
    Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new S1Point(2.3)));
    Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new S1Point(5.7)));
    Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new S1Point(1.2)));
    Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new S1Point(8.5)));
    Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new S1Point(8.7)));
    Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new S1Point(3.0)));
    Assert.assertEquals(1, set.asList().size());
    Assert.assertEquals(5.7, set.asList().get(0).getInf(), 1.0e-10);
    Assert.assertEquals(2.3 + MathUtils.TWO_PI, set.asList().get(0).getSup(), 1.0e-10);
}
 
Example 15
Source File: LimitAngleTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReversedLimit() {
    for (int k = -2; k < 3; ++k) {
        LimitAngle l  = new LimitAngle(new S1Point(1.0 + k * MathUtils.TWO_PI), false, 1.0e-10);
        Assert.assertEquals(l.getLocation().getAlpha(), l.getReverse().getLocation().getAlpha(), 1.0e-10);
        Assert.assertEquals(l.getTolerance(), l.getReverse().getTolerance(), 1.0e-10);
        Assert.assertTrue(l.sameOrientationAs(l));
        Assert.assertFalse(l.sameOrientationAs(l.getReverse()));
        Assert.assertEquals(MathUtils.TWO_PI, l.wholeSpace().getSize(), 1.0e-10);
        Assert.assertEquals(MathUtils.TWO_PI, l.getReverse().wholeSpace().getSize(), 1.0e-10);
    }
}
 
Example 16
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] params) {
            final double cx = params[0];
            final double cy = params[1];
            final double r = params[2];

            final double[] model = new double[points.size() * 2];

            final double deltaTheta = MathUtils.TWO_PI / resolution;
            for (int i = 0; i < points.size(); i++) {
                final double[] p = points.get(i);
                final double px = p[0];
                final double py = p[1];

                double bestX = 0;
                double bestY = 0;
                double dMin = Double.POSITIVE_INFINITY;

                // Find the angle for which the circle passes closest to the
                // current point (using a resolution of 100 points along the
                // circumference).
                for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
                    final double currentX = cx + r * FastMath.cos(theta);
                    final double currentY = cy + r * FastMath.sin(theta);
                    final double dX = currentX - px;
                    final double dY = currentY - py;
                    final double d = dX * dX + dY * dY;
                    if (d < dMin) {
                        dMin = d;
                        bestX = currentX;
                        bestY = currentY;
                    }
                }

                final int index = i * 2;
                model[index] = bestX;
                model[index + 1] = bestY;
            }

            return model;
        }
    };
}
 
Example 17
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ModelFunction getModelFunction() {
    return new ModelFunction(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                final double cx = params[0];
                final double cy = params[1];
                final double r = params[2];

                final double[] model = new double[points.size() * 2];

                final double deltaTheta = MathUtils.TWO_PI / resolution;
                for (int i = 0; i < points.size(); i++) {
                    final double[] p = points.get(i);
                    final double px = p[0];
                    final double py = p[1];

                    double bestX = 0;
                    double bestY = 0;
                    double dMin = Double.POSITIVE_INFINITY;

                    // Find the angle for which the circle passes closest to the
                    // current point (using a resolution of 100 points along the
                    // circumference).
                    for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
                        final double currentX = cx + r * FastMath.cos(theta);
                        final double currentY = cy + r * FastMath.sin(theta);
                        final double dX = currentX - px;
                        final double dY = currentY - py;
                        final double d = dX * dX + dY * dY;
                        if (d < dMin) {
                            dMin = d;
                            bestX = currentX;
                            bestY = currentY;
                        }
                    }

                    final int index = i * 2;
                    model[index] = bestX;
                    model[index + 1] = bestY;
                }

                return model;
            }
        });
}
 
Example 18
Source File: ArcsSet.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Walk the tree to select the pending sub-arc.
 */
private void selectPending() {

    // look for the start of the arc
    BSPTree<Sphere1D> start = current;
    while (start != null && !isArcStart(start)) {
        start = nextInternalNode(start);
    }

    if (start == null) {
        // we have exhausted the iterator
        current = null;
        pending = null;
        return;
    }

    // look for the end of the arc
    BSPTree<Sphere1D> end = start;
    while (end != null && !isArcEnd(end)) {
        end = nextInternalNode(end);
    }

    if (end != null) {

        // we have identified the arc
        pending = new double[] {
            getAngle(start), getAngle(end)
        };

        // prepare search for next arc
        current = end;

    } else {

        // the final arc wraps around 2\pi, its end is before the first start
        end = firstStart;
        while (end != null && !isArcEnd(end)) {
            end = previousInternalNode(end);
        }
        if (end == null) {
            // this should never happen
            throw new MathInternalError();
        }

        // we have identified the last arc
        pending = new double[] {
            getAngle(start), getAngle(end) + MathUtils.TWO_PI
        };

        // there won't be any other arcs
        current = null;

    }

}
 
Example 19
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] params) {
            final double cx = params[0];
            final double cy = params[1];
            final double r = params[2];

            final double[] model = new double[points.size() * 2];

            final double deltaTheta = MathUtils.TWO_PI / resolution;
            for (int i = 0; i < points.size(); i++) {
                final double[] p = points.get(i);
                final double px = p[0];
                final double py = p[1];

                double bestX = 0;
                double bestY = 0;
                double dMin = Double.POSITIVE_INFINITY;

                // Find the angle for which the circle passes closest to the
                // current point (using a resolution of 100 points along the
                // circumference).
                for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
                    final double currentX = cx + r * FastMath.cos(theta);
                    final double currentY = cy + r * FastMath.sin(theta);
                    final double dX = currentX - px;
                    final double dY = currentY - py;
                    final double d = dX * dX + dY * dY;
                    if (d < dMin) {
                        dMin = d;
                        bestX = currentX;
                        bestY = currentY;
                    }
                }

                final int index = i * 2;
                model[index] = bestX;
                model[index + 1] = bestY;
            }

            return model;
        }
    };
}
 
Example 20
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ModelFunction getModelFunction() {
    return new ModelFunction(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                final double cx = params[0];
                final double cy = params[1];
                final double r = params[2];

                final double[] model = new double[points.size() * 2];

                final double deltaTheta = MathUtils.TWO_PI / resolution;
                for (int i = 0; i < points.size(); i++) {
                    final double[] p = points.get(i);
                    final double px = p[0];
                    final double py = p[1];

                    double bestX = 0;
                    double bestY = 0;
                    double dMin = Double.POSITIVE_INFINITY;

                    // Find the angle for which the circle passes closest to the
                    // current point (using a resolution of 100 points along the
                    // circumference).
                    for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
                        final double currentX = cx + r * FastMath.cos(theta);
                        final double currentY = cy + r * FastMath.sin(theta);
                        final double dX = currentX - px;
                        final double dY = currentY - py;
                        final double d = dX * dX + dY * dY;
                        if (d < dMin) {
                            dMin = d;
                            bestX = currentX;
                            bestY = currentY;
                        }
                    }

                    final int index = i * 2;
                    model[index] = bestX;
                    model[index + 1] = bestY;
                }

                return model;
            }
        });
}