Skip to content

Commit d4fcb97

Browse files
committed
Fix overflow in eig_trunc_pullback! and use accelerative_smith_iteration!
1 parent 7e53b95 commit d4fcb97

1 file changed

Lines changed: 11 additions & 28 deletions

File tree

‎src/pullbacks/eig.jl‎

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,36 +161,19 @@ function eig_trunc_pullback!(
161161
Z = ViG * VᴴΔAV
162162

163163
# add contribution from orthogonal complement
164-
AP = mul!(complex.(A), V * Dmat, ViG', -1, 1)
165-
X₀ = iszerotangent(ΔV₊) ? AP' * Z : mul!(ΔV₊, AP', Z, 1, 1)
164+
# build the adjoint of AP directly, since that is what the series is summed with
165+
APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1)
166+
X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1)
166167
X₀ ./= D'
167-
dabsmax = maximum(abs, D)
168-
AP ./= dabsmax
169-
D̄⁻¹ = dabsmax ./ conj.(D)
170-
X₁ = rmul!(AP' * X₀, Diagonal(D̄⁻¹))
171-
X₁ .+= X₀
172-
Xₖ, Xₖ₊₁ = X₁, X₀
173-
APₖ, APₖ₊₁ = AP * AP, AP
174-
D̄⁻¹ₖ, D̄⁻¹ₖ₊₁ = D̄⁻¹ .^ 2, D̄⁻¹
175-
for k in 1:maxiter
176-
Xₖ₊₁ = rmul!(mul!(Xₖ₊₁, APₖ', Xₖ), Diagonal(D̄⁻¹ₖ))
177-
if norm(Xₖ₊₁, Inf) < degeneracy_atol
178-
break
179-
end
180-
Xₖ₊₁ .+= Xₖ
181-
if k == maxiter
182-
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(norm(Xₖ₊₁, Inf)))"
183-
break
184-
end
185-
D̄⁻¹ₖ₊₁ .= D̄⁻¹ₖ .^ 2
186-
APₖ₊₁ = mul!(APₖ₊₁, APₖ, APₖ)
187-
Xₖ, Xₖ₊₁ = Xₖ₊₁, Xₖ
188-
APₖ, APₖ₊₁ = APₖ₊₁, APₖ
189-
D̄⁻¹ₖ, D̄⁻¹ₖ₊₁ = D̄⁻¹ₖ₊₁, D̄⁻¹ₖ
190-
end
191-
Z .+= Xₖ
168+
# Normalize by the smallest |eigenvalue|, which caps `max|D̄⁻¹|` at 1, so squaring can
169+
# only shrink it.
170+
dabsmin = minimum(abs, D)
171+
APᴴ ./= dabsmin
172+
D̄⁻¹ = dabsmin ./ conj.(D)
173+
X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, D̄⁻¹, degeneracy_atol, maxiter)
174+
Z .+= X
192175
if eltype(ΔA) <: Real
193-
ΔAc = mul!(AP, Z, V') # recycle AP
176+
ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ
194177
ΔA .+= real.(ΔAc)
195178
else
196179
ΔA = mul!(ΔA, Z, V', 1, 1)

0 commit comments

Comments
 (0)