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  
23  package org.hipparchus.linear;
24  
25  import org.hipparchus.util.FastMath;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertSame;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  class BiDiagonalTransformerTest {
34  
35      private double[][] testSquare = {
36              { 24.0 / 25.0, 43.0 / 25.0 },
37              { 57.0 / 25.0, 24.0 / 25.0 }
38      };
39  
40      private double[][] testNonSquare = {
41          {  -540.0 / 625.0,  963.0 / 625.0, -216.0 / 625.0 },
42          { -1730.0 / 625.0, -744.0 / 625.0, 1008.0 / 625.0 },
43          {  -720.0 / 625.0, 1284.0 / 625.0, -288.0 / 625.0 },
44          {  -360.0 / 625.0,  192.0 / 625.0, 1756.0 / 625.0 },
45      };
46  
47      @Test
48      void testDimensions() {
49          checkdimensions(MatrixUtils.createRealMatrix(testSquare));
50          checkdimensions(MatrixUtils.createRealMatrix(testNonSquare));
51          checkdimensions(MatrixUtils.createRealMatrix(testNonSquare).transpose());
52      }
53  
54      private void checkdimensions(RealMatrix matrix) {
55          final int m = matrix.getRowDimension();
56          final int n = matrix.getColumnDimension();
57          BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix);
58          assertEquals(m, transformer.getU().getRowDimension());
59          assertEquals(m, transformer.getU().getColumnDimension());
60          assertEquals(m, transformer.getB().getRowDimension());
61          assertEquals(n, transformer.getB().getColumnDimension());
62          assertEquals(n, transformer.getV().getRowDimension());
63          assertEquals(n, transformer.getV().getColumnDimension());
64  
65      }
66  
67      @Test
68      void testAEqualUSVt() {
69          checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
70          checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
71          checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
72      }
73  
74      private void checkAEqualUSVt(RealMatrix matrix) {
75          BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix);
76          RealMatrix u = transformer.getU();
77          RealMatrix b = transformer.getB();
78          RealMatrix v = transformer.getV();
79          double norm = u.multiply(b).multiplyTransposed(v).subtract(matrix).getNorm1();
80          assertEquals(0, norm, 1.0e-14);
81      }
82  
83      @Test
84      void testUOrthogonal() {
85          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getU());
86          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getU());
87          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
88      }
89  
90      @Test
91      void testVOrthogonal() {
92          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getV());
93          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getV());
94          checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
95      }
96  
97      private void checkOrthogonal(RealMatrix m) {
98          RealMatrix mTm = m.transposeMultiply(m);
99          RealMatrix id  = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
100         assertEquals(0, mTm.subtract(id).getNorm1(), 1.0e-14);
101     }
102 
103     @Test
104     void testBBiDiagonal() {
105         checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getB());
106         checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getB());
107         checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getB());
108     }
109 
110     private void checkBiDiagonal(RealMatrix m) {
111         final int rows = m.getRowDimension();
112         final int cols = m.getColumnDimension();
113         for (int i = 0; i < rows; ++i) {
114             for (int j = 0; j < cols; ++j) {
115                 if (rows < cols) {
116                     if ((i < j) || (i > j + 1)) {
117                         assertEquals(0, m.getEntry(i, j), 1.0e-16);
118                     }
119                 } else {
120                     if ((i < j - 1) || (i > j)) {
121                         assertEquals(0, m.getEntry(i, j), 1.0e-16);
122                     }
123                 }
124             }
125         }
126     }
127 
128     @Test
129     void testSingularMatrix() {
130        BiDiagonalTransformer transformer =
131             new BiDiagonalTransformer(MatrixUtils.createRealMatrix(new double[][] {
132                 { 1.0, 2.0, 3.0 },
133                 { 2.0, 3.0, 4.0 },
134                 { 3.0, 5.0, 7.0 }
135             }));
136        final double s3  = FastMath.sqrt(3.0);
137        final double s14 = FastMath.sqrt(14.0);
138        final double s1553 = FastMath.sqrt(1553.0);
139        RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
140            {  -1.0 / s14,  5.0 / (s3 * s14),  1.0 / s3 },
141            {  -2.0 / s14, -4.0 / (s3 * s14),  1.0 / s3 },
142            {  -3.0 / s14,  1.0 / (s3 * s14), -1.0 / s3 }
143        });
144        RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] {
145            { -s14, s1553 / s14,   0.0 },
146            {  0.0, -87 * s3 / (s14 * s1553), -s3 * s14 / s1553 },
147            {  0.0, 0.0, 0.0 }
148        });
149        RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
150            { 1.0,   0.0,         0.0        },
151            { 0.0,  -23 / s1553,  32 / s1553 },
152            { 0.0,  -32 / s1553, -23 / s1553 }
153        });
154 
155        // check values against known references
156        RealMatrix u = transformer.getU();
157        assertEquals(0, u.subtract(uRef).getNorm1(), 1.0e-14);
158        RealMatrix b = transformer.getB();
159        assertEquals(0, b.subtract(bRef).getNorm1(), 1.0e-14);
160        RealMatrix v = transformer.getV();
161        assertEquals(0, v.subtract(vRef).getNorm1(), 1.0e-14);
162 
163        // check the same cached instance is returned the second time
164         assertSame(u, transformer.getU());
165         assertSame(b, transformer.getB());
166         assertSame(v, transformer.getV());
167 
168     }
169 
170     @Test
171     void testMatricesValues() {
172        BiDiagonalTransformer transformer =
173             new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare));
174        final double s17 = FastMath.sqrt(17.0);
175         RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
176                 {  -8 / (5 * s17), 19 / (5 * s17) },
177                 { -19 / (5 * s17), -8 / (5 * s17) }
178         });
179         RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] {
180                 { -3 * s17 / 5, 32 * s17 / 85 },
181                 {      0.0,     -5 * s17 / 17 }
182         });
183         RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
184                 { 1.0,  0.0 },
185                 { 0.0, -1.0 }
186         });
187 
188         // check values against known references
189         RealMatrix u = transformer.getU();
190         assertEquals(0, u.subtract(uRef).getNorm1(), 1.0e-14);
191         RealMatrix b = transformer.getB();
192         assertEquals(0, b.subtract(bRef).getNorm1(), 1.0e-14);
193         RealMatrix v = transformer.getV();
194         assertEquals(0, v.subtract(vRef).getNorm1(), 1.0e-14);
195 
196         // check the same cached instance is returned the second time
197         assertSame(u, transformer.getU());
198         assertSame(b, transformer.getB());
199         assertSame(v, transformer.getV());
200 
201     }
202 
203     @Test
204     void testUpperOrLower() {
205         assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).isUpperBiDiagonal());
206         assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).isUpperBiDiagonal());
207         assertFalse(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).isUpperBiDiagonal());
208     }
209 
210 }