StepFunction.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.analysis.function;

  22. import java.util.Arrays;

  23. import org.hipparchus.analysis.UnivariateFunction;
  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.exception.NullArgumentException;
  27. import org.hipparchus.util.MathArrays;

  28. /**
  29.  * <a href="http://en.wikipedia.org/wiki/Step_function">
  30.  *  Step function</a>.
  31.  *
  32.  */
  33. public class StepFunction implements UnivariateFunction {
  34.     /** Abscissae. */
  35.     private final double[] abscissa;
  36.     /** Ordinates. */
  37.     private final double[] ordinate;

  38.     /**
  39.      * Builds a step function from a list of arguments and the corresponding
  40.      * values. Specifically, returns the function h(x) defined by <pre><code>
  41.      * h(x) = y[0] for all x &lt; x[1]
  42.      *        y[1] for x[1] &le; x &lt; x[2]
  43.      *        ...
  44.      *        y[y.length - 1] for x &ge; x[x.length - 1]
  45.      * </code></pre>
  46.      * The value of {@code x[0]} is ignored, but it must be strictly less than
  47.      * {@code x[1]}.
  48.      *
  49.      * @param x Domain values where the function changes value.
  50.      * @param y Values of the function.
  51.      * @throws MathIllegalArgumentException
  52.      * if the {@code x} array is not sorted in strictly increasing order.
  53.      * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
  54.      * @throws MathIllegalArgumentException if {@code x} or {@code y} are zero-length.
  55.      * @throws MathIllegalArgumentException if {@code x} and {@code y} do not
  56.      * have the same length.
  57.      */
  58.     public StepFunction(double[] x,
  59.                         double[] y)
  60.         throws MathIllegalArgumentException, NullArgumentException {
  61.         if (x == null ||
  62.             y == null) {
  63.             throw new NullArgumentException();
  64.         }
  65.         if (x.length == 0 ||
  66.             y.length == 0) {
  67.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
  68.         }
  69.         MathArrays.checkEqualLength(y, x);
  70.         MathArrays.checkOrder(x);

  71.         abscissa = x.clone();
  72.         ordinate = y.clone();
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public double value(double x) {
  77.         final int index = Arrays.binarySearch(abscissa, x);

  78.         if (index < -1) {
  79.             // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]".
  80.             return ordinate[-index-2];
  81.         } else if (index >= 0) {
  82.             // "x" is exactly "abscissa[index]".
  83.             return ordinate[index];
  84.         } else {
  85.             // Otherwise, "x" is smaller than the first value in "abscissa"
  86.             // (hence the returned value should be "ordinate[0]").
  87.             return ordinate[0];
  88.         }

  89.     }
  90. }