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  
18  package org.apache.commons.math4.legacy.analysis.function;
19  
20  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Test for all classes in org.apache.commons.math4.legacy.analysis.function that implement UnivariateFunction explicitly.
27   */
28  public class UnivariateFunctionTest {
29  
30  
31      private final double EPS = Math.ulp(1d);
32  
33      @Test
34      public void testAbs() {
35          Abs abs = new Abs();
36          Assert.assertEquals(5, abs.value(-5), EPS);
37          Assert.assertEquals(5, abs.value(5), EPS);
38          Assert.assertEquals(5.123, abs.value(-5.123), EPS);
39          Assert.assertEquals(5.123, abs.value(5.123), EPS);
40          Assert.assertEquals(0, abs.value(0), EPS);
41      }
42  
43      @Test
44      public void testCeil() {
45          Ceil ceil = new Ceil();
46          Assert.assertEquals(-5, ceil.value(-5), EPS);
47          Assert.assertEquals(-4, ceil.value(-4.999), EPS);
48          Assert.assertEquals(0, ceil.value(0), EPS);
49          Assert.assertEquals(1, ceil.value(1), EPS);
50          Assert.assertEquals(2, ceil.value(1.000000001), EPS);
51      }
52  
53      @Test
54      public void testFloor() {
55          Floor floor = new Floor();
56          Assert.assertEquals(-5, floor.value(-5), EPS);
57          Assert.assertEquals(-5, floor.value(-4.999), EPS);
58          Assert.assertEquals(0, floor.value(0), EPS);
59          Assert.assertEquals(1, floor.value(1), EPS);
60          Assert.assertEquals(1, floor.value(1.000000001), EPS);
61      }
62  
63      @Test
64      public void testRint() {
65          //Rint function is round half even.
66          Rint rint = new Rint();
67          Assert.assertEquals(-5, rint.value(-5), EPS);
68          Assert.assertEquals(-4, rint.value(-4.5), EPS);
69          Assert.assertEquals(0, rint.value(0), EPS);
70          Assert.assertEquals(1, rint.value(1), EPS);
71          Assert.assertEquals(2, rint.value(1.5), EPS);
72          Assert.assertEquals(2, rint.value(2.5), EPS);
73          Assert.assertEquals(-1, rint.value(-0.99999999), EPS);
74          Assert.assertEquals(11, rint.value(10.99999999), EPS);
75      }
76  
77      @Test
78      public void testSignum() {
79          Signum signum = new Signum();
80          Assert.assertEquals(-1, signum.value(-5), EPS);
81          Assert.assertEquals(-1, signum.value(-4.5), EPS);
82          Assert.assertEquals(0, signum.value(0), EPS);
83          Assert.assertEquals(-0, signum.value(-0), EPS);
84          Assert.assertEquals(1, signum.value(1), EPS);
85          Assert.assertEquals(1, signum.value(1.5), EPS);
86          Assert.assertEquals(1, signum.value(2.5), EPS);
87          Assert.assertEquals(-1, signum.value(-0.99999999), EPS);
88          Assert.assertEquals(1, signum.value(10.99999999), EPS);
89      }
90  
91      @Test
92      public void testStepFunction() {
93          final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
94          final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };
95  
96          final UnivariateFunction f = new StepFunction(x, y);
97  
98          Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
99          Assert.assertEquals(4, f.value(-10), EPS);
100         Assert.assertEquals(-1, f.value(-0.4), EPS);
101         Assert.assertEquals(-5.5, f.value(0), EPS);
102         Assert.assertEquals(0.4, f.value(2), EPS);
103         Assert.assertEquals(5.8, f.value(10), EPS);
104         Assert.assertEquals(51.2, f.value(30), EPS);
105         Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
106     }
107 
108     @Test
109     public void testUlp() {
110         Ulp ulp = new Ulp();
111         Assert.assertEquals(expectedUlp(1),ulp.value(1), EPS);
112         Assert.assertEquals(expectedUlp(1.123456789),ulp.value(1.123456789), EPS);
113         Assert.assertEquals(expectedUlp(-1),ulp.value(-1), EPS);
114         Assert.assertEquals(expectedUlp(-1.123456789),ulp.value(-1.123456789), EPS);
115         Assert.assertEquals(expectedUlp(0),ulp.value(0), EPS);
116         Assert.assertEquals(expectedUlp(500000000),ulp.value(500000000), EPS);
117         Assert.assertEquals(expectedUlp(-500000000),ulp.value(-500000000), EPS);
118     }
119 
120     private double expectedUlp(double x) {
121         return JdkMath.abs(x - Double.longBitsToDouble(Double.doubleToRawLongBits(x) ^ 1));
122     }
123 }