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