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 java.util.Random;
26  
27  import org.hipparchus.exception.LocalizedCoreFormats;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  import org.hipparchus.random.RandomDataGenerator;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public class SchurTransformerTest {
34  
35      private double[][] testSquare5 = {
36              { 5, 4, 3, 2, 1 },
37              { 1, 4, 0, 3, 3 },
38              { 2, 0, 3, 0, 0 },
39              { 3, 2, 1, 2, 5 },
40              { 4, 2, 1, 4, 1 }
41      };
42  
43      private double[][] testSquare3 = {
44              {  2, -1, 1 },
45              { -1,  2, 1 },
46              {  1, -1, 2 }
47      };
48  
49      // from http://eigen.tuxfamily.org/dox/classEigen_1_1RealSchur.html
50      private double[][] testRandom = {
51              {  0.680, -0.3300, -0.2700, -0.717, -0.687,  0.0259 },
52              { -0.211,  0.5360,  0.0268,  0.214, -0.198,  0.6780 },
53              {  0.566, -0.4440,  0.9040, -0.967, -0.740,  0.2250 },
54              {  0.597,  0.1080,  0.8320, -0.514, -0.782, -0.4080 },
55              {  0.823, -0.0452,  0.2710, -0.726,  0.998,  0.2750 },
56              { -0.605,  0.2580,  0.4350,  0.608, -0.563,  0.0486 }
57      };
58  
59      @Test
60      public void testNonSquare() {
61          try {
62              new SchurTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
63              Assert.fail("an exception should have been thrown");
64          } catch (MathIllegalArgumentException ime) {
65              Assert.assertEquals(LocalizedCoreFormats.NON_SQUARE_MATRIX, ime.getSpecifier());
66          }
67      }
68  
69      @Test
70      public void testAEqualPTPt() {
71          checkAEqualPTPt(MatrixUtils.createRealMatrix(testSquare5));
72          checkAEqualPTPt(MatrixUtils.createRealMatrix(testSquare3));
73          checkAEqualPTPt(MatrixUtils.createRealMatrix(testRandom));
74     }
75  
76      @Test
77      public void testPOrthogonal() {
78          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getP());
79          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getP());
80          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getP());
81      }
82  
83      @Test
84      public void testPTOrthogonal() {
85          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getPT());
86          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getPT());
87          checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getPT());
88      }
89  
90      @Test
91      public void testSchurForm() {
92          checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT());
93          checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT());
94          checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getT());
95      }
96  
97      @Test
98      public void testRandomData() {
99          for (int run = 0; run < 100; run++) {
100             Random r = new Random(System.currentTimeMillis());
101 
102             // matrix size
103             int size = r.nextInt(20) + 4;
104 
105             double[][] data = new double[size][size];
106             for (int i = 0; i < size; i++) {
107                 for (int j = 0; j < size; j++) {
108                     data[i][j] = r.nextInt(100);
109                 }
110             }
111 
112             RealMatrix m = MatrixUtils.createRealMatrix(data);
113             RealMatrix s = checkAEqualPTPt(m);
114             checkSchurForm(s);
115         }
116     }
117 
118     @Test
119     public void testRandomDataNormalDistribution() {
120         for (int run = 0; run < 100; run++) {
121             Random r = new Random(System.currentTimeMillis());
122             RandomDataGenerator randomDataGenerator = new RandomDataGenerator(100);
123 
124             // matrix size
125             int size = r.nextInt(20) + 4;
126 
127             double[][] data = new double[size][size];
128             for (int i = 0; i < size; i++) {
129                 for (int j = 0; j < size; j++) {
130                     data[i][j] = randomDataGenerator.nextNormal(0.0, r.nextDouble() * 5);
131                 }
132             }
133 
134             RealMatrix m = MatrixUtils.createRealMatrix(data);
135             RealMatrix s = checkAEqualPTPt(m);
136             checkSchurForm(s);
137         }
138     }
139 
140     @Test
141     public void testMath848() {
142         double[][] data = {
143                 { 0.1849449280, -0.0646971046,  0.0774755812, -0.0969651755, -0.0692648806,  0.3282344352, -0.0177423074,  0.2063136340},
144                 {-0.0742700134, -0.0289063030, -0.0017269460, -0.0375550146, -0.0487737922, -0.2616837868, -0.0821201295, -0.2530000167},
145                 { 0.2549910127,  0.0995733692, -0.0009718388,  0.0149282808,  0.1791878897, -0.0823182816,  0.0582629256,  0.3219545182},
146                 {-0.0694747557, -0.1880649148, -0.2740630911,  0.0720096468, -0.1800836914, -0.3518996425,  0.2486747833,  0.6257938167},
147                 { 0.0536360918, -0.1339297778,  0.2241579764, -0.0195327484, -0.0054103808,  0.0347564518,  0.5120802482, -0.0329902864},
148                 {-0.5933332356, -0.2488721082,  0.2357173629,  0.0177285473,  0.0856630593, -0.3567126300, -0.1600668126, -0.1010899621},
149                 {-0.0514349819, -0.0854319435,  0.1125050061,  0.0063453560, -0.2250000688, -0.2209343090,  0.1964623477, -0.1512329924},
150                 { 0.0197395947, -0.1997170581, -0.1425959019, -0.2749477910, -0.0969467073,  0.0603688520, -0.2826905192,  0.1794315473}};
151         RealMatrix m = MatrixUtils.createRealMatrix(data);
152         RealMatrix s = checkAEqualPTPt(m);
153         checkSchurForm(s);
154     }
155 
156     ///////////////////////////////////////////////////////////////////////////
157     // Test helpers
158     ///////////////////////////////////////////////////////////////////////////
159 
160     private RealMatrix checkAEqualPTPt(RealMatrix matrix) {
161         SchurTransformer transformer = new SchurTransformer(matrix);
162         RealMatrix p  = transformer.getP();
163         RealMatrix t  = transformer.getT();
164         RealMatrix pT = transformer.getPT();
165 
166         RealMatrix result = p.multiply(t).multiply(pT);
167 
168         double norm = result.subtract(matrix).getNorm1();
169         Assert.assertEquals(0, norm, 1.0e-9);
170 
171         return t;
172     }
173 
174     private void checkOrthogonal(RealMatrix m) {
175         RealMatrix mTm = m.transposeMultiply(m);
176         RealMatrix id  = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
177         Assert.assertEquals(0, mTm.subtract(id).getNorm1(), 1.0e-14);
178     }
179 
180     private void checkSchurForm(final RealMatrix m) {
181         final int rows = m.getRowDimension();
182         final int cols = m.getColumnDimension();
183         for (int i = 0; i < rows; ++i) {
184             for (int j = 0; j < cols; ++j) {
185                 if (i > j + 1) {
186                     Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
187                 }
188             }
189         }
190     }
191 
192     @SuppressWarnings("unused")
193     private void checkMatricesValues(double[][] matrix, double[][] pRef, double[][] hRef) {
194 
195         SchurTransformer transformer =
196             new SchurTransformer(MatrixUtils.createRealMatrix(matrix));
197 
198         // check values against known references
199         RealMatrix p = transformer.getP();
200         Assert.assertEquals(0, p.subtract(MatrixUtils.createRealMatrix(pRef)).getNorm1(), 1.0e-14);
201 
202         RealMatrix t = transformer.getT();
203         Assert.assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(hRef)).getNorm1(), 1.0e-14);
204 
205         // check the same cached instance is returned the second time
206         Assert.assertTrue(p == transformer.getP());
207         Assert.assertTrue(t == transformer.getT());
208     }
209 }