From 893b860d933195e0973d25abf4c734ce2ec15b60 Mon Sep 17 00:00:00 2001 From: "Christopher N. Everett" <151160791+cneverett@users.noreply.github.com> Date: Tue, 28 Jul 2026 19:58:02 +0100 Subject: [PATCH 1/3] Update arnoldi.jl Added an option to reorthogonalise (`reorth`) each basis vector during its construction in `arnoldi!`. This helps numerical roundoff from leading to poor basis orthogonalisation, especially when the Krylov subspace dimension is large. --- src/arnoldi.jl | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/arnoldi.jl b/src/arnoldi.jl index 743a323e..fd75a336 100644 --- a/src/arnoldi.jl +++ b/src/arnoldi.jl @@ -126,7 +126,7 @@ Perform Arnoldi iterations to obtain the Krylov subspace ``K_m(A, b)``. - `ishermitian`: select Lanczos for a Hermitian operator; defaults to `LinearAlgebra.ishermitian(A)`. - Additional keywords are forwarded to [`arnoldi!`](@ref), including `tol`, - `iop`, and `opnorm`. + `iop`, `opnorm`, and `reorth`. # Returns @@ -285,7 +285,8 @@ Take the `j` 'th step of the Lanczos iteration. function arnoldi_step!( j::Integer, iop::Integer, A::AT, V::AbstractMatrix{T}, H::AbstractMatrix{U}, - n::Int = -1, p::Int = -1 + n::Int = -1, p::Int = -1, + reorth::Bool, ) where {AT, T, U} x, y = @view(V[:, j]), @view(V[:, j + 1]) applyA!(y, A, x, V, j, n, p) @@ -298,6 +299,14 @@ function arnoldi_step!( α = H[i, j] = coeff(U, dot(@view(V[:, i]), y)) axpy!(-α, @view(V[:, i]), y) end + + if reorth # simple re-orthogonalisation applied to the same window as above + @inbounds for i in max(1, j - iop + 1):j + α = coeff(U, dot(@view(V[:, i]), y)) + H[i, j] += α + axpy!(-α, @view(V[:, i]), y) + end + end β = H[j + 1, j] = norm(y) @. y /= β return β @@ -324,6 +333,7 @@ Populate an existing [`KrylovSubspace`](@ref) without allocating its basis. - `iop`: incomplete-orthogonalization length; `0` performs full Arnoldi. - `init`, `t`, `mu`, `l`: advanced controls for continuing or augmented Krylov constructions. + - `reorth`: re-orthogonalise each new basis vector after construction # Returns @@ -335,7 +345,8 @@ function arnoldi!( ishermitian::Bool = LinearAlgebra.ishermitian(A isa Tuple ? first(A) : A), opnorm = nothing, iop::Int = 0, init::Int = 0, t::Number = NaN, mu::Number = NaN, - l::Int = -1 + l::Int = -1, + reorth = false, ) where {T1 <: Number, U <: Number, AT} Ks.wasbreakdown = false @@ -353,7 +364,7 @@ function arnoldi!( iszero(Ks.beta) && return Ks iszero(iop) && (iop = m) for j in init:m - beta = arnoldi_step!(j, iop, A, V, H, n, p) + beta = arnoldi_step!(j, iop, A, V, H, n, p, reorth) if beta < tol # happy-breakdown Ks.m = j Ks.wasbreakdown = true From cdbc6efa737d7987e470878c85dd474225f0873b Mon Sep 17 00:00:00 2001 From: "Christopher N. Everett" <151160791+cneverett@users.noreply.github.com> Date: Mon, 3 Aug 2026 16:58:06 +0100 Subject: [PATCH 2/3] Update arnoldi.jl `reorth` now a kwarg to `arnoldi_step!` set by default to `false` to avoid downstream breaking of functions --- src/arnoldi.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arnoldi.jl b/src/arnoldi.jl index fd75a336..f2d45f2e 100644 --- a/src/arnoldi.jl +++ b/src/arnoldi.jl @@ -285,8 +285,8 @@ Take the `j` 'th step of the Lanczos iteration. function arnoldi_step!( j::Integer, iop::Integer, A::AT, V::AbstractMatrix{T}, H::AbstractMatrix{U}, - n::Int = -1, p::Int = -1, - reorth::Bool, + n::Int = -1, p::Int = -1; + reorth::Bool=false, ) where {AT, T, U} x, y = @view(V[:, j]), @view(V[:, j + 1]) applyA!(y, A, x, V, j, n, p) @@ -364,7 +364,7 @@ function arnoldi!( iszero(Ks.beta) && return Ks iszero(iop) && (iop = m) for j in init:m - beta = arnoldi_step!(j, iop, A, V, H, n, p, reorth) + beta = arnoldi_step!(j, iop, A, V, H, n, p; reorth=reorth) if beta < tol # happy-breakdown Ks.m = j Ks.wasbreakdown = true From 530bdc80b5aa1a4170369b34a0b6ea72e8d4e543 Mon Sep 17 00:00:00 2001 From: cnever Date: Tue, 6 Oct 2026 16:05:14 +0100 Subject: [PATCH 3/3] added test case to demonstrate use of reorth option in Arnoldi to improve orthogonalisation of V matrix in cases of high stiffness or non-normal A matrices --- test/basictests.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/basictests.jl b/test/basictests.jl index ea622895..ad9a6dfa 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -699,6 +699,25 @@ end t = 0.1 Q = phiv(t, A, ones(n), 10) @test Q[:, 2] ≈ (t * A) \ (exp(t * A) - I) * ones(n) + + # Test reorthogonalisation with large, stiff, non-normal matrix + Random.seed!(0) + n = 20 + m = 5 + A = zeros(2n,2n) + @views(A[1:n,n+1:2n]) .= 1e10 .* randn(n, n) + @views(A[n+1:2n,n+1:2n]) .= randn(n, n) + @views(A[1:n,1:n]) .= randn(n, n) + b = randn(2n) + Ks = arnoldi(A, b; m = m,reorth=false) + V1 = getV(Ks)[:,1:Ks.m] + Ks = arnoldi(A, b; m = m,reorth=true) + V2 = getV(Ks)[:,1:Ks.m] + # first vectors are equal + @test V1[:,1] ≈ V2[:,1] + # non-reorth Arnoldi leads to poor orthogonalisation while reorth keeps this to near machine precision + @test norm(V1' * V1 - I) > 100eps(Float64) && norm(V2' * V2 - I) < 100eps(Float64) + end @testset "PhivCache reuse (issue: cache reallocated every call)" begin