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, () -> {
42              new StepFunction(null, new double[]{0, -1, -2});
43          });
44      }
45  
46      @Test
47      void testPreconditions2() {
48          assertThrows(NullArgumentException.class, () -> {
49              new StepFunction(new double[]{0, 1}, null);
50          });
51      }
52  
53      @Test
54      void testPreconditions3() {
55          assertThrows(MathIllegalArgumentException.class, () -> {
56              new StepFunction(new double[]{0}, new double[]{});
57          });
58      }
59  
60      @Test
61      void testPreconditions4() {
62          assertThrows(MathIllegalArgumentException.class, () -> {
63              new StepFunction(new double[]{}, new double[]{0});
64          });
65      }
66  
67      @Test
68      void testPreconditions5() {
69          assertThrows(MathIllegalArgumentException.class, () -> {
70              new StepFunction(new double[]{0, 1}, new double[]{0, -1, -2});
71          });
72      }
73  
74      @Test
75      void testPreconditions6() {
76          assertThrows(MathIllegalArgumentException.class, () -> {
77              new StepFunction(new double[]{1, 0, 1}, new double[]{0, -1, -2});
78          });
79      }
80  
81      @Test
82      void testSomeValues() {
83          final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
84          final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };
85  
86          final UnivariateFunction f = new StepFunction(x, y);
87  
88          assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
89          assertEquals(4, f.value(-10), EPS);
90          assertEquals(-1, f.value(-0.4), EPS);
91          assertEquals(-5.5, f.value(0), EPS);
92          assertEquals(0.4, f.value(2), EPS);
93          assertEquals(5.8, f.value(10), EPS);
94          assertEquals(51.2, f.value(30), EPS);
95          assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
96      }
97  
98      @Test
99      void testEndpointBehavior() {
100         final double[] x = {0, 1, 2, 3};
101         final double[] xp = {-8, 1, 2, 3};
102         final double[] y = {1, 2, 3, 4};
103         final UnivariateFunction f = new StepFunction(x, y);
104         final UnivariateFunction fp = new StepFunction(xp, y);
105         assertEquals(f.value(-8), fp.value(-8), EPS);
106         assertEquals(f.value(-10), fp.value(-10), EPS);
107         assertEquals(f.value(0), fp.value(0), EPS);
108         assertEquals(f.value(0.5), fp.value(0.5), EPS);
109         for (int i = 0; i < x.length; i++) {
110            assertEquals(y[i], f.value(x[i]), EPS);
111            if (i > 0) {
112                assertEquals(y[i - 1], f.value(x[i] - 0.5), EPS);
113            } else {
114                assertEquals(y[0], f.value(x[i] - 0.5), EPS);
115            }
116         }
117     }
118 
119     @Test
120     void testHeaviside() {
121         final UnivariateFunction h = new StepFunction(new double[] {-1, 0},
122                                                           new double[] {0, 1});
123 
124         assertEquals(0, h.value(Double.NEGATIVE_INFINITY), 0);
125         assertEquals(0, h.value(-Double.MAX_VALUE), 0);
126         assertEquals(0, h.value(-2), 0);
127         assertEquals(0, h.value(-Double.MIN_VALUE), 0);
128         assertEquals(1, h.value(0), 0);
129         assertEquals(1, h.value(2), 0);
130         assertEquals(1, h.value(Double.POSITIVE_INFINITY), 0);
131     }
132 }