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