1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.linear;
24
25 import org.junit.jupiter.api.Test;
26
27 import java.io.BufferedReader;
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.util.Random;
32
33 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertFalse;
36 import static org.junit.jupiter.api.Assertions.assertSame;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38
39
40 public class SingularValueDecompositionTest {
41
42 private double[][] testSquare = {
43 { 24.0 / 25.0, 43.0 / 25.0 },
44 { 57.0 / 25.0, 24.0 / 25.0 }
45 };
46
47 private double[][] testNonSquare = {
48 { -540.0 / 625.0, 963.0 / 625.0, -216.0 / 625.0 },
49 { -1730.0 / 625.0, -744.0 / 625.0, 1008.0 / 625.0 },
50 { -720.0 / 625.0, 1284.0 / 625.0, -288.0 / 625.0 },
51 { -360.0 / 625.0, 192.0 / 625.0, 1756.0 / 625.0 },
52 };
53
54 private static final double normTolerance = 10e-14;
55
56 @Test
57 void testMoreRows() {
58 final double[] singularValues = { 123.456, 2.3, 1.001, 0.999 };
59 final int rows = singularValues.length + 2;
60 final int columns = singularValues.length;
61 Random r = new Random(15338437322523l);
62 SingularValueDecomposition svd =
63 new SingularValueDecomposition(createTestMatrix(r, rows, columns, singularValues));
64 double[] computedSV = svd.getSingularValues();
65 assertEquals(singularValues.length, computedSV.length);
66 for (int i = 0; i < singularValues.length; ++i) {
67 assertEquals(singularValues[i], computedSV[i], 1.0e-10);
68 }
69 }
70
71 @Test
72 void testMoreColumns() {
73 final double[] singularValues = { 123.456, 2.3, 1.001, 0.999 };
74 final int rows = singularValues.length;
75 final int columns = singularValues.length + 2;
76 Random r = new Random(732763225836210l);
77 SingularValueDecomposition svd =
78 new SingularValueDecomposition(createTestMatrix(r, rows, columns, singularValues));
79 double[] computedSV = svd.getSingularValues();
80 assertEquals(singularValues.length, computedSV.length);
81 for (int i = 0; i < singularValues.length; ++i) {
82 assertEquals(singularValues[i], computedSV[i], 1.0e-10);
83 }
84 }
85
86
87 @Test
88 void testDimensions() {
89 RealMatrix matrix = MatrixUtils.createRealMatrix(testSquare);
90 final int m = matrix.getRowDimension();
91 final int n = matrix.getColumnDimension();
92 SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
93 assertEquals(m, svd.getU().getRowDimension());
94 assertEquals(m, svd.getU().getColumnDimension());
95 assertEquals(m, svd.getS().getColumnDimension());
96 assertEquals(n, svd.getS().getColumnDimension());
97 assertEquals(n, svd.getV().getRowDimension());
98 assertEquals(n, svd.getV().getColumnDimension());
99
100 }
101
102 @Test
103 void testDecomposer() {
104 MatrixDecomposer decomposer = new SingularValueDecomposer();
105 assertTrue(decomposer.decompose(MatrixUtils.createRealMatrix(testSquare)).isNonSingular());
106 assertFalse(decomposer.decompose(MatrixUtils.createRealMatrix(testNonSquare)).isNonSingular());
107 }
108
109
110 @Test
111 void testHadamard() {
112 RealMatrix matrix = new Array2DRowRealMatrix(new double[][] {
113 {15.0 / 2.0, 5.0 / 2.0, 9.0 / 2.0, 3.0 / 2.0 },
114 { 5.0 / 2.0, 15.0 / 2.0, 3.0 / 2.0, 9.0 / 2.0 },
115 { 9.0 / 2.0, 3.0 / 2.0, 15.0 / 2.0, 5.0 / 2.0 },
116 { 3.0 / 2.0, 9.0 / 2.0, 5.0 / 2.0, 15.0 / 2.0 }
117 }, false);
118 SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
119 assertEquals(16.0, svd.getSingularValues()[0], 1.0e-14);
120 assertEquals( 8.0, svd.getSingularValues()[1], 1.0e-14);
121 assertEquals( 4.0, svd.getSingularValues()[2], 1.0e-14);
122 assertEquals( 2.0, svd.getSingularValues()[3], 1.0e-14);
123
124 RealMatrix fullCovariance = new Array2DRowRealMatrix(new double[][] {
125 { 85.0 / 1024, -51.0 / 1024, -75.0 / 1024, 45.0 / 1024 },
126 { -51.0 / 1024, 85.0 / 1024, 45.0 / 1024, -75.0 / 1024 },
127 { -75.0 / 1024, 45.0 / 1024, 85.0 / 1024, -51.0 / 1024 },
128 { 45.0 / 1024, -75.0 / 1024, -51.0 / 1024, 85.0 / 1024 }
129 }, false);
130 assertEquals(0.0,
131 fullCovariance.subtract(svd.getCovariance(0.0)).getNorm1(),
132 1.0e-14);
133
134 RealMatrix halfCovariance = new Array2DRowRealMatrix(new double[][] {
135 { 5.0 / 1024, -3.0 / 1024, 5.0 / 1024, -3.0 / 1024 },
136 { -3.0 / 1024, 5.0 / 1024, -3.0 / 1024, 5.0 / 1024 },
137 { 5.0 / 1024, -3.0 / 1024, 5.0 / 1024, -3.0 / 1024 },
138 { -3.0 / 1024, 5.0 / 1024, -3.0 / 1024, 5.0 / 1024 }
139 }, false);
140 assertEquals(0.0,
141 halfCovariance.subtract(svd.getCovariance(6.0)).getNorm1(),
142 1.0e-14);
143
144 }
145
146
147 @Test
148 void testAEqualUSVt() {
149 checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
150 checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
151 checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
152 }
153
154 public void checkAEqualUSVt(final RealMatrix matrix) {
155 SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
156 RealMatrix u = svd.getU();
157 RealMatrix s = svd.getS();
158 RealMatrix v = svd.getV();
159 double norm = u.multiply(s).multiplyTransposed(v).subtract(matrix).getNorm1();
160 assertEquals(0, norm, normTolerance);
161
162 }
163
164
165 @Test
166 void testUOrthogonal() {
167 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getU());
168 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare)).getU());
169 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
170 }
171
172
173 @Test
174 void testVOrthogonal() {
175 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getV());
176 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare)).getV());
177 checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
178 }
179
180 public void checkOrthogonal(final RealMatrix m) {
181 RealMatrix mTm = m.transposeMultiply(m);
182 RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
183 assertEquals(0, mTm.subtract(id).getNorm1(), normTolerance);
184 }
185
186
187
188
189 public void testMatricesValues1() {
190 SingularValueDecomposition svd =
191 new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare));
192 RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
193 { 3.0 / 5.0, -4.0 / 5.0 },
194 { 4.0 / 5.0, 3.0 / 5.0 }
195 });
196 RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
197 { 3.0, 0.0 },
198 { 0.0, 1.0 }
199 });
200 RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
201 { 4.0 / 5.0, 3.0 / 5.0 },
202 { 3.0 / 5.0, -4.0 / 5.0 }
203 });
204
205
206 RealMatrix u = svd.getU();
207 assertEquals(0, u.subtract(uRef).getNorm1(), normTolerance);
208 RealMatrix s = svd.getS();
209 assertEquals(0, s.subtract(sRef).getNorm1(), normTolerance);
210 RealMatrix v = svd.getV();
211 assertEquals(0, v.subtract(vRef).getNorm1(), normTolerance);
212
213
214 assertSame(u, svd.getU());
215 assertSame(s, svd.getS());
216 assertSame(v, svd.getV());
217
218 }
219
220
221
222
223 public void useless_testMatricesValues2() {
224
225 RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
226 { 0.0 / 5.0, 3.0 / 5.0, 0.0 / 5.0 },
227 { -4.0 / 5.0, 0.0 / 5.0, -3.0 / 5.0 },
228 { 0.0 / 5.0, 4.0 / 5.0, 0.0 / 5.0 },
229 { -3.0 / 5.0, 0.0 / 5.0, 4.0 / 5.0 }
230 });
231 RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
232 { 4.0, 0.0, 0.0 },
233 { 0.0, 3.0, 0.0 },
234 { 0.0, 0.0, 2.0 }
235 });
236 RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
237 { 80.0 / 125.0, -60.0 / 125.0, 75.0 / 125.0 },
238 { 24.0 / 125.0, 107.0 / 125.0, 60.0 / 125.0 },
239 { -93.0 / 125.0, -24.0 / 125.0, 80.0 / 125.0 }
240 });
241
242
243 SingularValueDecomposition svd =
244 new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare));
245 RealMatrix u = svd.getU();
246 assertEquals(0, u.subtract(uRef).getNorm1(), normTolerance);
247 RealMatrix s = svd.getS();
248 assertEquals(0, s.subtract(sRef).getNorm1(), normTolerance);
249 RealMatrix v = svd.getV();
250 assertEquals(0, v.subtract(vRef).getNorm1(), normTolerance);
251
252
253 assertSame(u, svd.getU());
254 assertSame(s, svd.getS());
255 assertSame(v, svd.getV());
256
257 }
258
259
260 @Test
261 void testRank() {
262 double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
263 RealMatrix m = new Array2DRowRealMatrix(d);
264 SingularValueDecomposition svd = new SingularValueDecomposition(m);
265 assertEquals(2, svd.getRank());
266 }
267
268
269 @Test
270 void testStability1() {
271 RealMatrix m = new Array2DRowRealMatrix(201, 201);
272 loadRealMatrix(m,"matrix1.csv");
273 assertDoesNotThrow(() -> {
274 new SingularValueDecomposition(m);
275 }, "Exception whilst constructing SVD");
276 }
277
278
279 @Test
280 void testStability2() {
281 RealMatrix m = new Array2DRowRealMatrix(7, 168);
282 loadRealMatrix(m,"matrix2.csv");
283 assertDoesNotThrow(() -> {
284 new SingularValueDecomposition(m);
285 }, "Exception whilst constructing SVD");
286 }
287
288 private void loadRealMatrix(RealMatrix m, String resourceName) {
289 try {
290 DataInputStream in = new DataInputStream(getClass().getResourceAsStream(resourceName));
291 BufferedReader br = new BufferedReader(new InputStreamReader(in));
292 String strLine;
293 int row = 0;
294 while ((strLine = br.readLine()) != null) {
295 if (!strLine.startsWith("#")) {
296 int col = 0;
297 for (String entry : strLine.split(",")) {
298 m.setEntry(row, col++, Double.parseDouble(entry));
299 }
300 row++;
301 }
302 }
303 in.close();
304 } catch (IOException e) {}
305 }
306
307
308 @Test
309 void testConditionNumber() {
310 SingularValueDecomposition svd =
311 new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare));
312
313 assertEquals(3.0, svd.getConditionNumber(), 1.5e-15);
314 }
315
316 @Test
317 void testInverseConditionNumber() {
318 SingularValueDecomposition svd =
319 new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare));
320 assertEquals(1.0/3.0, svd.getInverseConditionNumber(), 1.5e-15);
321 }
322
323 private RealMatrix createTestMatrix(final Random r, final int rows, final int columns,
324 final double[] singularValues) {
325 final RealMatrix u = EigenDecompositionSymmetricTest.createOrthogonalMatrix(r, rows);
326 final RealMatrix d = new Array2DRowRealMatrix(rows, columns);
327 d.setSubMatrix(MatrixUtils.createRealDiagonalMatrix(singularValues).getData(), 0, 0);
328 final RealMatrix v = EigenDecompositionSymmetricTest.createOrthogonalMatrix(r, columns);
329 return u.multiply(d).multiply(v);
330 }
331
332 @Test
333 void testIssue947() {
334 double[][] nans = new double[][] {
335 { Double.NaN, Double.NaN },
336 { Double.NaN, Double.NaN }
337 };
338 RealMatrix m = new Array2DRowRealMatrix(nans, false);
339 SingularValueDecomposition svd = new SingularValueDecomposition(m);
340 assertTrue(Double.isNaN(svd.getSingularValues()[0]));
341 assertTrue(Double.isNaN(svd.getSingularValues()[1]));
342 }
343
344 }