1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.linear;
18
19 import org.junit.Assert;
20 import org.hipparchus.UnitTestUtils;
21 import org.hipparchus.complex.Complex;
22 import org.junit.Test;
23
24 public class OrderedComplexEigenDecompositionTest {
25
26 private final RealMatrix A = MatrixUtils.createRealMatrix(new double[][] {
27 { 3, -2 }, { 4, -1 } });
28
29 @Test
30 public void testDefinition() {
31 OrderedComplexEigenDecomposition eigenDecomp = new OrderedComplexEigenDecomposition(A);
32
33 FieldMatrix<Complex> A = MatrixUtils.createFieldMatrix(new Complex[][] {
34 { new Complex(3, 0), new Complex(-2, 0) },
35 { new Complex(4, 0), new Complex(-1, 0) } });
36
37 double threshold = 1.0e-15;
38
39
40 compareVectors(A.operate(eigenDecomp.getEigenvector(0)),
41 eigenDecomp.getEigenvector(0).mapMultiply(eigenDecomp.getEigenvalues()[0]),
42 threshold);
43
44
45 compareVectors(A.operate(eigenDecomp.getEigenvector(1)),
46 eigenDecomp.getEigenvector(1).mapMultiply(eigenDecomp.getEigenvalues()[1]),
47 threshold);
48
49
50 compareMatrices(A.multiply(eigenDecomp.getV()),
51 eigenDecomp.getV().multiply(eigenDecomp.getD()),
52 threshold);
53
54 }
55
56 @Test
57 public void testIssue173() {
58
59 OrderedComplexEigenDecomposition eigenDecomp = new OrderedComplexEigenDecomposition(A);
60
61 UnitTestUtils.assertEquals(eigenDecomp.getEigenvalues()[0], eigenDecomp.getD().getEntry(0, 0), 1.0e-10);
62 UnitTestUtils.assertEquals(eigenDecomp.getEigenvalues()[1], eigenDecomp.getD().getEntry(1, 1), 1.0e-10);
63
64 UnitTestUtils.assertEquals(eigenDecomp.getEigenvector(0).getEntry(0), eigenDecomp.getV().getEntry(0, 0), 1.0e-10);
65 UnitTestUtils.assertEquals(eigenDecomp.getEigenvector(0).getEntry(1), eigenDecomp.getV().getEntry(1, 0), 1.0e-10);
66 UnitTestUtils.assertEquals(eigenDecomp.getEigenvector(1).getEntry(0), eigenDecomp.getV().getEntry(0, 1), 1.0e-10);
67 UnitTestUtils.assertEquals(eigenDecomp.getEigenvector(1).getEntry(1), eigenDecomp.getV().getEntry(1, 0), 1.0e-10);
68
69 }
70
71 @Test
72 public void testEqualEigenValues() {
73 double[][] d = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
74 Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(d);
75 ComplexEigenDecomposition ed = new OrderedComplexEigenDecomposition(matrix);
76 for (Complex z : ed.getEigenvalues()) {
77 Assert.assertEquals(1.0, z.getReal(), 1.0e-15);
78 Assert.assertEquals(0.0, z.getImaginary(), 1.0e-15);
79 }
80 }
81
82 @Test
83 public void testIssue111A() {
84 RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
85 {-0.0,0.0000002463,-0.000000462,0.0,-0.2843196976,-0.0,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
86 {-0.0000002463,-0.0,0.0000004577,0.0,-0.0,-0.2843196976,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
87 {0.000000462,-0.0000004577,-0.0,0.0,-0.0,-0.0,-0.2843196976,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
88 {-0.0000004577,-0.000000462,-0.0000002463,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
89 {0.0,0.0,0.0,0.0,-0.0000000015,0.0000005751,-0.0000015871,0.0,0.0,0.0,0.0,-0.0000104075,0.0,0.0},
90 {0.0,0.0,0.0,0.0,-0.000000421,-0.0000000048,0.0000013528,0.0,0.0,0.0,0.0,0.0,-0.000007717,0.0},
91 {0.0,0.0,0.0,0.0,0.0000005375,-0.00000062,0.0000000063,0.0,0.0,0.0,0.0,0.0,0.0,-0.0000035509},
92 {-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0000002463,-0.000000462,0.0000004577,0.0,0.0,0.0},
93 {0.0,-1.0,0.0,0.0,0.0,0.0,0.0,-0.0000002463,0.0,0.0000004577,0.000000462,0.0,0.0,0.0},
94 {0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.000000462,-0.0000004577,0.0,0.0000002463,0.0,0.0,0.0},
95 {0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
96 {0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.2843196976,0.0,0.0,0.0,0.0000000015,0.000000421,-0.0000005375},
97 {0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.2843196976,0.0,0.0,-0.0000005751,0.0000000048,0.00000062},
98 {0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.2843196976,0.0,0.0000015871,-0.0000013528,-0.0000000063}
99 });
100
101 Assert.assertNotNull(new OrderedComplexEigenDecomposition(m,
102 1.0e-6,
103 ComplexEigenDecomposition.DEFAULT_EPSILON,
104 ComplexEigenDecomposition.DEFAULT_EPSILON_AV_VD_CHECK));
105
106 }
107
108
109 @Test
110 public void testIssue111B() {
111 RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
112 {-0.0,0.0000002463,-0.000000462,0.0,-0.1095029805,-0.0,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
113 {-0.0000002463,-0.0,0.0000004577,0.0,-0.0,-0.1095029805,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
114 {0.000000462,-0.0000004577,-0.0,0.0,-0.0,-0.0,-0.1095029805,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
115 {-0.0000004577,-0.000000462,-0.0000002463,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
116 {0.0,0.0,0.0,0.0,-0.0000000015,0.0000005751,-0.0000015871,0.0,0.0,0.0,0.0,-0.0000104075,0.0,0.0},
117 {0.0,0.0,0.0,0.0,-0.000000421,-0.0000000048,0.0000013528,0.0,0.0,0.0,0.0,0.0,-0.000007717,0.0},
118 {0.0,0.0,0.0,0.0,0.0000005375,-0.00000062,0.0000000063,0.0,0.0,0.0,0.0,0.0,0.0,-0.0000035509},
119 {-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0000002463,-0.000000462,0.0000004577,0.0,0.0,0.0},
120 {0.0,-1.0,0.0,0.0,0.0,0.0,0.0,-0.0000002463,0.0,0.0000004577,0.000000462,0.0,0.0,0.0},
121 {0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.000000462,-0.0000004577,0.0,0.0000002463,0.0,0.0,0.0},
122 {0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
123 {0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.1095029805,0.0,0.0,0.0,0.0000000015,0.000000421,-0.0000005375},
124 {0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.1095029805,0.0,0.0,-0.0000005751,0.0000000048,0.00000062},
125 {0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.1095029805,0.0,0.0000015871,-0.0000013528,-0.0000000063}
126 });
127
128 Assert.assertNotNull(new OrderedComplexEigenDecomposition(m,
129 1.0e-6,
130 ComplexEigenDecomposition.DEFAULT_EPSILON,
131 ComplexEigenDecomposition.DEFAULT_EPSILON_AV_VD_CHECK));
132
133 }
134
135 @Test
136 public void testIssue111C() {
137 RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
138 {-0.0,0.0000002463,-0.000000462,0.0,-0.109502989,-0.0,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
139 {-0.0000002463,-0.0,0.0000004577,0.0,-0.0,-0.109502989,-0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
140 {0.000000462,-0.0000004577,-0.0,0.0,-0.0,-0.0,-0.109502989,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
141 {-0.0000004577,-0.000000462,-0.0000002463,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
142 {0.0,0.0,0.0,0.0,-0.0000000015,0.0000005751,-0.0000015871,0.0,0.0,0.0,0.0,-0.0000104075,0.0,0.0},
143 {0.0,0.0,0.0,0.0,-0.000000421,-0.0000000048,0.0000013528,0.0,0.0,0.0,0.0,0.0,-0.000007717,0.0},
144 {0.0,0.0,0.0,0.0,0.0000005375,-0.00000062,0.0000000063,0.0,0.0,0.0,0.0,0.0,0.0,-0.0000035509},
145 {-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0000002463,-0.000000462,0.0000004577,0.0,0.0,0.0},
146 {0.0,-1.0,0.0,0.0,0.0,0.0,0.0,-0.0000002463,0.0,0.0000004577,0.000000462,0.0,0.0,0.0},
147 {0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.000000462,-0.0000004577,0.0,0.0000002463,0.0,0.0,0.0},
148 {0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
149 {0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.109502989,0.0,0.0,0.0,0.0000000015,0.000000421,-0.0000005375},
150 {0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.109502989,0.0,0.0,-0.0000005751,0.0000000048,0.00000062},
151 {0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.109502989,0.0,0.0000015871,-0.0000013528,-0.0000000063}
152 });
153
154 Assert.assertNotNull(new OrderedComplexEigenDecomposition(m,
155 1.0e-6,
156 ComplexEigenDecomposition.DEFAULT_EPSILON,
157 ComplexEigenDecomposition.DEFAULT_EPSILON_AV_VD_CHECK));
158
159 }
160
161
162
163
164
165
166
167 private static void compareVectors(final FieldVector<Complex> vector1,
168 final FieldVector<Complex> vector2,
169 final double threshold) {
170
171
172 Assert.assertEquals(vector1.getDimension(), vector2.getDimension());
173
174
175 for (int index = 0; index < vector1.getDimension(); index++) {
176 final Complex complex1 = vector1.getEntry(index);
177 final Complex complex2 = vector2.getEntry(index);
178 Assert.assertEquals(complex1.getReal(), complex2.getReal(), threshold);
179 Assert.assertEquals(complex1.getImaginary(), complex2.getImaginary(), threshold);
180 }
181
182 }
183
184
185
186
187
188
189
190 private static void compareMatrices(final FieldMatrix<Complex> matrix1,
191 final FieldMatrix<Complex> matrix2,
192 final double threshold) {
193
194
195 Assert.assertEquals(matrix1.getRowDimension(), matrix2.getRowDimension());
196 Assert.assertEquals(matrix1.getColumnDimension(), matrix2.getColumnDimension());
197
198
199 for (int row = 0; row < matrix1.getRowDimension(); row++) {
200 for (int column = 0; column < matrix1.getColumnDimension(); column++) {
201 final Complex complex1 = matrix1.getEntry(row, column);
202 final Complex complex2 = matrix2.getEntry(row, column);
203 Assert.assertEquals(complex1.getReal(), complex2.getReal(), threshold);
204 Assert.assertEquals(complex1.getImaginary(), complex2.getImaginary(), threshold);
205 }
206
207 }
208
209 }
210
211 }