|
5 | 5 |
|
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
8 | | -public class ChebyshevIterationTest { |
| 8 | +public class ChebyshevIterationTest { |
9 | 9 |
|
10 | 10 | @Test |
11 | 11 | public void testSolveSimple2x2Diagonal() { |
12 | | - double[][] a = { { 2, 0 }, { 0, 1 } }; |
13 | | - double[] b = { 2, 2 }; |
14 | | - double[] x0 = { 0, 0 }; |
| 12 | + double[][] a = {{2, 0}, {0, 1}}; |
| 13 | + double[] b = {2, 2}; |
| 14 | + double[] x0 = {0, 0}; |
15 | 15 | double minEig = 1.0; |
16 | 16 | double maxEig = 2.0; |
17 | 17 | int maxIter = 50; |
18 | 18 | double tol = 1e-9; |
19 | | - double[] expected = { 1.0, 2.0 }; |
| 19 | + double[] expected = {1.0, 2.0}; |
20 | 20 |
|
21 | 21 | double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol); |
22 | 22 | assertArrayEquals(expected, result, 1e-9); |
23 | 23 | } |
24 | 24 |
|
25 | 25 | @Test |
26 | 26 | public void testSolve2x2Symmetric() { |
27 | | - double[][] a = { { 4, 1 }, { 1, 3 } }; |
28 | | - double[] b = { 1, 2 }; |
29 | | - double[] x0 = { 0, 0 }; |
| 27 | + double[][] a = {{4, 1}, {1, 3}}; |
| 28 | + double[] b = {1, 2}; |
| 29 | + double[] x0 = {0, 0}; |
30 | 30 | double minEig = (7.0 - Math.sqrt(5.0)) / 2.0; |
31 | 31 | double maxEig = (7.0 + Math.sqrt(5.0)) / 2.0; |
32 | 32 | int maxIter = 100; |
33 | 33 | double tol = 1e-10; |
34 | | - double[] expected = { 1.0 / 11.0, 7.0 / 11.0 }; |
| 34 | + double[] expected = {1.0 / 11.0, 7.0 / 11.0}; |
35 | 35 |
|
36 | 36 | double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol); |
37 | 37 | assertArrayEquals(expected, result, 1e-9); |
38 | 38 | } |
39 | 39 |
|
40 | 40 | @Test |
41 | 41 | public void testAlreadyAtSolution() { |
42 | | - double[][] a = { { 2, 0 }, { 0, 1 } }; |
43 | | - double[] b = { 2, 2 }; |
44 | | - double[] x0 = { 1, 2 }; |
| 42 | + double[][] a = {{2, 0}, {0, 1}}; |
| 43 | + double[] b = {2, 2}; |
| 44 | + double[] x0 = {1, 2}; |
45 | 45 | double minEig = 1.0; |
46 | 46 | double maxEig = 2.0; |
47 | 47 | int maxIter = 10; |
48 | 48 | double tol = 1e-5; |
49 | | - double[] expected = { 1.0, 2.0 }; |
| 49 | + double[] expected = {1.0, 2.0}; |
50 | 50 |
|
51 | 51 | double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol); |
52 | 52 | assertArrayEquals(expected, result, 0.0); |
53 | 53 | } |
54 | 54 |
|
55 | 55 | @Test |
56 | 56 | public void testMismatchedDimensionsAB() { |
57 | | - double[][] a = { { 1, 0 }, { 0, 1 } }; |
58 | | - double[] b = { 1 }; |
59 | | - double[] x0 = { 0, 0 }; |
| 57 | + double[][] a = {{1, 0}, {0, 1}}; |
| 58 | + double[] b = {1}; |
| 59 | + double[] x0 = {0, 0}; |
60 | 60 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5)); |
61 | 61 | } |
62 | 62 |
|
63 | 63 | @Test |
64 | 64 | public void testMismatchedDimensionsAX() { |
65 | | - double[][] a = { { 1, 0 }, { 0, 1 } }; |
66 | | - double[] b = { 1, 1 }; |
67 | | - double[] x0 = { 0 }; |
| 65 | + double[][] a = {{1, 0}, {0, 1}}; |
| 66 | + double[] b = {1, 1}; |
| 67 | + double[] x0 = {0}; |
68 | 68 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5)); |
69 | 69 | } |
70 | 70 |
|
71 | 71 | @Test |
72 | 72 | public void testNonSquareMatrix() { |
73 | | - double[][] a = { { 1, 0, 0 }, { 0, 1, 0 } }; |
74 | | - double[] b = { 1, 1 }; |
75 | | - double[] x0 = { 0, 0 }; |
| 73 | + double[][] a = {{1, 0, 0}, {0, 1, 0}}; |
| 74 | + double[] b = {1, 1}; |
| 75 | + double[] x0 = {0, 0}; |
76 | 76 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5)); |
77 | 77 | } |
78 | 78 |
|
79 | 79 | @Test |
80 | 80 | public void testInvalidEigenvalues() { |
81 | | - double[][] a = { { 1, 0 }, { 0, 1 } }; |
82 | | - double[] b = { 1, 1 }; |
83 | | - double[] x0 = { 0, 0 }; |
| 81 | + double[][] a = {{1, 0}, {0, 1}}; |
| 82 | + double[] b = {1, 1}; |
| 83 | + double[] x0 = {0, 0}; |
84 | 84 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 2, 1, 10, 1e-5)); |
85 | 85 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 1, 10, 1e-5)); |
86 | 86 | } |
87 | 87 |
|
88 | 88 | @Test |
89 | 89 | public void testNonPositiveDefinite() { |
90 | | - double[][] a = { { 1, 0 }, { 0, 1 } }; |
91 | | - double[] b = { 1, 1 }; |
92 | | - double[] x0 = { 0, 0 }; |
| 90 | + double[][] a = {{1, 0}, {0, 1}}; |
| 91 | + double[] b = {1, 1}; |
| 92 | + double[] x0 = {0, 0}; |
93 | 93 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 0, 1, 10, 1e-5)); |
94 | 94 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, -1, 1, 10, 1e-5)); |
95 | 95 | } |
96 | 96 |
|
97 | 97 | @Test |
98 | 98 | public void testInvalidIterationCount() { |
99 | | - double[][] a = { { 1, 0 }, { 0, 1 } }; |
100 | | - double[] b = { 1, 1 }; |
101 | | - double[] x0 = { 0, 0 }; |
| 99 | + double[][] a = {{1, 0}, {0, 1}}; |
| 100 | + double[] b = {1, 1}; |
| 101 | + double[] x0 = {0, 0}; |
102 | 102 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 0, 1e-5)); |
103 | 103 | assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, -1, 1e-5)); |
104 | 104 | } |
|
0 commit comments