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.  *      http://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.apache.commons.math4.legacy.analysis.function;

  18. import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
  19. import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
  20. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  21. import org.apache.commons.math4.core.jdkmath.JdkMath;

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

  60.     /**
  61.      * The sinc function, {@code sin(x) / x}.
  62.      */
  63.     public Sinc() {
  64.         this(false);
  65.     }

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

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

  88.     /** {@inheritDoc}
  89.      * @since 3.1
  90.      */
  91.     @Override
  92.     public DerivativeStructure value(final DerivativeStructure t)
  93.         throws DimensionMismatchException {

  94.         final double scaledX  = (normalized ? JdkMath.PI : 1) * t.getValue();
  95.         final double scaledX2 = scaledX * scaledX;

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

  97.         if (JdkMath.abs(scaledX) <= SHORTCUT) {

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

  111.             final double inv = 1 / scaledX;
  112.             final double cos = JdkMath.cos(scaledX);
  113.             final double sin = JdkMath.sin(scaledX);

  114.             f[0] = inv * sin;

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

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

  129.                 double s = 0;
  130.                 double c = 0;

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

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

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

  148.                     // cosine part
  149.                     sc[k - 1] = (k - 1 - n) * sc[k - 1] + sc[k -2];
  150.                     c         = c * scaledX2 + sc[k - 1];
  151.                 }
  152.                 sc[0] *= -n;
  153.                 s      = s * scaledX2 + sc[0];

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

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

  165.         return t.compose(f);
  166.     }
  167. }