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.  *      http://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. package org.apache.commons.math4.legacy.optim.univariate;

  18. import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
  19. import org.apache.commons.math4.legacy.exception.OutOfRangeException;
  20. import org.apache.commons.math4.legacy.optim.OptimizationData;

  21. /**
  22.  * Search interval and (optional) start value.
  23.  * <br>
  24.  * Immutable class.
  25.  *
  26.  * @since 3.1
  27.  */
  28. public class SearchInterval implements OptimizationData {
  29.     /** Lower bound. */
  30.     private final double lower;
  31.     /** Upper bound. */
  32.     private final double upper;
  33.     /** Start value. */
  34.     private final double start;

  35.     /**
  36.      * @param lo Lower bound.
  37.      * @param hi Upper bound.
  38.      * @param init Start value.
  39.      * @throws NumberIsTooLargeException if {@code lo >= hi}.
  40.      * @throws OutOfRangeException if {@code init < lo} or {@code init > hi}.
  41.      */
  42.     public SearchInterval(double lo,
  43.                           double hi,
  44.                           double init) {
  45.         if (lo >= hi) {
  46.             throw new NumberIsTooLargeException(lo, hi, false);
  47.         }
  48.         if (init < lo ||
  49.             init > hi) {
  50.             throw new OutOfRangeException(init, lo, hi);
  51.         }

  52.         lower = lo;
  53.         upper = hi;
  54.         start = init;
  55.     }

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

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