FieldComplexUnivariateIntegrator.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.Function;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  21. import org.hipparchus.analysis.integration.FieldUnivariateIntegrator;

  22. /**
  23.  * Wrapper to perform univariate complex integration using an underlying real integration algorithms.
  24.  * @param <T> the type of the field elements
  25.  * @since 2.0
  26.  */
  27. public class FieldComplexUnivariateIntegrator<T extends CalculusFieldElement<T>>  {

  28.     /** Underlying real integrator. */
  29.     private FieldUnivariateIntegrator<T> integrator;

  30.     /** Crate a complex integrator from a real integrator.
  31.      * @param integrator underlying real integrator to use
  32.      */
  33.     public FieldComplexUnivariateIntegrator(final FieldUnivariateIntegrator<T> integrator) {
  34.         this.integrator = integrator;
  35.     }

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

  48.         // linear mapping from real interval [0; 1] to function value along complex straight path from start to end
  49.         final FieldComplex<T>              rate   = end.subtract(start);
  50.         final Function<T, FieldComplex<T>> mapped = t -> f.value(start.add(rate.multiply(t)));

  51.         final T zero = start.getRealPart().getField().getZero();
  52.         final T one  = start.getRealPart().getField().getOne();

  53.         // integrate real and imaginary parts separately
  54.         final T real      = integrator.integrate(maxEval, t -> mapped.apply(t).getRealPart(),      zero, one);
  55.         final T imaginary = integrator.integrate(maxEval, t -> mapped.apply(t).getImaginaryPart(), zero, one);

  56.         // combine integrals
  57.         return new FieldComplex<>(real, imaginary).multiply(rate);

  58.     }

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

  81. }