Sinc.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) 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 ASF 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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.analysis.function;

  22. import org.hipparchus.analysis.differentiation.Derivative;
  23. import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.SinCos;

  27. /**
  28.  * <a href="http://en.wikipedia.org/wiki/Sinc_function">Sinc</a> function,
  29.  * defined by
  30.  * <pre><code>
  31.  *   sinc(x) = 1            if x = 0,
  32.  *             sin(x) / x   otherwise.
  33.  * </code></pre>
  34.  *
  35.  */
  36. public class Sinc implements UnivariateDifferentiableFunction {
  37.     /**
  38.      * Value below which the computations are done using Taylor series.
  39.      * <p>
  40.      * The Taylor series for sinc even order derivatives are:
  41.      * <pre>
  42.      * d^(2n)sinc/dx^(2n)     = Sum_(k>=0) (-1)^(n+k) / ((2k)!(2n+2k+1)) x^(2k)
  43.      *                        = (-1)^n     [ 1/(2n+1) - x^2/(4n+6) + x^4/(48n+120) - x^6/(1440n+5040) + O(x^8) ]
  44.      * </pre>
  45.      * </p>
  46.      * <p>
  47.      * The Taylor series for sinc odd order derivatives are:
  48.      * <pre>
  49.      * d^(2n+1)sinc/dx^(2n+1) = Sum_(k>=0) (-1)^(n+k+1) / ((2k+1)!(2n+2k+3)) x^(2k+1)
  50.      *                        = (-1)^(n+1) [ x/(2n+3) - x^3/(12n+30) + x^5/(240n+840) - x^7/(10080n+45360) + O(x^9) ]
  51.      * </pre>
  52.      * </p>
  53.      * <p>
  54.      * So the ratio of the fourth term with respect to the first term
  55.      * is always smaller than x^6/720, for all derivative orders.
  56.      * This implies that neglecting this term and using only the first three terms induces
  57.      * a relative error bounded by x^6/720. The SHORTCUT value is chosen such that this
  58.      * relative error is below double precision accuracy when |x| <= SHORTCUT.
  59.      * </p>
  60.      */
  61.     private static final double SHORTCUT = 6.0e-3;
  62.     /** For normalized sinc function. */
  63.     private final boolean normalized;

  64.     /**
  65.      * The sinc function, {@code sin(x) / x}.
  66.      */
  67.     public Sinc() {
  68.         this(false);
  69.     }

  70.     /**
  71.      * Instantiates the sinc function.
  72.      *
  73.      * @param normalized If {@code true}, the function is
  74.      * <code> sin(&pi;x) / &pi;x</code>, otherwise {@code sin(x) / x}.
  75.      */
  76.     public Sinc(boolean normalized) {
  77.         this.normalized = normalized;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public double value(final double x) {
  82.         final double scaledX = normalized ? FastMath.PI * x : x;
  83.         if (FastMath.abs(scaledX) <= SHORTCUT) {
  84.             // use Taylor series
  85.             final double scaledX2 = scaledX * scaledX;
  86.             return ((scaledX2 - 20) * scaledX2 + 120) / 120;
  87.         } else {
  88.             // use definition expression
  89.             return FastMath.sin(scaledX) / scaledX;
  90.         }
  91.     }

  92.     /** {@inheritDoc}
  93.      */
  94.     @Override
  95.     public <T extends Derivative<T>> T value(T t)
  96.         throws MathIllegalArgumentException {

  97.         final double scaledX   = (normalized ? FastMath.PI : 1) * t.getValue();
  98.         final double scaledX2  = scaledX * scaledX;

  99.         double[] f = new double[t.getOrder() + 1];

  100.         if (FastMath.abs(scaledX) <= SHORTCUT) {

  101.             for (int i = 0; i < f.length; ++i) {
  102.                 final int k = i / 2;
  103.                 if ((i & 0x1) == 0) {
  104.                     // even derivation order
  105.                     f[i] = (((k & 0x1) == 0) ? 1 : -1) *
  106.                            (1.0 / (i + 1) - scaledX2 * (1.0 / (2 * i + 6) - scaledX2 / (24 * i + 120)));
  107.                 } else {
  108.                     // odd derivation order
  109.                     f[i] = (((k & 0x1) == 0) ? -scaledX : scaledX) *
  110.                            (1.0 / (i + 2) - scaledX2 * (1.0 / (6 * i + 24) - scaledX2 / (120 * i + 720)));
  111.                 }
  112.             }

  113.         } else {

  114.             final double inv    = 1 / scaledX;
  115.             final SinCos sinCos = FastMath.sinCos(scaledX);

  116.             f[0] = inv * sinCos.sin();

  117.             // the nth order derivative of sinc has the form:
  118.             // dn(sinc(x)/dxn = [S_n(x) sin(x) + C_n(x) cos(x)] / x^(n+1)
  119.             // where S_n(x) is an even polynomial with degree n-1 or n (depending on parity)
  120.             // and C_n(x) is an odd polynomial with degree n-1 or n (depending on parity)
  121.             // S_0(x) = 1, S_1(x) = -1, S_2(x) = -x^2 + 2, S_3(x) = 3x^2 - 6...
  122.             // C_0(x) = 0, C_1(x) = x, C_2(x) = -2x, C_3(x) = -x^3 + 6x...
  123.             // the general recurrence relations for S_n and C_n are:
  124.             // S_n(x) = x S_(n-1)'(x) - n S_(n-1)(x) - x C_(n-1)(x)
  125.             // C_n(x) = x C_(n-1)'(x) - n C_(n-1)(x) + x S_(n-1)(x)
  126.             // as per polynomials parity, we can store both S_n and C_n in the same array
  127.             final double[] sc = new double[f.length];
  128.             sc[0] = 1;

  129.             double coeff = inv;
  130.             for (int n = 1; n < f.length; ++n) {

  131.                 double s = 0;
  132.                 double c = 0;

  133.                 // update and evaluate polynomials S_n(x) and C_n(x)
  134.                 final int kStart;
  135.                 if ((n & 0x1) == 0) {
  136.                     // even derivation order, S_n is degree n and C_n is degree n-1
  137.                     sc[n] = 0;
  138.                     kStart = n;
  139.                 } else {
  140.                     // odd derivation order, S_n is degree n-1 and C_n is degree n
  141.                     sc[n] = sc[n - 1];
  142.                     c = sc[n];
  143.                     kStart = n - 1;
  144.                 }

  145.                 // in this loop, k is always even
  146.                 for (int k = kStart; k > 1; k -= 2) {

  147.                     // sine part
  148.                     sc[k]     = (k - n) * sc[k] - sc[k - 1];
  149.                     s         = s * scaledX2 + sc[k];

  150.                     // cosine part
  151.                     sc[k - 1] = (k - 1 - n) * sc[k - 1] + sc[k -2];
  152.                     c         = c * scaledX2 + sc[k - 1];

  153.                 }
  154.                 sc[0] *= -n;
  155.                 s      = s * scaledX2 + sc[0];

  156.                 coeff *= inv;
  157.                 f[n]   = coeff * (s * sinCos.sin() + c * scaledX * sinCos.cos());

  158.             }

  159.         }

  160.         if (normalized) {
  161.             double scale = FastMath.PI;
  162.             for (int i = 1; i < f.length; ++i) {
  163.                 f[i]  *= scale;
  164.                 scale *= FastMath.PI;
  165.             }
  166.         }

  167.         return t.compose(f);

  168.     }

  169. }