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