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