ComplexUnivariateIntegrator.java

  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. import java.util.function.DoubleFunction;

  19. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  20. import org.hipparchus.analysis.integration.UnivariateIntegrator;

  21. /**
  22.  * Wrapper to perform univariate complex integration using an underlying real integration algorithms.
  23.  * @since 2.0
  24.  */
  25. public class ComplexUnivariateIntegrator  {

  26.     /** Underlying real integrator. */
  27.     private UnivariateIntegrator integrator;

  28.     /** Crate a complex integrator from a real integrator.
  29.      * @param integrator underlying real integrator to use
  30.      */
  31.     public ComplexUnivariateIntegrator(final UnivariateIntegrator integrator) {
  32.         this.integrator = integrator;
  33.     }

  34.     /**
  35.      * Integrate a function along a straight path between points.
  36.      *
  37.      * @param maxEval maximum number of evaluations (real and imaginary
  38.      * parts are evaluated separately, so up to twice this number may be used)
  39.      * @param f the integrand function
  40.      * @param start start point of the integration path
  41.      * @param end end point of the integration path
  42.      * @return the value of integral along the straight path
  43.      */
  44.     public Complex integrate(final int maxEval, final CalculusFieldUnivariateFunction<Complex> f,
  45.                              final Complex start, final Complex end) {

  46.         // linear mapping from real interval [0; 1] to function value along complex straight path from start to end
  47.         final Complex                 rate   = end.subtract(start);
  48.         final DoubleFunction<Complex> mapped = t -> f.value(start.add(rate.multiply(t)));

  49.         // integrate real and imaginary parts separately
  50.         final double real      = integrator.integrate(maxEval, t -> mapped.apply(t).getRealPart(),      0.0, 1.0);
  51.         final double imaginary = integrator.integrate(maxEval, t -> mapped.apply(t).getImaginaryPart(), 0.0, 1.0);

  52.         // combine integrals
  53.         return new Complex(real, imaginary).multiply(rate);

  54.     }

  55.     /**
  56.      * Integrate a function along a polyline path between any number of points.
  57.      *
  58.      * @param maxEval maximum number of evaluations (real and imaginary
  59.      * parts are evaluated separately and each path segments are also evaluated
  60.      * separately, so up to 2n times this number may be used for n segments)
  61.      * @param f the integrand function
  62.      * @param start start point of the integration path
  63.      * @param path successive points defining the path vertices
  64.      * @return the value of integral along the polyline path
  65.      */
  66.     public Complex integrate(final int maxEval, final CalculusFieldUnivariateFunction<Complex> f,
  67.                              final Complex start, final Complex...path) {
  68.         Complex sum      = Complex.ZERO;
  69.         Complex previous = start;
  70.         for (final Complex current : path) {
  71.             sum = sum.add(integrate(maxEval, f, previous, current));
  72.             previous = current;
  73.         }
  74.         return sum;
  75.     }

  76. }