Skip to content

Commit d66c700

Browse files
authored
Update ChebyshevIteration.java
1 parent 155ae56 commit d66c700

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,21 @@ public static double[] solve(
7373
alpha = 1.0 / d;
7474
p = Arrays.copyOf(r, n);
7575
} else {
76-
77-
// 1. Save the previous alpha
76+
// Fix for algorithmic bug (use alphaPrev)
7877
double alphaPrev = alpha;
7978

80-
// 2. Calculate new beta and alpha using alphaPrev
8179
beta = (c * alphaPrev / 2.0) * (c * alphaPrev / 2.0);
8280
alpha = 1.0 / (d - beta / alphaPrev);
8381

84-
// 3. Use alphaPrev in the p update
8582
double betaOverAlphaPrev = beta / alphaPrev;
8683
double[] rScaled = vectorScale(p, betaOverAlphaPrev);
8784
p = vectorAdd(r, rScaled);
88-
8985
}
9086

9187
double[] pScaled = vectorScale(p, alpha);
9288
x = vectorAdd(x, pScaled);
9389

9490
// Re-calculate residual to avoid accumulating floating-point errors
95-
// Note: Some variants calculate r = r - alpha * A * p for
96-
// efficiency,
97-
// but this direct calculation is more stable against drift.
9891
r = vectorSubtract(b, matrixVectorMultiply(A, x));
9992

10093
if (vectorNorm(r) < tolerance) {
@@ -195,4 +188,4 @@ private static double vectorNorm(double[] a) {
195188
}
196189
return Math.sqrt(sumOfSquares);
197190
}
198-
}
191+
}

0 commit comments

Comments
 (0)