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