Java Code Examples for org.apache.commons.math3.util.FastMath#PI

The following examples show how to use org.apache.commons.math3.util.FastMath#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: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<Vector2D> makeMoons(int samples, boolean shuffle, double noise, RandomGenerator random) {
    NormalDistribution dist = new NormalDistribution(random, 0.0, noise, 1e-9);

    int nSamplesOut = samples / 2;
    int nSamplesIn = samples - nSamplesOut;
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    double range = FastMath.PI;
    double step = range / (nSamplesOut / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D outerCircle = new Vector2D(FastMath.cos(angle), FastMath.sin(angle));
        points.add(outerCircle.add(generateNoiseVector(dist)));
    }

    step = range / (nSamplesIn / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D innerCircle = new Vector2D(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
        points.add(innerCircle.add(generateNoiseVector(dist)));
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example 2
Source File: EventFilterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIncreasingOnly()
    throws DimensionMismatchException, NumberIsTooSmallException,
           MaxCountExceededException, NoBracketingException {
    double e = 1e-15;
    FirstOrderIntegrator integrator;
    integrator = new DormandPrince853Integrator(1.0e-3, 100.0, 1e-7, 1e-7);
    Event allEvents = new Event(true, true);
    integrator.addEventHandler(allEvents, 0.1, e, 1000,
                               new BracketingNthOrderBrentSolver(1.0e-7, 5));
    Event onlyIncreasing = new Event(false, true);
    integrator.addEventHandler(new EventFilter(onlyIncreasing,
                                               FilterType.TRIGGER_ONLY_INCREASING_EVENTS),
                               0.1, e, 100,
                               new BracketingNthOrderBrentSolver(1.0e-7, 5));
    double t0 = 0.5 * FastMath.PI;
    double tEnd = 5.5 * FastMath.PI;
    double[] y = { 0.0, 1.0 };
    Assert.assertEquals(tEnd,
                        integrator.integrate(new SineCosine(), t0, y, tEnd, y),
                        1.0e-7);

    Assert.assertEquals(5, allEvents.getEventCount());
    Assert.assertEquals(2, onlyIncreasing.getEventCount());

}
 
Example 3
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<Vector2D> makeMoons(int samples, boolean shuffle, double noise, RandomGenerator random) {
    NormalDistribution dist = new NormalDistribution(random, 0.0, noise, 1e-9);

    int nSamplesOut = samples / 2;
    int nSamplesIn = samples - nSamplesOut;
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    double range = FastMath.PI;
    double step = range / (nSamplesOut / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D outerCircle = new Vector2D(FastMath.cos(angle), FastMath.sin(angle));
        points.add(outerCircle.add(generateNoiseVector(dist)));
    }

    step = range / (nSamplesIn / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D innerCircle = new Vector2D(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
        points.add(innerCircle.add(generateNoiseVector(dist)));
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example 4
Source File: FastFourierTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStandardTransformFunction() {
    final UnivariateFunction f = new Sinc();
    final double min = -FastMath.PI;
    final double max = FastMath.PI;
    final DftNormalization[] norm;
    norm = DftNormalization.values();
    final TransformType[] type;
    type = TransformType.values();
    for (int i = 0; i < norm.length; i++) {
        for (int j = 0; j < type.length; j++) {
            doTestTransformFunction(f, min, max, 2, 1.0E-15, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 4, 1.0E-14, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 8, 1.0E-14, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 16, 1.0E-13, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 32, 1.0E-13, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 64, 1.0E-12, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 128, 1.0E-11, norm[i], type[j]);
        }
    }
}
 
Example 5
Source File: FastFourierTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Naive implementation of DFT, for reference. */
private static Complex[] dft(final Complex[] x, final int sgn) {
    final int n = x.length;
    final double[] cos = new double[n];
    final double[] sin = new double[n];
    final Complex[] y = new Complex[n];
    for (int i = 0; i < n; i++) {
        final double arg = 2.0 * FastMath.PI * i / n;
        cos[i] = FastMath.cos(arg);
        sin[i] = FastMath.sin(arg);
    }
    for (int i = 0; i < n; i++) {
        double yr = 0.0;
        double yi = 0.0;
        for (int j = 0; j < n; j++) {
            final int index = (i * j) % n;
            final double c = cos[index];
            final double s = sin[index];
            final double xr = x[j].getReal();
            final double xi = x[j].getImaginary();
            yr += c * xr - sgn * s * xi;
            yi += sgn * s * xr + c * xi;
        }
        y[i] = new Complex(yr, yi);
    }
    return y;
}
 
Example 6
Source File: RootsOfUnityTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void doTestComputeRoots(final RootsOfUnity roots) {
    final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() :
        -roots.getNumberOfRoots();
    final double tol = 10 * Math.ulp(1.0);
    for (int k = 0; k < n; k++) {
        final double t = 2.0 * FastMath.PI * k / n;
        @SuppressWarnings("boxing")
        final String msg = String.format("n = %d, k = %d", n, k);
        Assert.assertEquals(msg, FastMath.cos(t), roots.getReal(k), tol);
        Assert.assertEquals(msg, FastMath.sin(t), roots.getImaginary(k), tol);
    }
}
 
Example 7
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public Line apply(final Hyperplane<Euclidean2D> hyperplane) {
    final Line   line    = (Line) hyperplane;
    final double rOffset = MathArrays.linearCombination(c1X, line.cos, c1Y, line.sin, c11, line.originOffset);
    final double rCos    = MathArrays.linearCombination(cXX, line.cos, cXY, line.sin);
    final double rSin    = MathArrays.linearCombination(cYX, line.cos, cYY, line.sin);
    final double inv     = 1.0 / FastMath.sqrt(rSin * rSin + rCos * rCos);
    return new Line(FastMath.PI + FastMath.atan2(-rSin, -rCos),
                    inv * rCos, inv * rSin,
                    inv * rOffset, line.tolerance);
}
 
Example 8
Source File: NPEfix_00159_t.java    From coming with MIT License 5 votes vote down vote up
/** Revert the instance.
 */
public void revertSelf() {
    if (angle < FastMath.PI) {
        angle += FastMath.PI;
    } else {
        angle -= FastMath.PI;
    }
    cos          = -cos;
    sin          = -sin;
    originOffset = -originOffset;
}
 
Example 9
Source File: RotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAxisAngle() throws MathIllegalArgumentException {

  Rotation r = new Rotation(new Vector3D(10, 10, 10), 2 * FastMath.PI / 3);
  checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_J);
  checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_K);
  checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_I);
  double s = 1 / FastMath.sqrt(3);
  checkVector(r.getAxis(), new Vector3D(s, s, s));
  checkAngle(r.getAngle(), 2 * FastMath.PI / 3);

  try {
    new Rotation(new Vector3D(0, 0, 0), 2 * FastMath.PI / 3);
    Assert.fail("an exception should have been thrown");
  } catch (MathIllegalArgumentException e) {
  }

  r = new Rotation(Vector3D.PLUS_K, 1.5 * FastMath.PI);
  checkVector(r.getAxis(), new Vector3D(0, 0, -1));
  checkAngle(r.getAngle(), 0.5 * FastMath.PI);

  r = new Rotation(Vector3D.PLUS_J, FastMath.PI);
  checkVector(r.getAxis(), Vector3D.PLUS_J);
  checkAngle(r.getAngle(), FastMath.PI);

  checkVector(Rotation.IDENTITY.getAxis(), Vector3D.PLUS_I);

}
 
Example 10
Source File: CauchyDistribution.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double density(double x) {
    final double dev = x - median;
    return (1 / FastMath.PI) * (scale / (dev * dev + scale * scale));
}
 
Example 11
Source File: CauchyDistribution.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double cumulativeProbability(double x) {
    return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}
 
Example 12
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public double value(double[] point) {
    final double x = point[0], y = point[1];
    final double twoS2 = 2.0 * std * std;
    return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2);
}
 
Example 13
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 14
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public double value(double[] point) {
    final double x = point[0], y = point[1];
    final double twoS2 = 2.0 * std * std;
    return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2);
}
 
