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.analysis.UnivariateFunction;
25 import org.hipparchus.analysis.function.Cos;
26 import org.hipparchus.analysis.function.Inverse;
27 import org.hipparchus.analysis.function.Log;
28 import org.hipparchus.exception.LocalizedCoreFormats;
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.junit.Assert;
31 import org.junit.Test;
32
33
34
35
36
37 public class LegendreTest {
38 private static final GaussIntegratorFactory factory = new GaussIntegratorFactory();
39
40 @Test
41 public void testTooLArgeNumberOfPoints() {
42 try {
43 factory.legendre(10000, 0, Math.PI / 2);
44 Assert.fail("an exception should have been thrown");
45 } catch (MathIllegalArgumentException miae) {
46 Assert.assertEquals(LocalizedCoreFormats.NUMBER_TOO_LARGE, miae.getSpecifier());
47 Assert.assertEquals(10000, ((Integer) miae.getParts()[0]).intValue());
48 Assert.assertEquals(1000, ((Integer) miae.getParts()[1]).intValue());
49 }
50 }
51
52 @Test
53 public void testCos() {
54 final UnivariateFunction cos = new Cos();
55
56 final GaussIntegrator integrator = factory.legendre(7, 0, Math.PI / 2);
57 final double s = integrator.integrate(cos);
58
59 Assert.assertEquals(1, s, Math.ulp(1d));
60 }
61
62
63 @Test
64 public void testInverse() {
65 final UnivariateFunction inv = new Inverse();
66 final UnivariateFunction log = new Log();
67
68 final double lo = 12.34;
69 final double hi = 456.78;
70
71 final GaussIntegrator integrator = factory.legendre(60, lo, hi);
72 final double s = integrator.integrate(inv);
73 final double expected = log.value(hi) - log.value(lo);
74
75 Assert.assertEquals(expected, s, 1e-14);
76 }
77 }