SearchInterval.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.optim.univariate;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.optim.OptimizationData;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * Search interval and (optional) start value.
  28.  * <br>
  29.  * Immutable class.
  30.  *
  31.  */
  32. public class SearchInterval implements OptimizationData {
  33.     /** Lower bound. */
  34.     private final double lower;
  35.     /** Upper bound. */
  36.     private final double upper;
  37.     /** Start value. */
  38.     private final double start;

  39.     /** Simple constructor.
  40.      * @param lo Lower bound.
  41.      * @param hi Upper bound.
  42.      * @param init Start value.
  43.      * @throws MathIllegalArgumentException if {@code lo >= hi}.
  44.      * @throws MathIllegalArgumentException if {@code init < lo} or {@code init > hi}.
  45.      */
  46.     public SearchInterval(double lo,
  47.                           double hi,
  48.                           double init) {
  49.         if (lo >= hi) {
  50.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
  51.                                                    lo, hi);
  52.         }

  53.         MathUtils.checkRangeInclusive(init, lo, hi);

  54.         lower = lo;
  55.         upper = hi;
  56.         start = init;
  57.     }

  58.     /** Simple constructor.
  59.      * @param lo Lower bound.
  60.      * @param hi Upper bound.
  61.      * @throws MathIllegalArgumentException if {@code lo >= hi}.
  62.      */
  63.     public SearchInterval(double lo,
  64.                           double hi) {
  65.         this(lo, hi, 0.5 * (lo + hi));
  66.     }

  67.     /**
  68.      * Gets the lower bound.
  69.      *
  70.      * @return the lower bound.
  71.      */
  72.     public double getMin() {
  73.         return lower;
  74.     }
  75.     /**
  76.      * Gets the upper bound.
  77.      *
  78.      * @return the upper bound.
  79.      */
  80.     public double getMax() {
  81.         return upper;
  82.     }
  83.     /**
  84.      * Gets the start value.
  85.      *
  86.      * @return the start value.
  87.      */
  88.     public double getStartValue() {
  89.         return start;
  90.     }
  91. }