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 java.util.function.DoubleFunction;
20  
21  import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
22  import org.hipparchus.analysis.integration.UnivariateIntegrator;
23  
24  /**
25   * Wrapper to perform univariate complex integration using an underlying real integration algorithms.
26   * @since 2.0
27   */
28  public class ComplexUnivariateIntegrator  {
29  
30      /** Underlying real integrator. */
31      private UnivariateIntegrator integrator;
32  
33      /** Crate a complex integrator from a real integrator.
34       * @param integrator underlying real integrator to use
35       */
36      public ComplexUnivariateIntegrator(final UnivariateIntegrator integrator) {
37          this.integrator = integrator;
38      }
39  
40      /**
41       * Integrate a function along a straight path between points.
42       *
43       * @param maxEval maximum number of evaluations (real and imaginary
44       * parts are evaluated separately, so up to twice this number may be used)
45       * @param f the integrand function
46       * @param start start point of the integration path
47       * @param end end point of the integration path
48       * @return the value of integral along the straight path
49       */
50      public Complex integrate(final int maxEval, final CalculusFieldUnivariateFunction<Complex> f,
51                               final Complex start, final Complex end) {
52  
53          // linear mapping from real interval [0; 1] to function value along complex straight path from start to end
54          final Complex                 rate   = end.subtract(start);
55          final DoubleFunction<Complex> mapped = t -> f.value(start.add(rate.multiply(t)));
56  
57          // integrate real and imaginary parts separately
58          final double real      = integrator.integrate(maxEval, t -> mapped.apply(t).getRealPart(),      0.0, 1.0);
59          final double imaginary = integrator.integrate(maxEval, t -> mapped.apply(t).getImaginaryPart(), 0.0, 1.0);
60  
61          // combine integrals
62          return new Complex(real, imaginary).multiply(rate);
63  
64      }
65  
66      /**
67       * Integrate a function along a polyline path between any number of points.
68       *
69       * @param maxEval maximum number of evaluations (real and imaginary
70       * parts are evaluated separately and each path segments are also evaluated
71       * separately, so up to 2n times this number may be used for n segments)
72       * @param f the integrand function
73       * @param start start point of the integration path
74       * @param path successive points defining the path vertices
75       * @return the value of integral along the polyline path
76       */
77      public Complex integrate(final int maxEval, final CalculusFieldUnivariateFunction<Complex> f,
78                               final Complex start, final Complex...path) {
79          Complex sum      = Complex.ZERO;
80          Complex previous = start;
81          for (final Complex current : path) {
82              sum = sum.add(integrate(maxEval, f, previous, current));
83              previous = current;
84          }
85          return sum;
86      }
87  
88  }