Skip to content

Commit 3968a1e

Browse files
authored
Update ChebyshevIterationTest.java
1 parent b315882 commit 3968a1e

File tree

1 file changed

+96
-36
lines changed

1 file changed

+96
-36
lines changed

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

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,115 @@
55

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

8-
/**
9-
* Unit tests for ChebyshevIteration class
10-
*/
11-
class ChebyshevIterationTest {
8+
public class ChebyshevTest {
129

13-
private static final double[][] M1_A = {{4.0, 1.0}, {1.0, 3.0}};
14-
private static final double[] M1_B = {1.0, 2.0};
15-
private static final double[] M1_X0 = {0.0, 0.0};
16-
private static final double M1_LAMBDA_MIN = (7.0 - Math.sqrt(5.0)) / 2.0;
17-
private static final double M1_LAMBDA_MAX = (7.0 + Math.sqrt(5.0)) / 2.0;
18-
private static final double[] M1_EXPECTED = {1.0 / 11.0, 7.0 / 11.0};
10+
@Test
11+
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]
16+
double[][] a = { { 2, 0 }, { 0, 1 } };
17+
double[] b = { 2, 2 };
18+
double[] x0 = { 0, 0 };
19+
double minEig = 1.0;
20+
double maxEig = 2.0;
21+
int maxIter = 50;
22+
double tol = 1e-9;
23+
double[] expected = { 1.0, 2.0 };
24+
25+
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
26+
assertArrayEquals(expected, result, 1e-9);
27+
}
28+
29+
@Test
30+
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]
36+
double[][] a = { { 4, 1 }, { 1, 3 } };
37+
double[] b = { 1, 2 };
38+
double[] x0 = { 0, 0 };
39+
double minEig = (7.0 - Math.sqrt(5.0)) / 2.0;
40+
double maxEig = (7.0 + Math.sqrt(5.0)) / 2.0;
41+
int maxIter = 100;
42+
double tol = 1e-10;
43+
double[] expected = { 1.0 / 11.0, 7.0 / 11.0 };
44+
45+
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
46+
assertArrayEquals(expected, result, 1e-9);
47+
}
1948

20-
private static final double[][] M2_A = {{5.0, 0.0, 0.0}, {0.0, 2.0, 0.0}, {0.0, 0.0, 8.0}};
21-
private static final double[] M2_B = {10.0, -4.0, 24.0};
22-
private static final double[] M2_X0 = {0.0, 0.0, 0.0};
23-
private static final double[] M2_EXPECTED = {2.0, -2.0, 3.0};
24-
private static final double M2_LAMBDA_MIN = 2.0;
25-
private static final double M2_LAMBDA_MAX = 8.0;
49+
@Test
50+
public void testAlreadyAtSolution() {
51+
// Test if the initial guess is already the solution
52+
double[][] a = { { 2, 0 }, { 0, 1 } };
53+
double[] b = { 2, 2 };
54+
double[] x0 = { 1, 2 }; // Initial guess is the solution
55+
double minEig = 1.0;
56+
double maxEig = 2.0;
57+
int maxIter = 10;
58+
double tol = 1e-5;
59+
double[] expected = { 1.0, 2.0 };
2660

27-
private static final int MAX_ITERATIONS = 1000;
28-
private static final double TOLERANCE = 1e-10;
29-
private static final double ASSERT_TOLERANCE = 1e-9;
61+
double[] result = Chebyshev.solve(a, b, x0, minEig, maxEig, maxIter, tol);
62+
assertArrayEquals(expected, result, 0.0); // Should be exact
63+
}
64+
65+
@Test
66+
public void testMismatchedDimensionsAB() {
67+
double[][] a = { { 1, 0 }, { 0, 1 } };
68+
double[] b = { 1 }; // Should be length 2
69+
double[] x0 = { 0, 0 };
70+
assertThrows(IllegalArgumentException.class, () -> Chebyshev.solve(a, b, x0, 1, 2, 10, 1e-5));
71+
}
72+
73+
@Test
74+
public void testMismatchedDimensionsAX() {
75+
double[][] a = { { 1, 0 }, { 0, 1 } };
76+
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));
79+
}
3080

3181
@Test
32-
void testSolve2x2System() {
33-
double[] solution = ChebyshevIteration.solve(
34-
M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
35-
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
82+
public void testNonSquareMatrix() {
83+
double[][] a = { { 1, 0, 0 }, { 0, 1, 0 } }; // 2x3
84+
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));
3687
}
3788

3889
@Test
39-
void testSolve3x3System() {
40-
double[] solution = ChebyshevIteration.solve(
41-
M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
42-
assertArrayEquals(M2_EXPECTED, solution, ASSERT_TOLERANCE);
90+
public void testInvalidEigenvalues() {
91+
double[][] a = { { 1, 0 }, { 0, 1 } };
92+
double[] b = { 1, 1 };
93+
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));
4398
}
4499

45100
@Test
46-
void testAlreadyConverged() {
47-
double[] solution = ChebyshevIteration.solve(
48-
M1_A, M1_B, M1_EXPECTED, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
49-
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
101+
public void testNonPositiveDefinite() {
102+
double[][] a = { { 1, 0 }, { 0, 1 } };
103+
double[] b = { 1, 1 };
104+
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));
50109
}
51110

52111
@Test
53-
void testInvalidEigenvalues() {
54-
assertThrows(IllegalArgumentException.class, () ->
55-
ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 2.0, 1.0, 10, 1e-5));
56-
assertThrows(IllegalArgumentException.class, () ->
57-
ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 0.0, 2.0, 10, 1e-5));
112+
public void testInvalidIterationCount() {
113+
double[][] a = { { 1, 0 }, { 0, 1 } };
114+
double[] b = { 1, 1 };
115+
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));
58118
}
59119
}

0 commit comments

Comments
 (0)