001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math3.analysis.function;
019
020 import org.apache.commons.math3.analysis.UnivariateFunction;
021 import org.apache.commons.math3.exception.DimensionMismatchException;
022 import org.apache.commons.math3.exception.NonMonotonicSequenceException;
023 import org.apache.commons.math3.exception.NullArgumentException;
024 import org.apache.commons.math3.exception.NoDataException;
025
026 import org.junit.Assert;
027 import org.junit.Test;
028
029 /**
030 * Test for class {@link StepFunction}.
031 */
032 public class StepFunctionTest {
033 private final double EPS = Math.ulp(1d);
034
035 @Test(expected=NullArgumentException.class)
036 public void testPreconditions1() {
037 new StepFunction(null, new double[] {0, -1, -2});
038 }
039
040 @Test(expected=NullArgumentException.class)
041 public void testPreconditions2() {
042 new StepFunction(new double[] {0, 1}, null);
043 }
044
045 @Test(expected=NoDataException.class)
046 public void testPreconditions3() {
047 new StepFunction(new double[] {0}, new double[] {});
048 }
049
050 @Test(expected=NoDataException.class)
051 public void testPreconditions4() {
052 new StepFunction(new double[] {}, new double[] {0});
053 }
054
055 @Test(expected=DimensionMismatchException.class)
056 public void testPreconditions5() {
057 new StepFunction(new double[] {0, 1}, new double[] {0, -1, -2});
058 }
059
060 @Test(expected=NonMonotonicSequenceException.class)
061 public void testPreconditions6() {
062 new StepFunction(new double[] {1, 0, 1}, new double[] {0, -1, -2});
063 }
064
065 @Test
066 public void testSomeValues() {
067 final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
068 final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };
069
070 final UnivariateFunction f = new StepFunction(x, y);
071
072 Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
073 Assert.assertEquals(4, f.value(-10), EPS);
074 Assert.assertEquals(-1, f.value(-0.4), EPS);
075 Assert.assertEquals(-5.5, f.value(0), EPS);
076 Assert.assertEquals(0.4, f.value(2), EPS);
077 Assert.assertEquals(5.8, f.value(10), EPS);
078 Assert.assertEquals(51.2, f.value(30), EPS);
079 Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
080 }
081
082 @Test
083 public void testEndpointBehavior() {
084 final double[] x = {0, 1, 2, 3};
085 final double[] xp = {-8, 1, 2, 3};
086 final double[] y = {1, 2, 3, 4};
087 final UnivariateFunction f = new StepFunction(x, y);
088 final UnivariateFunction fp = new StepFunction(xp, y);
089 Assert.assertEquals(f.value(-8), fp.value(-8), EPS);
090 Assert.assertEquals(f.value(-10), fp.value(-10), EPS);
091 Assert.assertEquals(f.value(0), fp.value(0), EPS);
092 Assert.assertEquals(f.value(0.5), fp.value(0.5), EPS);
093 for (int i = 0; i < x.length; i++) {
094 Assert.assertEquals(y[i], f.value(x[i]), EPS);
095 if (i > 0) {
096 Assert.assertEquals(y[i - 1], f.value(x[i] - 0.5), EPS);
097 } else {
098 Assert.assertEquals(y[0], f.value(x[i] - 0.5), EPS);
099 }
100 }
101 }
102
103 @Test
104 public void testHeaviside() {
105 final UnivariateFunction h = new StepFunction(new double[] {-1, 0},
106 new double[] {0, 1});
107
108 Assert.assertEquals(0, h.value(Double.NEGATIVE_INFINITY), 0);
109 Assert.assertEquals(0, h.value(-Double.MAX_VALUE), 0);
110 Assert.assertEquals(0, h.value(-2), 0);
111 Assert.assertEquals(0, h.value(-Double.MIN_VALUE), 0);
112 Assert.assertEquals(1, h.value(0), 0);
113 Assert.assertEquals(1, h.value(2), 0);
114 Assert.assertEquals(1, h.value(Double.POSITIVE_INFINITY), 0);
115 }
116 }