View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.function;
23  
24  import org.hipparchus.analysis.UnivariateFunction;
25  import org.hipparchus.analysis.differentiation.DSFactory;
26  import org.hipparchus.analysis.differentiation.DerivativeStructure;
27  import org.hipparchus.dfp.Dfp;
28  import org.hipparchus.dfp.DfpField;
29  import org.hipparchus.dfp.DfpMath;
30  import org.hipparchus.util.FastMath;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  
35  class SincTest {
36  
37      @Test
38      void testShortcut() {
39         final Sinc s = new Sinc();
40         final UnivariateFunction f = x -> {
41             Dfp dfpX = new DfpField(25).newDfp(x);
42             return DfpMath.sin(dfpX).divide(dfpX).toDouble();
43         };
44  
45         for (double x = 1e-30; x < 1e10; x *= 2) {
46             final double fX = f.value(x);
47             final double sX = s.value(x);
48             assertEquals(fX, sX, 2.0e-16, "x=" + x);
49         }
50     }
51  
52      @Test
53      void testCrossings() {
54         final Sinc s = new Sinc(true);
55         final int numCrossings = 1000;
56         final double tol = 2e-16;
57         for (int i = 1; i <= numCrossings; i++) {
58             assertEquals(0, s.value(i), tol, "i=" + i);
59         }
60     }
61  
62      @Test
63      void testZero() {
64         final Sinc s = new Sinc();
65         assertEquals(1d, s.value(0), 0);
66     }
67  
68      @Test
69      void testEuler() {
70         final Sinc s = new Sinc();
71         final double x = 123456.789;
72         double prod = 1;
73         double xOverPow2 = x / 2;
74         while (xOverPow2 > 0) {
75             prod *= FastMath.cos(xOverPow2);
76             xOverPow2 /= 2;
77         }
78         assertEquals(prod, s.value(x), 1e-13);
79     }
80  
81      @Test
82      void testDerivativeZero() {
83         final DerivativeStructure s0 = new Sinc(true).value(new DSFactory(1, 1).variable(0, 0.0));
84         assertEquals(0, s0.getPartialDerivative(1), 0);
85     }
86  
87      @Test
88      void testDerivatives1Dot2Unnormalized() {
89         DerivativeStructure s = new Sinc(false).value(new DSFactory(1, 5).variable(0, 1.2));
90         assertEquals( 0.77669923830602195806, s.getPartialDerivative(0), 1.0e-16);
91         assertEquals(-0.34528456985779031701, s.getPartialDerivative(1), 1.0e-16);
92         assertEquals(-0.2012249552097047631,  s.getPartialDerivative(2), 1.0e-16);
93         assertEquals( 0.2010975926270339262,  s.getPartialDerivative(3), 4.0e-16);
94         assertEquals( 0.106373929549242204,   s.getPartialDerivative(4), 1.0e-15);
95         assertEquals(-0.1412599110579478695,  s.getPartialDerivative(5), 3.0e-15);
96     }
97  
98      @Test
99      void testDerivatives1Dot2Normalized() {
100        DerivativeStructure s = new Sinc(true).value(new DSFactory(1, 5).variable(0, 1.2));
101        assertEquals(-0.15591488063143983888, s.getPartialDerivative(0), 6.0e-17);
102        assertEquals(-0.54425176145292298767, s.getPartialDerivative(1), 2.0e-16);
103        assertEquals(2.4459044611635856107,   s.getPartialDerivative(2), 9.0e-16);
104        assertEquals(0.5391369206235909586,   s.getPartialDerivative(3), 7.0e-16);
105        assertEquals(-16.984649869728849865,  s.getPartialDerivative(4), 8.0e-15);
106        assertEquals(5.0980327462666316586,   s.getPartialDerivative(5), 9.0e-15);
107    }
108 
109     @Test
110     void testDerivativeShortcut() {
111        final Sinc sinc = new Sinc();
112        final UnivariateFunction f = x -> {
113            Dfp dfpX = new DfpField(25).newDfp(x);
114            return DfpMath.cos(dfpX).subtract(DfpMath.sin(dfpX).divide(dfpX)).divide(dfpX).toDouble();
115        };
116 
117        DSFactory factory = new DSFactory(1, 1);
118        for (double x = 1e-30; x < 1e10; x *= 2) {
119            final double fX = f.value(x);
120            final DerivativeStructure sX = sinc.value(factory.variable(0, x));
121            assertEquals(fX, sX.getPartialDerivative(1), 3.0e-13, "x=" + x);
122        }
123    }
124 }