Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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 β
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down