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.IterativeLegendreGaussIntegrator;
22  import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
23  import org.hipparchus.util.MathUtils;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  
29  public class ComplexUnivariateIntegratorTest {
30  
31      private ComplexUnivariateIntegrator integrator;
32  
33      @Test
34      public void testZero() {
35          final Complex start = new Complex(-1.75,   4.0);
36          final Complex end   = new Complex( 1.5,  -12.0);
37          UnitTestUtils.assertEquals(Complex.ZERO,
38                                     integrator.integrate(1000, z -> Complex.ZERO, start, end),
39                                     1.0e-15);
40      }
41  
42      @Test
43      public void testIdentity() {
44          final Complex end = new Complex( 1.5, -12.0);
45          UnitTestUtils.assertEquals(end.multiply(end).multiply(0.5),
46                                     integrator.integrate(1000, z -> z, Complex.ZERO, end),
47                                     1.0e-15);
48      }
49  
50      @Test
51      public void testPolynomialStraightPath() {
52          final FieldPolynomialFunction<Complex> polynomial =
53                          new FieldPolynomialFunction<>(new Complex[] {
54                              new Complex(1.25, 2.0), new Complex(-3.25, 0.125), new Complex(0.0, 3.0)
55                          });
56          final Complex start = new Complex(-1.75,   4.0);
57          final Complex end   = new Complex( 1.5,  -12.0);
58          UnitTestUtils.assertEquals(polynomial.integrate(start, end),
59                                     integrator.integrate(1000, polynomial, start, end),
60                                     1.0e-15);
61      }
62  
63      @Test
64      public void testPolynomialPolylinePath() {
65          final FieldPolynomialFunction<Complex> polynomial =
66                          new FieldPolynomialFunction<>(new Complex[] {
67                              new Complex(1.25, 2.0), new Complex(-3.25, 0.125), new Complex(0.0, 3.0)
68                          });
69          final Complex z0 = new Complex(-1.75,  4.0);
70          final Complex z1 = new Complex( 1.00,  3.0);
71          final Complex z2 = new Complex( 6.00,  0.5);
72          final Complex z3 = new Complex( 6.00, -6.5);
73          final Complex z4 = new Complex( 1.5, -12.0);
74          UnitTestUtils.assertEquals(polynomial.integrate(z0, z4),
75                                     integrator.integrate(1000, polynomial, z0, z1, z2, z3, z4),
76                                     1.0e-15);
77      }
78  
79      @Test
80      public void testAroundPole() {
81          final Complex pole = new Complex(-2.0, -1.0);
82          final CalculusFieldUnivariateFunction<Complex> f = z -> z.subtract(pole).reciprocal();
83          final Complex z0 = new Complex( 1,  0);
84          final Complex z1 = new Complex(-1,  2);
85          final Complex z2 = new Complex(-3,  2);
86          final Complex z3 = new Complex(-5,  0);
87          final Complex z4 = new Complex(-5, -2);
88          final Complex z5 = new Complex(-4, -4);
89          final Complex z6 = new Complex(-1, -4);
90          final Complex z7 = new Complex( 1, -2);
91          final Complex z8 = new Complex( 1,  0);
92          UnitTestUtils.assertEquals(new Complex(0.0, MathUtils.TWO_PI),
93                                     integrator.integrate(1000, f, z0, z1, z2, z3, z4, z5, z6, z7, z8),
94                                     1.0e-15);
95      }
96  
97      @Test
98      public void testAroundRoot() {
99          final Complex pole = new Complex(-2.0, -1.0);
100         final CalculusFieldUnivariateFunction<Complex> f = z -> z.subtract(pole);
101         final Complex z0 = new Complex( 1,  0);
102         final Complex z1 = new Complex(-1,  2);
103         final Complex z2 = new Complex(-3,  2);
104         final Complex z3 = new Complex(-5,  0);
105         final Complex z4 = new Complex(-5, -2);
106         final Complex z5 = new Complex(-4, -4);
107         final Complex z6 = new Complex(-1, -4);
108         final Complex z7 = new Complex( 1, -2);
109         final Complex z8 = new Complex( 1,  0);
110         UnitTestUtils.assertEquals(Complex.ZERO,
111                                    integrator.integrate(1000, f, z0, z1, z2, z3, z4, z5, z6, z7, z8),
112                                    1.0e-15);
113     }
114 
115     @Before
116     public void setUp() {
117         integrator = new ComplexUnivariateIntegrator(new IterativeLegendreGaussIntegrator(24,
118                                                                                           1.0e-12,
119                                                                                           1.0e-12));
120     }
121 
122     @After
123     public void tearDown() {
124         integrator = null;
125     }
126 
127 }