Skip to content

Commit 5226d9b

Browse files
authored
Update ChebyshevIterationTest.java
1 parent 3cd55db commit 5226d9b

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

src/test/java/com/thealgorithms/maths/ChebyshevIterationTest.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55

66
import org.junit.jupiter.api.Test;
77

8-
public class ChebyshevTest {
8+
public class ChebyshevIterationTest {
99

1010
@Test
1111
public void testSolveSimple2x2Diagonal() {
12-
// A = [[2, 0], [0, 1]] (SPD)
13-
// Eigenvalues: m=1, M=2
14-
// b = [2, 2]
15-
// Exact solution: x = [1, 2]
1612
double[][] a = { { 2, 0 }, { 0, 1 } };
1713
double[] b = { 2, 2 };
1814
double[] x0 = { 0, 0 };
@@ -22,17 +18,12 @@ public void testSolveSimple2x2Diagonal() {
2218
double tol = 1e-9;
2319
double[] expected = { 1.0, 2.0 };
2420

25-
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
21+
double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
2622
assertArrayEquals(expected, result, 1e-9);
2723
}
2824

2925
@Test
3026
public void testSolve2x2Symmetric() {
31-
// A = [[4, 1], [1, 3]] (SPD)
32-
// Eigenvalues = (7 +/- sqrt(5)) / 2
33-
// m ≈ 2.3819, M ≈ 4.6180
34-
// b = [1, 2]
35-
// Exact solution: x = [1/11, 7/11]
3627
double[][] a = { { 4, 1 }, { 1, 3 } };
3728
double[] b = { 1, 2 };
3829
double[] x0 = { 0, 0 };
@@ -42,78 +33,73 @@ public void testSolve2x2Symmetric() {
4233
double tol = 1e-10;
4334
double[] expected = { 1.0 / 11.0, 7.0 / 11.0 };
4435

45-
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
36+
double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
4637
assertArrayEquals(expected, result, 1e-9);
4738
}
4839

4940
@Test
5041
public void testAlreadyAtSolution() {
51-
// Test if the initial guess is already the solution
5242
double[][] a = { { 2, 0 }, { 0, 1 } };
5343
double[] b = { 2, 2 };
54-
double[] x0 = { 1, 2 }; // Initial guess is the solution
44+
double[] x0 = { 1, 2 };
5545
double minEig = 1.0;
5646
double maxEig = 2.0;
5747
int maxIter = 10;
5848
double tol = 1e-5;
5949
double[] expected = { 1.0, 2.0 };
6050

61-
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
62-
assertArrayEquals(expected, result, 0.0); // Should be exact
51+
double[] result = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
52+
assertArrayEquals(expected, result, 0.0);
6353
}
6454

6555
@Test
6656
public void testMismatchedDimensionsAB() {
6757
double[][] a = { { 1, 0 }, { 0, 1 } };
68-
double[] b = { 1 }; // Should be length 2
58+
double[] b = { 1 };
6959
double[] x0 = { 0, 0 };
70-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, 10, 1e-5));
60+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5));
7161
}
7262

7363
@Test
7464
public void testMismatchedDimensionsAX() {
7565
double[][] a = { { 1, 0 }, { 0, 1 } };
7666
double[] b = { 1, 1 };
77-
double[] x0 = { 0 }; // Should be length 2
78-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, 10, 1e-5));
67+
double[] x0 = { 0 };
68+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5));
7969
}
8070

8171
@Test
8272
public void testNonSquareMatrix() {
83-
double[][] a = { { 1, 0, 0 }, { 0, 1, 0 } }; // 2x3
73+
double[][] a = { { 1, 0, 0 }, { 0, 1, 0 } };
8474
double[] b = { 1, 1 };
85-
double[] x0 = { 0, 0 }; // This check will fail first
86-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, 10, 1e-5));
75+
double[] x0 = { 0, 0 };
76+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 10, 1e-5));
8777
}
8878

8979
@Test
9080
public void testInvalidEigenvalues() {
9181
double[][] a = { { 1, 0 }, { 0, 1 } };
9282
double[] b = { 1, 1 };
9383
double[] x0 = { 0, 0 };
94-
// min > max
95-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 2, 1, 10, 1e-5));
96-
// min == max
97-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 1, 10, 1e-5));
84+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 2, 1, 10, 1e-5));
85+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 1, 10, 1e-5));
9886
}
9987

10088
@Test
10189
public void testNonPositiveDefinite() {
10290
double[][] a = { { 1, 0 }, { 0, 1 } };
10391
double[] b = { 1, 1 };
10492
double[] x0 = { 0, 0 };
105-
// min = 0
106-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 0, 1, 10, 1e-5));
107-
// min < 0
108-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, -1, 1, 10, 1e-5));
93+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 0, 1, 10, 1e-5));
94+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, -1, 1, 10, 1e-5));
10995
}
11096

11197
@Test
11298
public void testInvalidIterationCount() {
11399
double[][] a = { { 1, 0 }, { 0, 1 } };
114100
double[] b = { 1, 1 };
115101
double[] x0 = { 0, 0 };
116-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, 0, 1e-5));
117-
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, -1, 1e-5));
102+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, 0, 1e-5));
103+
assertThrows(IllegalArgumentException.class, () -> ChebyshevIteration.solve(a, b, x0, 1, 2, -1, 1e-5));
118104
}
119105
}

0 commit comments

Comments
 (0)