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