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 = new UnivariateFunction() {
41             @Override
42             public double value(double x) {
43                 Dfp dfpX = new DfpField(25).newDfp(x);
44                 return DfpMath.sin(dfpX).divide(dfpX).toDouble();
45             }
46         };
47  
48         for (double x = 1e-30; x < 1e10; x *= 2) {
49             final double fX = f.value(x);
50             final double sX = s.value(x);
51             assertEquals(fX, sX, 2.0e-16, "x=" + x);
52         }
53     }
54  
55      @Test
56      void testCrossings() {
57         final Sinc s = new Sinc(true);
58         final int numCrossings = 1000;
59         final double tol = 2e-16;
60         for (int i = 1; i <= numCrossings; i++) {
61             assertEquals(0, s.value(i), tol, "i=" + i);
62         }
63     }
64  
65      @Test
66      void testZero() {
67         final Sinc s = new Sinc();
68         assertEquals(1d, s.value(0), 0);
69     }
70  
71      @Test
72      void testEuler() {
73         final Sinc s = new Sinc();
74         final double x = 123456.789;
75         double prod = 1;
76         double xOverPow2 = x / 2;
77         while (xOverPow2 > 0) {
78             prod *= FastMath.cos(xOverPow2);
79             xOverPow2 /= 2;
80         }
81         assertEquals(prod, s.value(x), 1e-13);
82     }
83  
84      @Test
85      void testDerivativeZero() {
86         final DerivativeStructure s0 = new Sinc(true).value(new DSFactory(1, 1).variable(0, 0.0));
87         assertEquals(0, s0.getPartialDerivative(1), 0);
88     }
89  
90      @Test
91      void testDerivatives1Dot2Unnormalized() {
92         DerivativeStructure s = new Sinc(false).value(new DSFactory(1, 5).variable(0, 1.2));
93         assertEquals( 0.77669923830602195806, s.getPartialDerivative(0), 1.0e-16);
94         assertEquals(-0.34528456985779031701, s.getPartialDerivative(1), 1.0e-16);
95         assertEquals(-0.2012249552097047631,  s.getPartialDerivative(2), 1.0e-16);
96         assertEquals( 0.2010975926270339262,  s.getPartialDerivative(3), 4.0e-16);
97         assertEquals( 0.106373929549242204,   s.getPartialDerivative(4), 1.0e-15);
98         assertEquals(-0.1412599110579478695,  s.getPartialDerivative(5), 3.0e-15);
99     }
100 
101     @Test
102     void testDerivatives1Dot2Normalized() {
103        DerivativeStructure s = new Sinc(true).value(new DSFactory(1, 5).variable(0, 1.2));
104        assertEquals(-0.15591488063143983888, s.getPartialDerivative(0), 6.0e-17);
105        assertEquals(-0.54425176145292298767, s.getPartialDerivative(1), 2.0e-16);
106        assertEquals(2.4459044611635856107,   s.getPartialDerivative(2), 9.0e-16);
107        assertEquals(0.5391369206235909586,   s.getPartialDerivative(3), 7.0e-16);
108        assertEquals(-16.984649869728849865,  s.getPartialDerivative(4), 8.0e-15);
109        assertEquals(5.0980327462666316586,   s.getPartialDerivative(5), 9.0e-15);
110    }
111 
112     @Test
113     void testDerivativeShortcut() {
114        final Sinc sinc = new Sinc();
115        final UnivariateFunction f = new UnivariateFunction() {
116                @Override
117             public double value(double x) {
118                    Dfp dfpX = new DfpField(25).newDfp(x);
119                    return DfpMath.cos(dfpX).subtract(DfpMath.sin(dfpX).divide(dfpX)).divide(dfpX).toDouble();
120                }
121            };
122 
123        DSFactory factory = new DSFactory(1, 1);
124        for (double x = 1e-30; x < 1e10; x *= 2) {
125            final double fX = f.value(x);
126            final DerivativeStructure sX = sinc.value(factory.variable(0, x));
127            assertEquals(fX, sX.getPartialDerivative(1), 3.0e-13, "x=" + x);
128        }
129    }
130 }