BiCGstab discards a converged solution when convergence lands on the last permitted iteration

Open Beginner friendly
#3,098 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@Rahul19771 is already working on this.

Since Nov 18, 2026.

  • #3108 by @Rahul19771 — open
  • #3132 by @RAJ-A58 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lballabio/QuantLib

All issues in lballabio/QuantLib

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.