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 java.util.Arrays; 26 27 import org.hipparchus.analysis.UnivariateFunction; 28 import org.hipparchus.exception.LocalizedCoreFormats; 29 import org.hipparchus.exception.MathIllegalArgumentException; 30 import org.hipparchus.exception.NullArgumentException; 31 import org.hipparchus.util.MathArrays; 32 33 /** 34 * <a href="http://en.wikipedia.org/wiki/Step_function"> 35 * Step function</a>. 36 * 37 */ 38 public class StepFunction implements UnivariateFunction { 39 /** Abscissae. */ 40 private final double[] abscissa; 41 /** Ordinates. */ 42 private final double[] ordinate; 43 44 /** 45 * Builds a step function from a list of arguments and the corresponding 46 * values. Specifically, returns the function h(x) defined by <pre><code> 47 * h(x) = y[0] for all x < x[1] 48 * y[1] for x[1] ≤ x < x[2] 49 * ... 50 * y[y.length - 1] for x ≥ x[x.length - 1] 51 * </code></pre> 52 * The value of {@code x[0]} is ignored, but it must be strictly less than 53 * {@code x[1]}. 54 * 55 * @param x Domain values where the function changes value. 56 * @param y Values of the function. 57 * @throws MathIllegalArgumentException 58 * if the {@code x} array is not sorted in strictly increasing order. 59 * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. 60 * @throws MathIllegalArgumentException if {@code x} or {@code y} are zero-length. 61 * @throws MathIllegalArgumentException if {@code x} and {@code y} do not 62 * have the same length. 63 */ 64 public StepFunction(double[] x, 65 double[] y) 66 throws MathIllegalArgumentException, NullArgumentException { 67 if (x == null || 68 y == null) { 69 throw new NullArgumentException(); 70 } 71 if (x.length == 0 || 72 y.length == 0) { 73 throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA); 74 } 75 MathArrays.checkEqualLength(y, x); 76 MathArrays.checkOrder(x); 77 78 abscissa = x.clone(); 79 ordinate = y.clone(); 80 } 81 82 /** {@inheritDoc} */ 83 @Override 84 public double value(double x) { 85 final int index = Arrays.binarySearch(abscissa, x); 86 87 if (index < -1) { 88 // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]". 89 return ordinate[-index-2]; 90 } else if (index >= 0) { 91 // "x" is exactly "abscissa[index]". 92 return ordinate[index]; 93 } else { 94 // Otherwise, "x" is smaller than the first value in "abscissa" 95 // (hence the returned value should be "ordinate[0]"). 96 return ordinate[0]; 97 } 98 99 } 100 }