Java Code Examples for org.apache.commons.math.util.FastMath#PI
The following examples show how to use
org.apache.commons.math.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: RombergIntegratorTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of integrator for the sine function. */ @Test public void testSinFunction() throws MathException { UnivariateRealFunction f = new SinFunction(); UnivariateRealIntegrator integrator = new RombergIntegrator(); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(f, min, max); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example 2
Source File: Vector3D.java From astor with GNU General Public License v2.0 | 6 votes |
/** Compute the angular separation between two vectors. * <p>This method computes the angular separation between two * vectors using the dot product for well separated vectors and the * cross product for almost aligned vectors. This allows to have a * good accuracy in all cases, even for vectors very close to each * other.</p> * @param v1 first vector * @param v2 second vector * @return angular separation between v1 and v2 * @exception ArithmeticException if either vector has a null norm */ public static double angle(Vector3D v1, Vector3D v2) { double normProduct = v1.getNorm() * v2.getNorm(); if (normProduct == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } double dot = dotProduct(v1, v2); double threshold = normProduct * 0.9999; if ((dot < -threshold) || (dot > threshold)) { // the vectors are almost aligned, compute using the sine Vector3D v3 = crossProduct(v1, v2); if (dot >= 0) { return FastMath.asin(v3.getNorm() / normProduct); } return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct); } // the vectors are sufficiently separated to use the cosine return FastMath.acos(dot / normProduct); }
Example 3
Source File: FastCosineTransformerTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of transformer for the sine function. */ @Test public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); FastCosineTransformer transformer = new FastCosineTransformer(); double min, max, result[], tolerance = 1E-12; int N = 9; double expected[] = { 0.0, 3.26197262739567, 0.0, -2.17958042710327, 0.0, -0.648846697642915, 0.0, -0.433545502649478, 0.0 }; min = 0.0; max = 2.0 * FastMath.PI * N / (N-1); result = transformer.transform(f, min, max, N); for (int i = 0; i < N; i++) { Assert.assertEquals(expected[i], result[i], tolerance); } min = -FastMath.PI; max = FastMath.PI * (N+1) / (N-1); result = transformer.transform(f, min, max, N); for (int i = 0; i < N; i++) { Assert.assertEquals(-expected[i], result[i], tolerance); } }
Example 4
Source File: Line.java From astor with GNU General Public License v2.0 | 6 votes |
/** Reset the instance as if built from two points. * <p>The line is oriented from p1 to p2</p> * @param p1 first point * @param p2 second point */ public void reset(final Vector2D p1, final Vector2D p2) { final double dx = p2.getX() - p1.getX(); final double dy = p2.getY() - p1.getY(); final double d = FastMath.hypot(dx, dy); if (d == 0.0) { angle = 0.0; cos = 1.0; sin = 0.0; originOffset = p1.getY(); } else { angle = FastMath.PI + FastMath.atan2(-dy, -dx); cos = FastMath.cos(angle); sin = FastMath.sin(angle); originOffset = (p2.getX() * p1.getY() - p1.getX() * p2.getY()) / d; } }
Example 5
Source File: RiddersSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the sine function. */ @Test public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new RiddersSolver(); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -1.0; max = 1.5; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example 6
Source File: RiddersSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the sine function. */ @Test public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new RiddersSolver(); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -1.0; max = 1.5; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example 7
Source File: FirstOrderIntegratorWithJacobiansTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testFinalResult() throws IntegratorException, DerivativeException { FirstOrderIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-10, 1.0e-10 }, new double[] { 1.0e-10, 1.0e-10 }); double[] y = new double[] { 0.0, 1.0 }; Circle circle = new Circle(y, 1.0, 1.0, 0.1); double[][] dydy0 = new double[2][2]; double[][] dydp = new double[2][3]; double t = 18 * FastMath.PI; FirstOrderIntegratorWithJacobians extInt = new FirstOrderIntegratorWithJacobians(integ, circle); extInt.integrate(0, y, circle.exactDyDp(0), t, y, dydy0, dydp); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(circle.exactY(t)[i], y[i], 1.0e-9); } for (int i = 0; i < dydy0.length; ++i) { for (int j = 0; j < dydy0[i].length; ++j) { Assert.assertEquals(circle.exactDyDy0(t)[i][j], dydy0[i][j], 1.0e-9); } } for (int i = 0; i < dydp.length; ++i) { for (int j = 0; j < dydp[i].length; ++j) { Assert.assertEquals(circle.exactDyDp(t)[i][j], dydp[i][j], 1.0e-7); } } }
Example 8
Source File: BigFractionTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSerial() throws FractionConversionException { BigFraction[] fractions = { new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO, new BigFraction(17), new BigFraction(FastMath.PI, 1000), new BigFraction(-5, 2) }; for (BigFraction fraction : fractions) { assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); } }
Example 9
Source File: ArgumentOutsideDomainExceptionTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testConstructor(){ ArgumentOutsideDomainException ex = new ArgumentOutsideDomainException(FastMath.PI, 10.0, 20.0); assertNull(ex.getCause()); assertNotNull(ex.getMessage()); assertTrue(ex.getMessage().indexOf("3.14") > 0); assertEquals(FastMath.PI, ex.getArgument()[0], 0); assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH))); }
Example 10
Source File: JGenProg2015_007_t.java From coming with MIT License | 5 votes |
/** * <p>Computes the n-th roots of this complex number. * </p> * <p>The nth roots are defined by the formula: <pre> * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))</code></pre> * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number. * </p> * <p>If one or both parts of this complex number is NaN, a list with just one element, * {@link #NaN} is returned.</p> * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element * list containing {@link #INF}.</p> * * @param n degree of root * @return List<Complex> all nth roots of this complex number * @throws IllegalArgumentException if parameter n is less than or equal to 0 * @since 2.0 */ public List<Complex> nthRoot(int n) throws IllegalArgumentException { if (n <= 0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N, n); } List<Complex> result = new ArrayList<Complex>(); if (isNaN) { result.add(NaN); return result; } if (isInfinite()) { result.add(INF); return result; } // nth root of abs -- faster / more accurate to use a solver here? final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n); // Compute nth roots of complex number with k = 0, 1, ... n-1 final double nthPhi = getArgument()/n; final double slice = 2 * FastMath.PI / n; double innerPart = nthPhi; for (int k = 0; k < n ; k++) { // inner part final double realPart = nthRootOfAbs * FastMath.cos(innerPart); final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart); result.add(createComplex(realPart, imaginaryPart)); innerPart += slice; } return result; }
Example 11
Source File: FastFourierTransformerTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Test of transformer for the sine function. */ @Test public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); FastFourierTransformer transformer = new FastFourierTransformer(); Complex result[]; int N = 1 << 8; double min, max, tolerance = 1E-12; min = 0.0; max = 2.0 * FastMath.PI; result = transformer.transform(f, min, max, N); Assert.assertEquals(0.0, result[1].getReal(), tolerance); Assert.assertEquals(-(N >> 1), result[1].getImaginary(), tolerance); Assert.assertEquals(0.0, result[N-1].getReal(), tolerance); Assert.assertEquals(N >> 1, result[N-1].getImaginary(), tolerance); for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) { Assert.assertEquals(0.0, result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } min = -FastMath.PI; max = FastMath.PI; result = transformer.inversetransform(f, min, max, N); Assert.assertEquals(0.0, result[1].getReal(), tolerance); Assert.assertEquals(-0.5, result[1].getImaginary(), tolerance); Assert.assertEquals(0.0, result[N-1].getReal(), tolerance); Assert.assertEquals(0.5, result[N-1].getImaginary(), tolerance); for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) { Assert.assertEquals(0.0, result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } }
Example 12
Source File: arja8_eigth_s.java From coming with MIT License | 5 votes |
/** * <p>Computes the n-th roots of this complex number. * </p> * <p>The nth roots are defined by the formula: <pre> * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))</code></pre> * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number. * </p> * <p>If one or both parts of this complex number is NaN, a list with just one element, * {@link #NaN} is returned.</p> * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element * list containing {@link #INF}.</p> * * @param n degree of root * @return List<Complex> all nth roots of this complex number * @throws IllegalArgumentException if parameter n is less than or equal to 0 * @since 2.0 */ public List<Complex> nthRoot(int n) throws IllegalArgumentException { if (n <= 0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N, n); } List<Complex> result = new ArrayList<Complex>(); if (isNaN) { result.add(NaN); return result; } if (isInfinite()) { result.add(INF); return result; } // nth root of abs -- faster / more accurate to use a solver here? final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n); // Compute nth roots of complex number with k = 0, 1, ... n-1 final double nthPhi = getArgument()/n; final double slice = 2 * FastMath.PI / n; double innerPart = nthPhi; for (int k = 0; k < n ; k++) { // inner part final double realPart = nthRootOfAbs * FastMath.cos(innerPart); final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart); result.add(createComplex(realPart, imaginaryPart)); innerPart += slice; } return result; }
Example 13
Source File: Math_53_Complex_s.java From coming with MIT License | 5 votes |
/** * <p>Computes the n-th roots of this complex number. * </p> * <p>The nth roots are defined by the formula: <pre> * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))</code></pre> * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number. * </p> * <p>If one or both parts of this complex number is NaN, a list with just one element, * {@link #NaN} is returned.</p> * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element * list containing {@link #INF}.</p> * * @param n degree of root * @return List<Complex> all nth roots of this complex number * @throws IllegalArgumentException if parameter n is less than or equal to 0 * @since 2.0 */ public List<Complex> nthRoot(int n) throws IllegalArgumentException { if (n <= 0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N, n); } List<Complex> result = new ArrayList<Complex>(); if (isNaN) { result.add(NaN); return result; } if (isInfinite()) { result.add(INF); return result; } // nth root of abs -- faster / more accurate to use a solver here? final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n); // Compute nth roots of complex number with k = 0, 1, ... n-1 final double nthPhi = getArgument()/n; final double slice = 2 * FastMath.PI / n; double innerPart = nthPhi; for (int k = 0; k < n ; k++) { // inner part final double realPart = nthRootOfAbs * FastMath.cos(innerPart); final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart); result.add(createComplex(realPart, imaginaryPart)); innerPart += slice; } return result; }
Example 14
Source File: Line.java From astor with GNU General Public License v2.0 | 5 votes |
/** Revert the instance. */ public void revertSelf() { if (angle < FastMath.PI) { angle += FastMath.PI; } else { angle -= FastMath.PI; } cos = -cos; sin = -sin; originOffset = -originOffset; }
Example 15
Source File: PolygonsSetTest.java From astor with GNU General Public License v2.0 | 4 votes |
@Test public void testLineIntersection() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0), new Vector2D( 1.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set = buildSet(vertices); Line l1 = new Line(new Vector2D(-1.5, 0.0), FastMath.PI / 4); SubLine s1 = (SubLine) set.intersection(l1.wholeHyperplane()); List<Interval> i1 = ((IntervalsSet) s1.getRemainingRegion()).asList(); Assert.assertEquals(2, i1.size()); Interval v10 = i1.get(0); Vector2D p10Lower = l1.toSpace(new Vector1D(v10.getLower())); Assert.assertEquals(0.0, p10Lower.getX(), 1.0e-10); Assert.assertEquals(1.5, p10Lower.getY(), 1.0e-10); Vector2D p10Upper = l1.toSpace(new Vector1D(v10.getUpper())); Assert.assertEquals(0.5, p10Upper.getX(), 1.0e-10); Assert.assertEquals(2.0, p10Upper.getY(), 1.0e-10); Interval v11 = i1.get(1); Vector2D p11Lower = l1.toSpace(new Vector1D(v11.getLower())); Assert.assertEquals(1.0, p11Lower.getX(), 1.0e-10); Assert.assertEquals(2.5, p11Lower.getY(), 1.0e-10); Vector2D p11Upper = l1.toSpace(new Vector1D(v11.getUpper())); Assert.assertEquals(1.5, p11Upper.getX(), 1.0e-10); Assert.assertEquals(3.0, p11Upper.getY(), 1.0e-10); Line l2 = new Line(new Vector2D(-1.0, 2.0), 0); SubLine s2 = (SubLine) set.intersection(l2.wholeHyperplane()); List<Interval> i2 = ((IntervalsSet) s2.getRemainingRegion()).asList(); Assert.assertEquals(1, i2.size()); Interval v20 = i2.get(0); Vector2D p20Lower = l2.toSpace(new Vector1D(v20.getLower())); Assert.assertEquals(1.0, p20Lower.getX(), 1.0e-10); Assert.assertEquals(2.0, p20Lower.getY(), 1.0e-10); Vector2D p20Upper = l2.toSpace(new Vector1D(v20.getUpper())); Assert.assertEquals(3.0, p20Upper.getX(), 1.0e-10); Assert.assertEquals(2.0, p20Upper.getY(), 1.0e-10); }
Example 16
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 4 votes |
public void testCircleFitting() throws FunctionEvaluationException { Circle circle = new Circle(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); VectorialPointValuePair optimum = optimizer.optimize(circle, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); assertTrue(optimizer.getEvaluations() < 10); assertTrue(optimizer.getJacobianEvaluations() < 10); double rms = optimizer.getRMS(); assertEquals(1.768262623567235, FastMath.sqrt(circle.getN()) * rms, 1.0e-10); Point2D.Double center = new Point2D.Double(optimum.getPointRef()[0], optimum.getPointRef()[1]); assertEquals(69.96016176931406, circle.getRadius(center), 1.0e-10); assertEquals(96.07590211815305, center.x, 1.0e-10); assertEquals(48.13516790438953, center.y, 1.0e-10); double[][] cov = optimizer.getCovariances(); assertEquals(1.839, cov[0][0], 0.001); assertEquals(0.731, cov[0][1], 0.001); assertEquals(cov[0][1], cov[1][0], 1.0e-14); assertEquals(0.786, cov[1][1], 0.001); double[] errors = optimizer.guessParametersErrors(); assertEquals(1.384, errors[0], 0.001); assertEquals(0.905, errors[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.x + r * FastMath.cos(d), center.y + r * FastMath.sin(d)); } double[] target = new double[circle.getN()]; Arrays.fill(target, 0.0); double[] weights = new double[circle.getN()]; Arrays.fill(weights, 2.0); optimizer.optimize(circle, target, weights, new double[] { 98.680, 47.345 }); cov = optimizer.getCovariances(); assertEquals(0.0016, cov[0][0], 0.001); assertEquals(3.2e-7, cov[0][1], 1.0e-9); assertEquals(cov[0][1], cov[1][0], 1.0e-14); assertEquals(0.0016, cov[1][1], 0.001); errors = optimizer.guessParametersErrors(); assertEquals(0.004, errors[0], 0.001); assertEquals(0.004, errors[1], 0.001); }
Example 17
Source File: Line.java From astor with GNU General Public License v2.0 | 2 votes |
/** 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: Plane.java From astor with GNU General Public License v2.0 | 2 votes |
/** Check if the instance is similar to another plane. * <p>Planes are considered similar if they contain the same * points. This does not mean they are equal since they can have * opposite normals.</p> * @param plane plane to which the instance is compared * @return true if the planes are similar */ public boolean isSimilarTo(final Plane plane) { final double angle = Vector3D.angle(w, plane.w); return ((angle < 1.0e-10) && (FastMath.abs(originOffset - plane.originOffset) < 1.0e-10)) || ((angle > (FastMath.PI - 1.0e-10)) && (FastMath.abs(originOffset + plane.originOffset) < 1.0e-10)); }
Example 19
Source File: Line.java From astor with GNU General Public License v2.0 | 2 votes |
/** 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: Line.java From astor with GNU General Public License v2.0 | 2 votes |
/** 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); }