Skip to content

Commit b4e47fc

Browse files
authored
Update ChebyshevIteration.java
1 parent 61a5fbf commit b4e47fc

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* equations (Ax = b).
88
*
99
* This iterative method requires:
10-
* - Matrix A to be symmetric positive definite (SPD)
10+
* - Matrix a to be symmetric positive definite (SPD)
1111
* - Knowledge of minimum (lambdaMin) and maximum (lambdaMax) eigenvalues
1212
*
1313
* Reference: https://en.wikipedia.org/wiki/Chebyshev_iteration
@@ -20,9 +20,9 @@ private ChebyshevIteration() {
2020
}
2121

2222
/**
23-
* Solves Ax = b using Chebyshev Iteration.
23+
* Solves ax = b using Chebyshev Iteration.
2424
*
25-
* @param A SPD matrix
25+
* @param a SPD matrix
2626
* @param b vector b
2727
* @param x0 initial guess
2828
* @param lambdaMin minimum eigenvalue
@@ -31,14 +31,13 @@ private ChebyshevIteration() {
3131
* @param tolerance convergence tolerance
3232
* @return solution vector x
3333
*/
34-
public static double[] solve(double[][] A, double[] b, double[] x0,
35-
double lambdaMin, double lambdaMax,
34+
public static double[] solve(double[][] a, double[] b, double[] x0, double lambdaMin, double lambdaMax,
3635
int maxIterations, double tolerance) {
37-
validateInputs(A, b, x0, lambdaMin, lambdaMax);
36+
validateInputs(a, b, x0, lambdaMin, lambdaMax);
3837

3938
int n = b.length;
4039
double[] x = Arrays.copyOf(x0, n);
41-
double[] r = vectorSubtract(b, matrixVectorMultiply(A, x));
40+
double[] r = vectorSubtract(b, matrixVectorMultiply(a, x));
4241
double[] p = new double[n];
4342
double alpha = 0.0;
4443
double beta = 0.0;
@@ -62,7 +61,7 @@ public static double[] solve(double[][] A, double[] b, double[] x0,
6261
}
6362

6463
x = vectorAdd(x, vectorScale(p, alpha));
65-
r = vectorSubtract(b, matrixVectorMultiply(A, x));
64+
r = vectorSubtract(b, matrixVectorMultiply(a, x));
6665

6766
if (vectorNorm(r) < tolerance) {
6867
break;
@@ -72,25 +71,29 @@ public static double[] solve(double[][] A, double[] b, double[] x0,
7271
return x;
7372
}
7473

75-
private static void validateInputs(double[][] A, double[] b, double[] x0,
76-
double lambdaMin, double lambdaMax) {
74+
private static void validateInputs(double[][] a, double[] b, double[] x0, double lambdaMin, double lambdaMax) {
7775
int n = b.length;
78-
if (n == 0) throw new IllegalArgumentException("Vectors cannot be empty.");
79-
if (A.length != n || A[0].length != n)
80-
throw new IllegalArgumentException("Matrix A must be square (n x n).");
81-
if (x0.length != n)
76+
if (n == 0) {
77+
throw new IllegalArgumentException("Vectors cannot be empty.");
78+
}
79+
if (a.length != n || a[0].length != n) {
80+
throw new IllegalArgumentException("Matrix a must be square (n x n).");
81+
}
82+
if (x0.length != n) {
8283
throw new IllegalArgumentException("Initial guess vector x0 must have length n.");
83-
if (lambdaMin >= lambdaMax || lambdaMin <= 0)
84+
}
85+
if (lambdaMin >= lambdaMax || lambdaMin <= 0) {
8486
throw new IllegalArgumentException("Eigenvalues must satisfy 0 < lambdaMin < lambdaMax.");
87+
}
8588
}
8689

87-
private static double[] matrixVectorMultiply(double[][] A, double[] x) {
88-
int n = A.length;
90+
private static double[] matrixVectorMultiply(double[][] a, double[] x) {
91+
int n = a.length;
8992
double[] y = new double[n];
9093
for (int i = 0; i < n; i++) {
9194
double sum = 0.0;
9295
for (int j = 0; j < n; j++) {
93-
sum += A[i][j] * x[j];
96+
sum += a[i][j] * x[j];
9497
}
9598
y[i] = sum;
9699
}
@@ -100,27 +103,35 @@ private static double[] matrixVectorMultiply(double[][] A, double[] x) {
100103
private static double[] vectorAdd(double[] a, double[] b) {
101104
int n = a.length;
102105
double[] c = new double[n];
103-
for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
106+
for (int i = 0; i < n; i++) {
107+
c[i] = a[i] + b[i];
108+
}
104109
return c;
105110
}
106111

107112
private static double[] vectorSubtract(double[] a, double[] b) {
108113
int n = a.length;
109114
double[] c = new double[n];
110-
for (int i = 0; i < n; i++) c[i] = a[i] - b[i];
115+
for (int i = 0; i < n; i++) {
116+
c[i] = a[i] - b[i];
117+
}
111118
return c;
112119
}
113120

114121
private static double[] vectorScale(double[] a, double scalar) {
115122
int n = a.length;
116123
double[] c = new double[n];
117-
for (int i = 0; i < n; i++) c[i] = a[i] * scalar;
124+
for (int i = 0; i < n; i++) {
125+
c[i] = a[i] * scalar;
126+
}
118127
return c;
119128
}
120129

121130
private static double vectorNorm(double[] a) {
122131
double sum = 0.0;
123-
for (double val : a) sum += val * val;
132+
for (double val : a) {
133+
sum += val * val;
134+
}
124135
return Math.sqrt(sum);
125136
}
126137
}

0 commit comments

Comments
 (0)