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 */
017package org.apache.commons.math4.optim;
018
019import java.util.Arrays;
020
021/**
022 * Simple optimization constraints: lower and upper bounds.
023 * The valid range of the parameters is an interval that can be infinite
024 * (in one or both directions).
025 * <br>
026 * Immutable class.
027 *
028 * @since 3.1
029 */
030public class SimpleBounds implements OptimizationData {
031    /** Lower bounds. */
032    private final double[] lower;
033    /** Upper bounds. */
034    private final double[] upper;
035
036    /**
037     * @param lB Lower bounds.
038     * @param uB Upper bounds.
039     */
040    public SimpleBounds(double[] lB,
041                        double[] uB) {
042        lower = lB.clone();
043        upper = uB.clone();
044    }
045
046    /**
047     * Gets the lower bounds.
048     *
049     * @return the lower bounds.
050     */
051    public double[] getLower() {
052        return lower.clone();
053    }
054    /**
055     * Gets the upper bounds.
056     *
057     * @return the upper bounds.
058     */
059    public double[] getUpper() {
060        return upper.clone();
061    }
062
063    /**
064     * Factory method that creates instance of this class that represents
065     * unbounded ranges.
066     *
067     * @param dim Number of parameters.
068     * @return a new instance suitable for passing to an optimizer that
069     * requires bounds specification.
070     */
071    public static SimpleBounds unbounded(int dim) {
072        final double[] lB = new double[dim];
073        Arrays.fill(lB, Double.NEGATIVE_INFINITY);
074        final double[] uB = new double[dim];
075        Arrays.fill(uB, Double.POSITIVE_INFINITY);
076
077        return new SimpleBounds(lB, uB);
078    }
079}