1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.optim.nonlinear.vector.constrained;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.hipparchus.exception.MathIllegalArgumentException;
21 import org.hipparchus.linear.AbstractRealMatrix;
22 import org.hipparchus.linear.ArrayRealVector;
23 import org.hipparchus.linear.MatrixUtils;
24 import org.hipparchus.linear.RealMatrix;
25 import org.hipparchus.linear.RealVector;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public final class QPConstraintMatrix extends AbstractRealMatrix {
58
59
60 private final int variableCount;
61
62
63 private final RealMatrix equalityMatrix;
64
65
66 private final RealMatrix inequalityMatrix;
67
68
69 private final RealMatrix boundedMatrix;
70
71
72 private final int equalityCount;
73
74
75 private final int inequalityCount;
76
77
78 private final int boundedCount;
79
80
81 private final int inequalityOffset;
82
83
84 private final int lowerBoundOffset;
85
86
87 private final int upperBoundOffset;
88
89
90 private final int constraintCount;
91
92
93
94
95
96
97
98
99
100 public QPConstraintMatrix(final int variableCount,
101 final RealMatrix equalityMatrix,
102 final RealMatrix inequalityMatrix,
103 final RealMatrix boundedMatrix) {
104
105 if (variableCount < 0) {
106 throw new MathIllegalArgumentException(LocalizedCoreFormats.NEGATIVE_VALUE, variableCount);
107 }
108
109 this.variableCount = variableCount;
110 this.equalityMatrix = equalityMatrix;
111 this.inequalityMatrix = inequalityMatrix;
112 this.boundedMatrix = boundedMatrix;
113
114 checkColumnDimension(equalityMatrix, variableCount);
115 checkColumnDimension(inequalityMatrix, variableCount);
116 checkColumnDimension(boundedMatrix, variableCount);
117
118 this.equalityCount = equalityMatrix == null ? 0 : equalityMatrix.getRowDimension();
119 this.inequalityCount = inequalityMatrix == null ? 0 : inequalityMatrix.getRowDimension();
120 this.boundedCount = boundedMatrix == null ? 0 : boundedMatrix.getRowDimension();
121
122 this.inequalityOffset = equalityCount;
123 this.lowerBoundOffset = equalityCount + inequalityCount;
124 this.upperBoundOffset = lowerBoundOffset + boundedCount;
125 this.constraintCount = upperBoundOffset + boundedCount;
126 }
127
128
129
130
131
132 private static void checkColumnDimension(final RealMatrix matrix,
133 final int expected) {
134 if (matrix != null && matrix.getColumnDimension() != expected) {
135 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
136 matrix.getColumnDimension(), expected);
137 }
138 }
139
140
141 @Override
142 public int getRowDimension() {
143 return constraintCount;
144 }
145
146
147 @Override
148 public int getColumnDimension() {
149 return variableCount;
150 }
151
152
153
154
155
156
157
158
159 @Override
160 public double getEntry(final int row, final int column) {
161 MatrixUtils.checkMatrixIndex(this, row, column);
162
163 if (row < inequalityOffset) {
164 return equalityMatrix.getEntry(row, column);
165 }
166 if (row < lowerBoundOffset) {
167 return inequalityMatrix.getEntry(row - inequalityOffset, column);
168 }
169 if (row < upperBoundOffset) {
170 return boundedMatrix.getEntry(row - lowerBoundOffset, column);
171 }
172 return -boundedMatrix.getEntry(row - upperBoundOffset, column);
173 }
174
175
176
177
178
179
180
181
182
183
184
185 @Override
186 public RealVector getRowVector(final int row) {
187 MatrixUtils.checkRowIndex(this, row);
188
189 if (row < inequalityOffset) {
190 return equalityMatrix.getRowVector(row);
191 }
192 if (row < lowerBoundOffset) {
193 return inequalityMatrix.getRowVector(row - inequalityOffset);
194 }
195 if (row < upperBoundOffset) {
196 return boundedMatrix.getRowVector(row - lowerBoundOffset);
197 }
198
199 return boundedMatrix.getRowVector(row - upperBoundOffset)
200 .mapMultiplyToSelf(-1.0);
201 }
202
203
204
205
206
207
208
209 @Override
210 public double[] getRow(final int row) {
211 MatrixUtils.checkRowIndex(this, row);
212
213 if (row < inequalityOffset) {
214 return equalityMatrix.getRow(row);
215 }
216 if (row < lowerBoundOffset) {
217 return inequalityMatrix.getRow(row - inequalityOffset);
218 }
219 if (row < upperBoundOffset) {
220 return boundedMatrix.getRow(row - lowerBoundOffset);
221 }
222
223 final double[] data = boundedMatrix.getRow(row - upperBoundOffset);
224 for (int i = 0; i < data.length; i++) {
225 data[i] = -data[i];
226 }
227 return data;
228 }
229
230
231
232
233
234
235
236
237
238
239
240 @Override
241 public RealVector operate(final RealVector x) {
242 final double[] result = new double[constraintCount];
243
244 if (equalityCount > 0) {
245 copy(equalityMatrix.operate(x), result, 0, 1.0);
246 }
247
248 if (inequalityCount > 0) {
249 copy(inequalityMatrix.operate(x), result, inequalityOffset, 1.0);
250 }
251
252 if (boundedCount > 0) {
253 final RealVector boundedValue = boundedMatrix.operate(x);
254 copy(boundedValue, result, lowerBoundOffset, 1.0);
255 copy(boundedValue, result, upperBoundOffset, -1.0);
256 }
257
258 return new ArrayRealVector(result, false);
259 }
260
261
262
263
264
265
266
267 private static void copy(final RealVector source,
268 final double[] destination,
269 final int offset,
270 final double scale) {
271 for (int i = 0; i < source.getDimension(); i++) {
272 destination[offset + i] = scale * source.getEntry(i);
273 }
274 }
275
276
277
278
279
280 @Override
281 public void setEntry(final int row, final int column, final double value) {
282 throw new UnsupportedOperationException("QPConstraintMatrix is read-only");
283 }
284
285
286
287
288 @Override
289 public RealMatrix createMatrix(final int rowDimension, final int columnDimension) {
290 return MatrixUtils.createRealMatrix(rowDimension, columnDimension);
291 }
292
293
294
295
296 @Override
297 public RealMatrix copy() {
298 final RealMatrix result = MatrixUtils.createRealMatrix(constraintCount, variableCount);
299 for (int row = 0; row < constraintCount; row++) {
300 result.setRow(row, getRow(row));
301 }
302 return result;
303 }
304 }