1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.integration.gauss;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.Binary64;
27 import org.hipparchus.util.Binary64Field;
28 import org.hipparchus.util.FastMath;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.fail;
33
34
35
36
37
38 class FieldLegendreTest {
39 private static final FieldGaussIntegratorFactory<Binary64> factory = new FieldGaussIntegratorFactory<>(Binary64Field.getInstance());
40
41 @Test
42 void testTooLArgeNumberOfPoints() {
43 try {
44 factory.legendre(10000, new Binary64(0), new Binary64(Math.PI / 2));
45 fail("an exception should have been thrown");
46 } catch (MathIllegalArgumentException miae) {
47 assertEquals(LocalizedCoreFormats.NUMBER_TOO_LARGE, miae.getSpecifier());
48 assertEquals(10000, ((Integer) miae.getParts()[0]).intValue());
49 assertEquals(1000, ((Integer) miae.getParts()[1]).intValue());
50 }
51 }
52
53 @Test
54 void testCos() {
55 final FieldGaussIntegrator<Binary64> integrator = factory.legendre(7, new Binary64(0), new Binary64(Math.PI / 2));
56 final double s = integrator.integrate(x -> FastMath.cos(x)).getReal();
57
58 assertEquals(1, s, Math.ulp(1d));
59 }
60
61
62 @Test
63 void testInverse() {
64
65 final Binary64 lo = new Binary64(12.34);
66 final Binary64 hi = new Binary64(456.78);
67
68 final FieldGaussIntegrator<Binary64> integrator = factory.legendre(60, lo, hi);
69 final double s = integrator.integrate(x -> x.reciprocal()).getReal();
70 final double expected = FastMath.log(hi).subtract(FastMath.log(lo)).getReal();
71
72 assertEquals(expected, s, 1e-14);
73 }
74 }