Skip to content

Commit 24e219e

Browse files
authored
Update ChebyshevIteration.java
1 parent 693e02f commit 24e219e

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/main/java/com/thealgorithms/maths/ChebyshevIteration.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private ChebyshevIteration() {
3131
/**
3232
* Solves the linear system Ax = b using Chebyshev Iteration.
3333
*
34-
* @param A A symmetric positive definite matrix.
34+
* @param a A symmetric positive definite matrix.
3535
* @param b The vector 'b' in the equation Ax = b.
3636
* @param x0 An initial guess vector for 'x'.
3737
* @param lambdaMin The smallest eigenvalue of matrix A.
@@ -43,13 +43,12 @@ private ChebyshevIteration() {
4343
* match,
4444
* or if max/min eigenvalues are invalid.
4545
*/
46-
// FIX: Method signature flattened to one line
47-
public static double[] solve(double[][] A, double[] b, double[] x0, double lambdaMin, double lambdaMax, int maxIterations, double tolerance) {
48-
validateInputs(A, b, x0, lambdaMin, lambdaMax);
46+
public static double[] solve(double[][] a, double[] b, double[] x0, double lambdaMin, double lambdaMax, int maxIterations, double tolerance) {
47+
validateInputs(a, b, x0, lambdaMin, lambdaMax);
4948

5049
int n = b.length;
5150
double[] x = Arrays.copyOf(x0, n);
52-
double[] r = vectorSubtract(b, matrixVectorMultiply(A, x));
51+
double[] r = vectorSubtract(b, matrixVectorMultiply(a, x)); // Use `a`
5352
double[] p = new double[n];
5453
double alpha = 0.0;
5554
double beta = 0.0;
@@ -66,7 +65,6 @@ public static double[] solve(double[][] A, double[] b, double[] x0, double lambd
6665
alpha = 1.0 / d;
6766
p = Arrays.copyOf(r, n);
6867
} else {
69-
// Fix for algorithmic bug (use alphaPrev)
7068
double alphaPrev = alpha;
7169

7270
beta = (c * alphaPrev / 2.0) * (c * alphaPrev / 2.0);
@@ -81,7 +79,7 @@ public static double[] solve(double[][] A, double[] b, double[] x0, double lambd
8179
x = vectorAdd(x, pScaled);
8280

8381
// Re-calculate residual to avoid accumulating floating-point errors
84-
r = vectorSubtract(b, matrixVectorMultiply(A, x));
82+
r = vectorSubtract(b, matrixVectorMultiply(a, x)); // Use `a`
8583

8684
if (vectorNorm(r) < tolerance) {
8785
break; // Converged
@@ -91,12 +89,12 @@ public static double[] solve(double[][] A, double[] b, double[] x0, double lambd
9189
}
9290

9391
// --- Helper Methods for Linear Algebra ---
94-
private static void validateInputs(double[][] A, double[] b, double[] x0, double lambdaMin, double lambdaMax) {
92+
private static void validateInputs(double[][] a, double[] b, double[] x0, double lambdaMin, double lambdaMax) {
9593
int n = b.length;
9694
if (n == 0) {
9795
throw new IllegalArgumentException("Vectors cannot be empty.");
9896
}
99-
if (A.length != n || A[0].length != n) {
97+
if (a.length != n || a[0].length != n) { // Use `a`
10098
throw new IllegalArgumentException("Matrix A must be square with dimensions n x n.");
10199
}
102100
if (x0.length != n) {
@@ -110,13 +108,13 @@ private static void validateInputs(double[][] A, double[] b, double[] x0, double
110108
/**
111109
* Computes y = Ax
112110
*/
113-
private static double[] matrixVectorMultiply(double[][] A, double[] x) {
114-
int n = A.length;
111+
private static double[] matrixVectorMultiply(double[][] a, double[] x) {
112+
int n = a.length; // Use `a`
115113
double[] y = new double[n];
116114
for (int i = 0; i < n; i++) {
117115
double sum = 0.0;
118116
for (int j = 0; j < n; j++) {
119-
sum += A[i][j] * x[j];
117+
sum += a[i][j] * x[j]; // Use `a`
120118
}
121119
y[i] = sum;
122120
}

0 commit comments

Comments
 (0)