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.legacy.optim;
018
019import org.apache.commons.math4.legacy.core.Pair;
020
021/**
022 * This class holds a point and the vectorial value of an objective function at
023 * that point.
024 *
025 * @see PointValuePair
026 * @see org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction
027 * @since 3.0
028 */
029public class PointVectorValuePair extends Pair<double[], double[]> {
030    /**
031     * Builds a point/objective function value pair.
032     *
033     * @param point Point coordinates. This instance will store
034     * a copy of the array, not the array passed as argument.
035     * @param value Value of the objective function at the point.
036     */
037    public PointVectorValuePair(final double[] point,
038                                final double[] value) {
039        this(point, value, true);
040    }
041
042    /**
043     * Build a point/objective function value pair.
044     *
045     * @param point Point coordinates.
046     * @param value Value of the objective function at the point.
047     * @param copyArray if {@code true}, the input arrays will be copied,
048     * otherwise they will be referenced.
049     */
050    public PointVectorValuePair(final double[] point,
051                                final double[] value,
052                                final boolean copyArray) {
053        super(copyArray ?
054              ((point == null) ? null :
055               point.clone()) :
056              point,
057              copyArray ?
058              ((value == null) ? null :
059               value.clone()) :
060              value);
061    }
062
063    /**
064     * Gets the point.
065     *
066     * @return a copy of the stored point.
067     */
068    public double[] getPoint() {
069        final double[] p = getKey();
070        return p == null ? null : p.clone();
071    }
072
073    /**
074     * Gets a reference to the point.
075     *
076     * @return a reference to the internal array storing the point.
077     */
078    public double[] getPointRef() {
079        return getKey();
080    }
081
082    /**
083     * Gets the value of the objective function.
084     *
085     * @return a copy of the stored value of the objective function.
086     */
087    @Override
088    public double[] getValue() {
089        final double[] v = super.getValue();
090        return v == null ? null : v.clone();
091    }
092
093    /**
094     * Gets a reference to the value of the objective function.
095     *
096     * @return a reference to the internal array storing the value of
097     * the objective function.
098     */
099    public double[] getValueRef() {
100        return super.getValue();
101    }
102}