BiCGstab discards a converged solution when convergence lands on the last permitted iteration
Maintainers usually reply within 3 days
@Rahul19771 is already working on this.
Since Nov 18, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start with ql/math/matrixutilities/bicgstab.cpp and inspect the final convergence checks in BiCGstab::solve, including the s-break path. Check for existing BiCGstab tests and add or run a case where convergence occurs on the last permitted iteration. Done means a converged iterate is returned at that boundary while an unconverged run still reports iteration exhaustion.
Written by the indexing model from the issue text.
Description
Summary
BiCGstab::solve (ql/math/matrixutilities/bicgstab.cpp) tests the iteration counter before the error criterion after the loop:
QL_REQUIRE(i < maxIter_, "max number of iterations exceeded");
QL_REQUIRE(error < relTol_, "could not converge");
When convergence is achieved on exactly the last permitted iteration, the first require fires and the solver throws "max number of iterations exceeded", discarding an iterate that already satisfies the tolerance. The identical system with maxIter one larger returns normally — the outcome depends on the budget, not the achieved accuracy.
The mid-loop exit path behaves oppositely at the same boundary: when the s-break fires on the final iteration, the loop exits with i < maxIter_ and the converged result is returned. So two runs that perform the same final iteration and reach the same error can differ in outcome purely by which exit path they took.
Reproduction
import QuantLib as ql
class MatMult:
def __init__(self, A): self.A = A
def __call__(self, x):
y = ql.Array(len(x))
for i in range(len(x)):
y[i] = sum(self.A[i][j] * x[j] for j in range(len(x)))
return y
A = ql.Matrix(3, 3)
for i, row in enumerate([[1.0, -2.0, -3.0],
[-2.0, 2.0, -2.0],
[-1.0, -3.0, -2.0]]):
for j, v in enumerate(row):
A[i][j] = v
b = ql.Array(3, 1.0); b[2] = 2.0
for maxIter in (2, 3):
try:
solver = ql.BiCGstab(ql.MatrixMultiplicationProxy(MatMult(A)), maxIter, 1e-12)
x = solver.solve(b)
print(f"maxIter={maxIter}: returns x = [{x[0]}, {x[1]}, {x[2]}]")
except Exception as e:
print(f"maxIter={maxIter}: throws: {e}")
Output:
maxIter=2: throws: max number of iterations exceeded
maxIter=3: returns x = [-0.5, -0.3, -0.3]
Both runs execute the same two iterations. The discarded iterate at maxIter=2 is exact: relative residual 5.7e-18 (tolerance 1e-12), agreement with the direct solution at 1.2e-16, relative residual of A·x−b at 9.1e-17.
Expected behavior
A solver that reaches error < relTol_ should report convergence regardless of which exit path fired. A caller sizing maxIter from a complexity estimate (as calibration loops do) loses a fully converged solution to an off-by-one in the budget.
The natural reordering is to test the error criterion before the counter (or to treat counter exhaustion as failure only when the error criterion is also unmet), which preserves the failure for genuinely unconverged runs.
Environment: QuantLib-Python 1.44; the same guard structure is present in current master.
Prepared with AI assistance (ZCode/GLM-5.3-flash).
- Dominant language
- C++
- Stars
- 7.7k
- Forks
- 2.3k
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 100
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from lballabio/QuantLib
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
lballabio/QuantLib#3134 · 1 comment ·
Maintainers usually reply within 3 days
-
Solver1D tests: Probe in test-suite/solvers.cpp has the wrong derivative signPossibly taken @igitur claimed this 6 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 35/100
lballabio/QuantLib#3114 · 1 comment ·
Maintainers usually reply within 3 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
lballabio/QuantLib#3106 · 2 comments ·
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 28/100
lballabio/QuantLib#3105 · 1 comment ·
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
lballabio/QuantLib#3093 · 2 comments ·
Maintainers usually reply within 3 days
All issues in lballabio/QuantLib
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
patrick-hudson/put.gd-desktop#2044 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
uxlfoundation/oneDNN#6604 · 1 comment ·
Maintainers usually reply within 2 days