diff --git a/src/arnoldi.jl b/src/arnoldi.jl index 9f84a362..3d5a1d9e 100644 --- a/src/arnoldi.jl +++ b/src/arnoldi.jl @@ -135,7 +135,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 @@ -299,7 +299,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=false, ) where {AT, T, U} x, y = @view(V[:, j]), @view(V[:, j + 1]) applyA!(y, A, x, V, j, n, p) @@ -312,6 +313,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 β @@ -338,6 +347,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 @@ -358,7 +368,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 @@ -376,7 +387,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=reorth) if beta < tol # happy-breakdown Ks.m = j Ks.wasbreakdown = true 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