View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim;
23  
24  import java.io.Serializable;
25  
26  import org.hipparchus.util.Pair;
27  
28  /**
29   * This class holds a point and the vectorial value of an objective function at
30   * that point.
31   *
32   * @see PointValuePair
33   * @see org.hipparchus.analysis.MultivariateVectorFunction
34   */
35  public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
36      /** Serializable UID. */
37      private static final long serialVersionUID = 20120513L;
38  
39      /**
40       * Builds a point/objective function value pair.
41       *
42       * @param point Point coordinates. This instance will store
43       * a copy of the array, not the array passed as argument.
44       * @param value Value of the objective function at the point.
45       */
46      public PointVectorValuePair(final double[] point,
47                                  final double[] value) {
48          this(point, value, true);
49      }
50  
51      /**
52       * Build a point/objective function value pair.
53       *
54       * @param point Point coordinates.
55       * @param value Value of the objective function at the point.
56       * @param copyArray if {@code true}, the input arrays will be copied,
57       * otherwise they will be referenced.
58       */
59      public PointVectorValuePair(final double[] point,
60                                  final double[] value,
61                                  final boolean copyArray) {
62          super(copyArray ?
63                ((point == null) ? null :
64                 point.clone()) :
65                point,
66                copyArray ?
67                ((value == null) ? null :
68                 value.clone()) :
69                value);
70      }
71  
72      /**
73       * Gets the point.
74       *
75       * @return a copy of the stored point.
76       */
77      public double[] getPoint() {
78          final double[] p = getKey();
79          return p == null ? null : p.clone();
80      }
81  
82      /**
83       * Gets a reference to the point.
84       *
85       * @return a reference to the internal array storing the point.
86       */
87      public double[] getPointRef() {
88          return getKey();
89      }
90  
91      /**
92       * Gets the value of the objective function.
93       *
94       * @return a copy of the stored value of the objective function.
95       */
96      @Override
97      public double[] getValue() {
98          final double[] v = super.getValue();
99          return v == null ? null : v.clone();
100     }
101 
102     /**
103      * Gets a reference to the value of the objective function.
104      *
105      * @return a reference to the internal array storing the value of
106      * the objective function.
107      */
108     public double[] getValueRef() {
109         return super.getValue();
110     }
111 
112     /**
113      * Replace the instance with a data transfer object for serialization.
114      * @return data transfer object that will be serialized
115      */
116     private Object writeReplace() {
117         return new DataTransferObject(getKey(), getValue());
118     }
119 
120     /** Internal class used only for serialization. */
121     private static class DataTransferObject implements Serializable {
122         /** Serializable UID. */
123         private static final long serialVersionUID = 20120513L;
124         /**
125          * Point coordinates.
126          * @Serial
127          */
128         private final double[] point;
129         /**
130          * Value of the objective function at the point.
131          * @Serial
132          */
133         private final double[] value;
134 
135         /** Simple constructor.
136          * @param point Point coordinates.
137          * @param value Value of the objective function at the point.
138          */
139         DataTransferObject(final double[] point, final double[] value) {
140             this.point = point.clone();
141             this.value = value.clone();
142         }
143 
144         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
145          * @return replacement {@link PointValuePair}
146          */
147         private Object readResolve() {
148             return new PointVectorValuePair(point, value, false);
149         }
150     }
151 }