Example 15
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(new double[] { 0, 0, 0, 0, 0 })
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 98.680, 47.345 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertTrue(optimizer.getEvaluations() < 10);

    double rms = optimizer.computeRMS(optimum);
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms, 1e-10);

    Vector2D center = new Vector2D(optimum[0], optimum[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(), 1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(), 1e-6);

    double[][] cov = optimizer.computeCovariances(optimum, 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }

    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimizer = optimizer.withTarget(target).withWeight(new DiagonalMatrix(weights));
    optimum = optimizer.optimize().getPoint();

    cov = optimizer.computeCovariances(optimum, 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 16
Source File: NPEfix_00177_t.java    From coming with MIT License 2 votes vote down vote up
/** Check if the instance is similar to another line.
 * <p>Lines are considered similar if they contain the same
 * points. This does not mean they are equal since they can have
 * opposite directions.</p>
 * @param line line to which instance should be compared
 * @return true if the lines are similar
 */
public boolean isSimilarTo(final Line line) {
    final double angle = Vector3D.angle(direction, line.direction);
    return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero);
}
 
Example 17
Source File: NPEfix_00162_s.java    From coming with MIT License 2 votes vote down vote up
/** Get the reverse of the instance.
 * <p>Get a line with reversed orientation with respect to the
 * instance. A new object is built, the instance is untouched.</p>
 * @return a new line, with orientation opposite to the instance orientation
 */
public Line getReverse() {
    return new Line((angle < FastMath.PI) ? (angle + FastMath.PI) : (angle - FastMath.PI),
                    -cos, -sin, -originOffset);
}
 
Example 18
Source File: NPEfix_00181_s.java    From coming with MIT License 2 votes vote down vote up
/** Check if the instance is similar to another line.
 * <p>Lines are considered similar if they contain the same
 * points. This does not mean they are equal since they can have
 * opposite directions.</p>
 * @param line line to which instance should be compared
 * @return true if the lines are similar
 */
public boolean isSimilarTo(final Line line) {
    final double angle = Vector3D.angle(direction, line.direction);
    return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero);
}
 
Example 19
Source File: NPEfix_00179_s.java    From coming with MIT License 2 votes vote down vote up
/** Check if the instance is similar to another line.
 * <p>Lines are considered similar if they contain the same
 * points. This does not mean they are equal since they can have
 * opposite directions.</p>
 * @param line line to which instance should be compared
 * @return true if the lines are similar
 */
public boolean isSimilarTo(final Line line) {
    final double angle = Vector3D.angle(direction, line.direction);
    return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero);
}
 
Example 20
Source File: NPEfix_00189_s.java    From coming with MIT License 2 votes vote down vote up
/** Check if the instance is similar to another line.
 * <p>Lines are considered similar if they contain the same
 * points. This does not mean they are equal since they can have
 * opposite directions.</p>
 * @param line line to which instance should be compared
 * @return true if the lines are similar
 */
public boolean isSimilarTo(final Line line) {
    final double angle = Vector3D.angle(direction, line.direction);
    return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero);
}