PointVectorValuePair.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;

  22. import java.io.Serializable;

  23. import org.hipparchus.util.Pair;

  24. /**
  25.  * This class holds a point and the vectorial value of an objective function at
  26.  * that point.
  27.  *
  28.  * @see PointValuePair
  29.  * @see org.hipparchus.analysis.MultivariateVectorFunction
  30.  */
  31. public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
  32.     /** Serializable UID. */
  33.     private static final long serialVersionUID = 20120513L;

  34.     /**
  35.      * Builds a point/objective function value pair.
  36.      *
  37.      * @param point Point coordinates. This instance will store
  38.      * a copy of the array, not the array passed as argument.
  39.      * @param value Value of the objective function at the point.
  40.      */
  41.     public PointVectorValuePair(final double[] point,
  42.                                 final double[] value) {
  43.         this(point, value, true);
  44.     }

  45.     /**
  46.      * Build a point/objective function value pair.
  47.      *
  48.      * @param point Point coordinates.
  49.      * @param value Value of the objective function at the point.
  50.      * @param copyArray if {@code true}, the input arrays will be copied,
  51.      * otherwise they will be referenced.
  52.      */
  53.     public PointVectorValuePair(final double[] point,
  54.                                 final double[] value,
  55.                                 final boolean copyArray) {
  56.         super(copyArray ?
  57.               ((point == null) ? null :
  58.                point.clone()) :
  59.               point,
  60.               copyArray ?
  61.               ((value == null) ? null :
  62.                value.clone()) :
  63.               value);
  64.     }

  65.     /**
  66.      * Gets the point.
  67.      *
  68.      * @return a copy of the stored point.
  69.      */
  70.     public double[] getPoint() {
  71.         final double[] p = getKey();
  72.         return p == null ? null : p.clone();
  73.     }

  74.     /**
  75.      * Gets a reference to the point.
  76.      *
  77.      * @return a reference to the internal array storing the point.
  78.      */
  79.     public double[] getPointRef() {
  80.         return getKey();
  81.     }

  82.     /**
  83.      * Gets the value of the objective function.
  84.      *
  85.      * @return a copy of the stored value of the objective function.
  86.      */
  87.     @Override
  88.     public double[] getValue() {
  89.         final double[] v = super.getValue();
  90.         return v == null ? null : v.clone();
  91.     }

  92.     /**
  93.      * Gets a reference to the value of the objective function.
  94.      *
  95.      * @return a reference to the internal array storing the value of
  96.      * the objective function.
  97.      */
  98.     public double[] getValueRef() {
  99.         return super.getValue();
  100.     }

  101.     /**
  102.      * Replace the instance with a data transfer object for serialization.
  103.      * @return data transfer object that will be serialized
  104.      */
  105.     private Object writeReplace() {
  106.         return new DataTransferObject(getKey(), getValue());
  107.     }

  108.     /** Internal class used only for serialization. */
  109.     private static class DataTransferObject implements Serializable {
  110.         /** Serializable UID. */
  111.         private static final long serialVersionUID = 20120513L;
  112.         /**
  113.          * Point coordinates.
  114.          * @Serial
  115.          */
  116.         private final double[] point;
  117.         /**
  118.          * Value of the objective function at the point.
  119.          * @Serial
  120.          */
  121.         private final double[] value;

  122.         /** Simple constructor.
  123.          * @param point Point coordinates.
  124.          * @param value Value of the objective function at the point.
  125.          */
  126.         DataTransferObject(final double[] point, final double[] value) {
  127.             this.point = point.clone();
  128.             this.value = value.clone();
  129.         }

  130.         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
  131.          * @return replacement {@link PointValuePair}
  132.          */
  133.         private Object readResolve() {
  134.             return new PointVectorValuePair(point, value, false);
  135.         }
  136.     }
  137. }