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.legacy.exception.DimensionMismatchException;
22  import org.apache.commons.math4.legacy.exception.NoDataException;
23  import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
24  import org.apache.commons.math4.legacy.exception.NullArgumentException;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Test for class {@link StepFunction}.
30   */
31  public class StepFunctionTest {
32      private final double EPS = Math.ulp(1d);
33  
34      @Test(expected=NullArgumentException.class)
35      public void testPreconditions1() {
36          new StepFunction(null, new double[] {0, -1, -2});
37      }
38  
39      @Test(expected=NullArgumentException.class)
40      public void testPreconditions2() {
41          new StepFunction(new double[] {0, 1}, null);
42      }
43  
44      @Test(expected=NoDataException.class)
45      public void testPreconditions3() {
46          new StepFunction(new double[] {0}, new double[] {});
47      }
48  
49      @Test(expected=NoDataException.class)
50      public void testPreconditions4() {
51          new StepFunction(new double[] {}, new double[] {0});
52      }
53  
54      @Test(expected=DimensionMismatchException.class)
55      public void testPreconditions5() {
56          new StepFunction(new double[] {0, 1}, new double[] {0, -1, -2});
57      }
58  
59      @Test(expected=NonMonotonicSequenceException.class)
60      public void testPreconditions6() {
61          new StepFunction(new double[] {1, 0, 1}, new double[] {0, -1, -2});
62      }
63  
64      @Test
65      public void testSomeValues() {
66          final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
67          final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };
68  
69          final UnivariateFunction f = new StepFunction(x, y);
70  
71          Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
72          Assert.assertEquals(4, f.value(-10), EPS);
73          Assert.assertEquals(-1, f.value(-0.4), EPS);
74          Assert.assertEquals(-5.5, f.value(0), EPS);
75          Assert.assertEquals(0.4, f.value(2), EPS);
76          Assert.assertEquals(5.8, f.value(10), EPS);
77          Assert.assertEquals(51.2, f.value(30), EPS);
78          Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
79      }
80  
81      @Test
82      public void testEndpointBehavior() {
83          final double[] x = {0, 1, 2, 3};
84          final double[] xp = {-8, 1, 2, 3};
85          final double[] y = {1, 2, 3, 4};
86          final UnivariateFunction f = new StepFunction(x, y);
87          final UnivariateFunction fp = new StepFunction(xp, y);
88          Assert.assertEquals(f.value(-8), fp.value(-8), EPS);
89          Assert.assertEquals(f.value(-10), fp.value(-10), EPS);
90          Assert.assertEquals(f.value(0), fp.value(0), EPS);
91          Assert.assertEquals(f.value(0.5), fp.value(0.5), EPS);
92          for (int i = 0; i < x.length; i++) {
93             Assert.assertEquals(y[i], f.value(x[i]), EPS);
94             if (i > 0) {
95                 Assert.assertEquals(y[i - 1], f.value(x[i] - 0.5), EPS);
96             } else {
97                 Assert.assertEquals(y[0], f.value(x[i] - 0.5), EPS);
98             }
99          }
100     }
101 
102     @Test
103     public void testHeaviside() {
104         final UnivariateFunction h = new StepFunction(new double[] {-1, 0},
105                                                           new double[] {0, 1});
106 
107         Assert.assertEquals(0, h.value(Double.NEGATIVE_INFINITY), 0);
108         Assert.assertEquals(0, h.value(-Double.MAX_VALUE), 0);
109         Assert.assertEquals(0, h.value(-2), 0);
110         Assert.assertEquals(0, h.value(-Double.MIN_VALUE), 0);
111         Assert.assertEquals(1, h.value(0), 0);
112         Assert.assertEquals(1, h.value(2), 0);
113         Assert.assertEquals(1, h.value(Double.POSITIVE_INFINITY), 0);
114     }
115 }