View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.complex;
18  
19  import org.hipparchus.UnitTestUtils;
20  import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
21  import org.hipparchus.analysis.integration.IterativeLegendreFieldGaussIntegrator;
22  import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
23  import org.hipparchus.util.Binary64;
24  import org.hipparchus.util.Binary64Field;
25  import org.hipparchus.util.MathUtils;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  
31  public class FieldComplexUnivariateIntegratorTest {
32  
33      private FieldComplexUnivariateIntegrator<Binary64> integrator;
34      private FieldComplex<Binary64> zero = FieldComplex.getZero(Binary64Field.getInstance());
35  
36      private FieldComplex<Binary64> buildComplex(final double r, final double i) {
37          return new FieldComplex<>(new Binary64(r), new Binary64(i));
38      }
39  
40      @Test
41      public void testZero() {
42          final FieldComplex<Binary64> start = buildComplex(-1.75,   4.0);
43          final FieldComplex<Binary64> end   = buildComplex( 1.5,  -12.0);
44          UnitTestUtils.assertEquals(zero,
45                                     integrator.integrate(1000, z -> zero, start, end),
46                                     1.0e-15);
47      }
48  
49      @Test
50      public void testIdentity() {
51          final FieldComplex<Binary64> end = buildComplex( 1.5, -12.0);
52          UnitTestUtils.assertEquals(end.multiply(end).multiply(0.5),
53                                     integrator.integrate(1000, z -> z, zero, end),
54                                     1.0e-15);
55      }
56  
57      @Test
58      public void testPolynomialStraightPath() {
59          @SuppressWarnings("unchecked")
60          final FieldPolynomialFunction<FieldComplex<Binary64>> polynomial =
61                          new FieldPolynomialFunction<>(new FieldComplex[] {
62                              buildComplex(1.25, 2.0), buildComplex(-3.25, 0.125), buildComplex(0.0, 3.0)
63                          });
64          final FieldComplex<Binary64> start = buildComplex(-1.75,   4.0);
65          final FieldComplex<Binary64> end   = buildComplex( 1.5,  -12.0);
66          UnitTestUtils.assertEquals(polynomial.integrate(start, end),
67                                     integrator.integrate(1000, polynomial, start, end),
68                                     1.0e-15);
69      }
70  
71      @SuppressWarnings("unchecked")
72      @Test
73      public void testPolynomialPolylinePath() {
74          final FieldPolynomialFunction<FieldComplex<Binary64>> polynomial =
75                          new FieldPolynomialFunction<>(new FieldComplex[] {
76                              buildComplex(1.25, 2.0), buildComplex(-3.25, 0.125), buildComplex(0.0, 3.0)
77                          });
78          final FieldComplex<Binary64> z0 = buildComplex(-1.75,  4.0);
79          final FieldComplex<Binary64> z1 = buildComplex( 1.00,  3.0);
80          final FieldComplex<Binary64> z2 = buildComplex( 6.00,  0.5);
81          final FieldComplex<Binary64> z3 = buildComplex( 6.00, -6.5);
82          final FieldComplex<Binary64> z4 = buildComplex( 1.5, -12.0);
83          UnitTestUtils.assertEquals(polynomial.integrate(z0, z4),
84                                     integrator.integrate(1000, polynomial, z0, z1, z2, z3, z4),
85                                     1.0e-15);
86      }
87  
88      @SuppressWarnings("unchecked")
89      @Test
90      public void testAroundPole() {
91          final FieldComplex<Binary64> pole = buildComplex(-2.0, -1.0);
92          final CalculusFieldUnivariateFunction<FieldComplex<Binary64>> f = z -> z.subtract(pole).reciprocal();
93          final FieldComplex<Binary64> z0 = buildComplex( 1,  0);
94          final FieldComplex<Binary64> z1 = buildComplex(-1,  2);
95          final FieldComplex<Binary64> z2 = buildComplex(-3,  2);
96          final FieldComplex<Binary64> z3 = buildComplex(-5,  0);
97          final FieldComplex<Binary64> z4 = buildComplex(-5, -2);
98          final FieldComplex<Binary64> z5 = buildComplex(-4, -4);
99          final FieldComplex<Binary64> z6 = buildComplex(-1, -4);
100         final FieldComplex<Binary64> z7 = buildComplex( 1, -2);
101         final FieldComplex<Binary64> z8 = buildComplex( 1,  0);
102         UnitTestUtils.assertEquals(buildComplex(0.0, MathUtils.TWO_PI),
103                                    integrator.integrate(1000, f, z0, z1, z2, z3, z4, z5, z6, z7, z8),
104                                    1.0e-15);
105     }
106 
107     @SuppressWarnings("unchecked")
108     @Test
109     public void testAroundRoot() {
110         final FieldComplex<Binary64> pole = buildComplex(-2.0, -1.0);
111         final CalculusFieldUnivariateFunction<FieldComplex<Binary64>> f = z -> z.subtract(pole);
112         final FieldComplex<Binary64> z0 = buildComplex( 1,  0);
113         final FieldComplex<Binary64> z1 = buildComplex(-1,  2);
114         final FieldComplex<Binary64> z2 = buildComplex(-3,  2);
115         final FieldComplex<Binary64> z3 = buildComplex(-5,  0);
116         final FieldComplex<Binary64> z4 = buildComplex(-5, -2);
117         final FieldComplex<Binary64> z5 = buildComplex(-4, -4);
118         final FieldComplex<Binary64> z6 = buildComplex(-1, -4);
119         final FieldComplex<Binary64> z7 = buildComplex( 1, -2);
120         final FieldComplex<Binary64> z8 = buildComplex( 1,  0);
121         UnitTestUtils.assertEquals(zero,
122                                    integrator.integrate(1000, f, z0, z1, z2, z3, z4, z5, z6, z7, z8),
123                                    1.0e-15);
124     }
125 
126     @Before
127     public void setUp() {
128         integrator = new FieldComplexUnivariateIntegrator<>(new IterativeLegendreFieldGaussIntegrator<>(Binary64Field.getInstance(),
129                                                                                                         24,
130                                                                                                         1.0e-12,
131                                                                                                         1.0e-12));
132     }
133 
134     @After
135     public void tearDown() {
136         integrator = null;
137     }
138 
139 }