From b560bfc66f64d182687024422db3cfca7ffdac90 Mon Sep 17 00:00:00 2001 From: leburgel Date: Wed, 18 Nov 2026 12:16:12 +0100 Subject: [PATCH 1/5] Smith series tolerance of the truncated pullbacks relative to the right-hand side (stein_rtol) instead of degeneracy_atol Co-Authored-By: Claude Opus 5.5 --- src/pullbacks/eig.jl | 9 +++++++-- src/pullbacks/eigh.jl | 9 +++++++-- src/pullbacks/svd.jl | 11 ++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/pullbacks/eig.jl b/src/pullbacks/eig.jl index 11fbc02c9..6c016f5e5 100755 --- a/src/pullbacks/eig.jl +++ b/src/pullbacks/eig.jl @@ -119,7 +119,8 @@ end eig_trunc_pullback!( ΔA::AbstractMatrix, ΔDV, A, DV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), - gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) + gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the @@ -136,11 +137,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. + +The contribution from outside the kept eigenvectors is summed as a series, until its terms are +below `stein_rtol` relative to its first. """ function eig_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -165,7 +170,7 @@ function eig_trunc_pullback!( APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1) X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1) X₀ ./= D' - X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), degeneracy_atol, maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), stein_rtol * maximum(abs, X₀), maxiter) Z .+= X if eltype(ΔA) <: Real ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ diff --git a/src/pullbacks/eigh.jl b/src/pullbacks/eigh.jl index 2b3afacd0..3efc24573 100755 --- a/src/pullbacks/eigh.jl +++ b/src/pullbacks/eigh.jl @@ -121,7 +121,8 @@ end eigh_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), - gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) + gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`, @@ -138,11 +139,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. + +The contribution from outside the kept eigenvectors is summed as a series, until its terms are +below `stein_rtol` relative to its first. """ function eigh_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -162,7 +167,7 @@ function eigh_trunc_pullback!( if !iszerotangent(ΔV₊) X₀ = rdiv!(ΔV₊, Diagonal(D)) AP = mul!(copy(A), V * Dmat, V', -1, 1) - X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), degeneracy_atol, maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), stein_rtol * maximum(abs, X₀), maxiter) Z .+= X # we cannot directly multiply Z * V' into ΔA, because we have to # take the Hermitian part, and cannot apply project_hermitian! to diff --git a/src/pullbacks/svd.jl b/src/pullbacks/svd.jl index 576997b69..0cdbe60aa 100755 --- a/src/pullbacks/svd.jl +++ b/src/pullbacks/svd.jl @@ -201,7 +201,8 @@ end ΔA, A, USVᴴ, ΔUSVᴴ; rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]), degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), - gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...) + gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the @@ -217,12 +218,16 @@ singular values (not included in `S`) are (sufficiently) smaller than those in ` A warning will be printed if the cotangents are not gauge-invariant, i.e. if the anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. + +The contribution from outside the kept singular vectors is summed as a series, until its terms +are below `stein_rtol` relative to its first. """ function svd_trunc_pullback!( ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ; rank_atol::Real = 0, degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), + stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) # Extract the SVD components @@ -257,7 +262,7 @@ function svd_trunc_pullback!( if m ≤ n X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹)) X .+= X₀ - X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, degeneracy_atol, maxiter) # recycle X₀ + X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, stein_rtol * maximum(abs, X), maxiter) # recycle X₀ Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP) Yᴴ .+= Y₀ᴴ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -265,7 +270,7 @@ function svd_trunc_pullback!( else Y = rmul!(AP' * X₀, Diagonal(S⁻¹)) Y .+= Y₀ᴴ' - Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, degeneracy_atol, maxiter) + Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, stein_rtol * maximum(abs, Y), maxiter) X = rmul!(AP * Y, Diagonal(S⁻¹)) X .+= X₀ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) From 0bd82d0e4e5513a9734a22bf93a60cc25866b6b4 Mon Sep 17 00:00:00 2001 From: leburgel Date: Wed, 18 Nov 2026 14:16:22 +0100 Subject: [PATCH 2/5] Rename stein_rtol to complement_rtol Co-Authored-By: Claude Opus 5.5 --- src/pullbacks/eig.jl | 8 ++++---- src/pullbacks/eigh.jl | 8 ++++---- src/pullbacks/svd.jl | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/pullbacks/eig.jl b/src/pullbacks/eig.jl index 6c016f5e5..605baf38e 100755 --- a/src/pullbacks/eig.jl +++ b/src/pullbacks/eig.jl @@ -120,7 +120,7 @@ end ΔA::AbstractMatrix, ΔDV, A, DV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the @@ -139,13 +139,13 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `stein_rtol` relative to its first. +below `complement_rtol` relative to its first. """ function eig_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -170,7 +170,7 @@ function eig_trunc_pullback!( APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1) X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1) X₀ ./= D' - X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), stein_rtol * maximum(abs, X₀), maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_rtol * maximum(abs, X₀), maxiter) Z .+= X if eltype(ΔA) <: Real ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ diff --git a/src/pullbacks/eigh.jl b/src/pullbacks/eigh.jl index 3efc24573..ac0fd9671 100755 --- a/src/pullbacks/eigh.jl +++ b/src/pullbacks/eigh.jl @@ -122,7 +122,7 @@ end ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`, @@ -141,13 +141,13 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `stein_rtol` relative to its first. +below `complement_rtol` relative to its first. """ function eigh_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -167,7 +167,7 @@ function eigh_trunc_pullback!( if !iszerotangent(ΔV₊) X₀ = rdiv!(ΔV₊, Diagonal(D)) AP = mul!(copy(A), V * Dmat, V', -1, 1) - X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), stein_rtol * maximum(abs, X₀), maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_rtol * maximum(abs, X₀), maxiter) Z .+= X # we cannot directly multiply Z * V' into ΔA, because we have to # take the Hermitian part, and cannot apply project_hermitian! to diff --git a/src/pullbacks/svd.jl b/src/pullbacks/svd.jl index 0cdbe60aa..13b2e1263 100755 --- a/src/pullbacks/svd.jl +++ b/src/pullbacks/svd.jl @@ -202,7 +202,7 @@ end rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]), degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) ) Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the @@ -220,14 +220,14 @@ anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and c which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. The contribution from outside the kept singular vectors is summed as a series, until its terms -are below `stein_rtol` relative to its first. +are below `complement_rtol` relative to its first. """ function svd_trunc_pullback!( ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ; rank_atol::Real = 0, degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), - stein_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) # Extract the SVD components @@ -262,7 +262,7 @@ function svd_trunc_pullback!( if m ≤ n X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹)) X .+= X₀ - X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, stein_rtol * maximum(abs, X), maxiter) # recycle X₀ + X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_rtol * maximum(abs, X), maxiter) # recycle X₀ Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP) Yᴴ .+= Y₀ᴴ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -270,7 +270,7 @@ function svd_trunc_pullback!( else Y = rmul!(AP' * X₀, Diagonal(S⁻¹)) Y .+= Y₀ᴴ' - Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, stein_rtol * maximum(abs, Y), maxiter) + Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_rtol * maximum(abs, Y), maxiter) X = rmul!(AP * Y, Diagonal(S⁻¹)) X .+= X₀ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) From bbf03f4383f3ecf9347a2fe00b180a1eeb1a9da4 Mon Sep 17 00:00:00 2001 From: leburgel Date: Wed, 18 Nov 2026 14:47:49 +0100 Subject: [PATCH 3/5] Smith series tolerance relative to each column's right-hand side Co-Authored-By: Claude Opus 5.5 --- src/common/pullbacks.jl | 16 ++++++++++------ src/pullbacks/eig.jl | 4 ++-- src/pullbacks/eigh.jl | 4 ++-- src/pullbacks/svd.jl | 6 +++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/common/pullbacks.jl b/src/common/pullbacks.jl index da4eff952..8984efe16 100644 --- a/src/common/pullbacks.jl +++ b/src/common/pullbacks.jl @@ -43,12 +43,12 @@ is_leading_index(ind::AbstractRange, p::Int) = ind == 1:p is_leading_index(ind::AbstractVector, p::Int) = length(ind) == p && all(ind .== 1:p) """ - accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) + accelerative_smith_iteration!(X, Xₙ, G, w, rtol, maxiter) Solve `X = B + G * X * Diagonal(w)` by summing the Neumann series `X = Σₖ Gᵏ * B * Diagonal(w)ᵏ` by doubling (Smith's method), i.e. by repeatedly adding -`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until the norm of that increment drops below `atol`, -for at most `maxiter` steps. +`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until that increment drops below `rtol` relative to `B`, +column by column (largest entries), for at most `maxiter` steps. On entry, `X` contains `B`, and it is overwritten with the result. `Xₙ` is used as a buffer, and `G` and `w` are overwritten. `w` is normalized such that `maximum(abs, w) == 1`, so that @@ -56,14 +56,18 @@ squaring it can only shrink it; `G` is scaled by the inverse factor to compensat Reference: https://doi.org/10.1016/j.aml.2009.01.012. """ -function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) +function accelerative_smith_iteration!(X, Xₙ, G, w, rtol, maxiter) + # the columns are independent: sum the series for normalized right-hand sides, so that `rtol` holds per column + nb = maximum(abs, X; dims = 1) + nb = ifelse.(iszero.(nb), one(eltype(nb)), nb) + X ./= nb Gₙ = similar(G) wmax = maximum(abs, w) w ./= wmax G .*= wmax for k in 1:maxiter Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w)) - if maximum(abs, Xₙ) < atol + if maximum(abs, Xₙ) < rtol break end X .+= Xₙ @@ -75,7 +79,7 @@ function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) Gₙ = mul!(Gₙ, G, G) G, Gₙ = Gₙ, G end - return X + return X .*= nb end """ diff --git a/src/pullbacks/eig.jl b/src/pullbacks/eig.jl index 605baf38e..656f42d2c 100755 --- a/src/pullbacks/eig.jl +++ b/src/pullbacks/eig.jl @@ -139,7 +139,7 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `complement_rtol` relative to its first. +below `complement_rtol` relative to its first, column by column. """ function eig_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; @@ -170,7 +170,7 @@ function eig_trunc_pullback!( APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1) X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1) X₀ ./= D' - X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_rtol * maximum(abs, X₀), maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_rtol, maxiter) Z .+= X if eltype(ΔA) <: Real ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ diff --git a/src/pullbacks/eigh.jl b/src/pullbacks/eigh.jl index ac0fd9671..03add8807 100755 --- a/src/pullbacks/eigh.jl +++ b/src/pullbacks/eigh.jl @@ -141,7 +141,7 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `complement_rtol` relative to its first. +below `complement_rtol` relative to its first, column by column. """ function eigh_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; @@ -167,7 +167,7 @@ function eigh_trunc_pullback!( if !iszerotangent(ΔV₊) X₀ = rdiv!(ΔV₊, Diagonal(D)) AP = mul!(copy(A), V * Dmat, V', -1, 1) - X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_rtol * maximum(abs, X₀), maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_rtol, maxiter) Z .+= X # we cannot directly multiply Z * V' into ΔA, because we have to # take the Hermitian part, and cannot apply project_hermitian! to diff --git a/src/pullbacks/svd.jl b/src/pullbacks/svd.jl index 13b2e1263..3ea5b801e 100755 --- a/src/pullbacks/svd.jl +++ b/src/pullbacks/svd.jl @@ -220,7 +220,7 @@ anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and c which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. The contribution from outside the kept singular vectors is summed as a series, until its terms -are below `complement_rtol` relative to its first. +are below `complement_rtol` relative to its first, column by column. """ function svd_trunc_pullback!( ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ; @@ -262,7 +262,7 @@ function svd_trunc_pullback!( if m ≤ n X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹)) X .+= X₀ - X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_rtol * maximum(abs, X), maxiter) # recycle X₀ + X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_rtol, maxiter) # recycle X₀ Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP) Yᴴ .+= Y₀ᴴ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -270,7 +270,7 @@ function svd_trunc_pullback!( else Y = rmul!(AP' * X₀, Diagonal(S⁻¹)) Y .+= Y₀ᴴ' - Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_rtol * maximum(abs, Y), maxiter) + Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_rtol, maxiter) X = rmul!(AP * Y, Diagonal(S⁻¹)) X .+= X₀ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) From f66e94a7751a9d4771ed7df702d30614bf1e9f1b Mon Sep 17 00:00:00 2001 From: leburgel Date: Wed, 25 Nov 2026 11:37:29 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Absolute=20Smith=20series=20tolerance=20com?= =?UTF-8?q?plement=5Fatol,=20in=20the=20units=20of=20=CE=94A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/defaults.jl | 25 +++++++++++++++++++++++++ src/common/pullbacks.jl | 16 ++++++---------- src/pullbacks/eig.jl | 13 +++++++------ src/pullbacks/eigh.jl | 13 +++++++------ src/pullbacks/svd.jl | 15 ++++++++------- 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/common/defaults.jl b/src/common/defaults.jl index c64c24ad4..39d732320 100644 --- a/src/common/defaults.jl +++ b/src/common/defaults.jl @@ -36,6 +36,31 @@ Default tolerance for deciding what values should be considered equal to 0. default_pullback_rank_atol(A) = eps(norm(A, Inf))^(3 / 4) default_pullback_rank_atol(A::Diagonal) = default_pullback_rank_atol(diagview(A)) +""" + default_pullback_complement_atol(D, ΔD, ΔV) + default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ) + +Default tolerance for the series that sums the contribution from outside the kept vectors in +the truncated pullbacks. The terms of that series have the units of `ΔA`, so the tolerance is +set by the cotangents in those units: `ΔD` and `ΔV ./ transpose(D)`, or `ΔS`, `ΔU ./ transpose(S)` +and `ΔVᴴ ./ S`. +""" +function default_pullback_complement_atol(D, ΔD, ΔV) + d = diagview(D) + x = zero(real(float(eltype(d)))) + iszerotangent(ΔD) || (x = max(x, norm(ΔD, Inf))) + iszerotangent(ΔV) || (x = max(x, norm(ΔV ./ transpose(d), Inf))) + return eps(typeof(x))^(3 / 4) * x +end +function default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ) + s = diagview(S) + x = zero(real(float(eltype(s)))) + iszerotangent(ΔS) || (x = max(x, norm(ΔS, Inf))) + iszerotangent(ΔU) || (x = max(x, norm(ΔU ./ transpose(s), Inf))) + iszerotangent(ΔVᴴ) || (x = max(x, norm(ΔVᴴ ./ s, Inf))) + return eps(typeof(x))^(3 / 4) * x +end + """ default_hermitian_tol(A) diff --git a/src/common/pullbacks.jl b/src/common/pullbacks.jl index 8984efe16..da4eff952 100644 --- a/src/common/pullbacks.jl +++ b/src/common/pullbacks.jl @@ -43,12 +43,12 @@ is_leading_index(ind::AbstractRange, p::Int) = ind == 1:p is_leading_index(ind::AbstractVector, p::Int) = length(ind) == p && all(ind .== 1:p) """ - accelerative_smith_iteration!(X, Xₙ, G, w, rtol, maxiter) + accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) Solve `X = B + G * X * Diagonal(w)` by summing the Neumann series `X = Σₖ Gᵏ * B * Diagonal(w)ᵏ` by doubling (Smith's method), i.e. by repeatedly adding -`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until that increment drops below `rtol` relative to `B`, -column by column (largest entries), for at most `maxiter` steps. +`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until the norm of that increment drops below `atol`, +for at most `maxiter` steps. On entry, `X` contains `B`, and it is overwritten with the result. `Xₙ` is used as a buffer, and `G` and `w` are overwritten. `w` is normalized such that `maximum(abs, w) == 1`, so that @@ -56,18 +56,14 @@ squaring it can only shrink it; `G` is scaled by the inverse factor to compensat Reference: https://doi.org/10.1016/j.aml.2009.01.012. """ -function accelerative_smith_iteration!(X, Xₙ, G, w, rtol, maxiter) - # the columns are independent: sum the series for normalized right-hand sides, so that `rtol` holds per column - nb = maximum(abs, X; dims = 1) - nb = ifelse.(iszero.(nb), one(eltype(nb)), nb) - X ./= nb +function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) Gₙ = similar(G) wmax = maximum(abs, w) w ./= wmax G .*= wmax for k in 1:maxiter Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w)) - if maximum(abs, Xₙ) < rtol + if maximum(abs, Xₙ) < atol break end X .+= Xₙ @@ -79,7 +75,7 @@ function accelerative_smith_iteration!(X, Xₙ, G, w, rtol, maxiter) Gₙ = mul!(Gₙ, G, G) G, Gₙ = Gₙ, G end - return X .*= nb + return X end """ diff --git a/src/pullbacks/eig.jl b/src/pullbacks/eig.jl index 656f42d2c..b4ee584a2 100755 --- a/src/pullbacks/eig.jl +++ b/src/pullbacks/eig.jl @@ -120,7 +120,7 @@ end ΔA::AbstractMatrix, ΔDV, A, DV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the @@ -139,13 +139,13 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `complement_rtol` relative to its first, column by column. +below `complement_atol`. """ function eig_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -170,7 +170,7 @@ function eig_trunc_pullback!( APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1) X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1) X₀ ./= D' - X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_rtol, maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_atol, maxiter) Z .+= X if eltype(ΔA) <: Real ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ @@ -183,10 +183,11 @@ end function eig_trunc_pullback!( ΔA::Diagonal, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), - gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) + gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) ΔA_full = zero!(similar(ΔA, size(ΔA))) - ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol) + ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol) diagview(ΔA) .+= diagview(ΔA_full) return ΔA end diff --git a/src/pullbacks/eigh.jl b/src/pullbacks/eigh.jl index 03add8807..3d3c22214 100755 --- a/src/pullbacks/eigh.jl +++ b/src/pullbacks/eigh.jl @@ -122,7 +122,7 @@ end ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`, @@ -141,13 +141,13 @@ of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degenera not small compared to `gauge_atol`. The contribution from outside the kept eigenvectors is summed as a series, until its terms are -below `complement_rtol` relative to its first, column by column. +below `complement_atol`. """ function eigh_trunc_pullback!( ΔA::AbstractMatrix, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) @@ -167,7 +167,7 @@ function eigh_trunc_pullback!( if !iszerotangent(ΔV₊) X₀ = rdiv!(ΔV₊, Diagonal(D)) AP = mul!(copy(A), V * Dmat, V', -1, 1) - X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_rtol, maxiter) + X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_atol, maxiter) Z .+= X # we cannot directly multiply Z * V' into ΔA, because we have to # take the Hermitian part, and cannot apply project_hermitian! to @@ -185,10 +185,11 @@ end function eigh_trunc_pullback!( ΔA::Diagonal, A, DV, ΔDV; degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), - gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) + gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) ΔA_full = zero!(similar(ΔA, size(ΔA))) - ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol) + ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol) diagview(ΔA) .+= diagview(ΔA_full) return ΔA end diff --git a/src/pullbacks/svd.jl b/src/pullbacks/svd.jl index 3ea5b801e..e90d1150d 100755 --- a/src/pullbacks/svd.jl +++ b/src/pullbacks/svd.jl @@ -202,7 +202,7 @@ end rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]), degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4) + complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...) ) Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the @@ -220,14 +220,14 @@ anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and c which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`. The contribution from outside the kept singular vectors is summed as a series, until its terms -are below `complement_rtol` relative to its first, column by column. +are below `complement_atol`. """ function svd_trunc_pullback!( ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ; rank_atol::Real = 0, degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...), - complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4), + complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...), maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence? ) # Extract the SVD components @@ -262,7 +262,7 @@ function svd_trunc_pullback!( if m ≤ n X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹)) X .+= X₀ - X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_rtol, maxiter) # recycle X₀ + X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_atol, maxiter) # recycle X₀ Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP) Yᴴ .+= Y₀ᴴ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -270,7 +270,7 @@ function svd_trunc_pullback!( else Y = rmul!(AP' * X₀, Diagonal(S⁻¹)) Y .+= Y₀ᴴ' - Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_rtol, maxiter) + Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_atol, maxiter) X = rmul!(AP * Y, Diagonal(S⁻¹)) X .+= X₀ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -284,10 +284,11 @@ function svd_trunc_pullback!( ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ; rank_atol::Real = 0, degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]), - gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3]) + gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3]), + complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...) ) ΔA_full = zero!(similar(ΔA, size(ΔA))) - ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol) + ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol, complement_atol) diagview(ΔA) .+= diagview(ΔA_full) return ΔA end From 45e12dc684dd4592da901e0da62ef257a8023bc9 Mon Sep 17 00:00:00 2001 From: leburgel Date: Wed, 25 Nov 2026 11:54:56 +0100 Subject: [PATCH 5/5] Reword the default_pullback_complement_atol docstring --- src/common/defaults.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/defaults.jl b/src/common/defaults.jl index 39d732320..b74977bf9 100644 --- a/src/common/defaults.jl +++ b/src/common/defaults.jl @@ -41,8 +41,8 @@ default_pullback_rank_atol(A::Diagonal) = default_pullback_rank_atol(diagview(A) default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ) Default tolerance for the series that sums the contribution from outside the kept vectors in -the truncated pullbacks. The terms of that series have the units of `ΔA`, so the tolerance is -set by the cotangents in those units: `ΔD` and `ΔV ./ transpose(D)`, or `ΔS`, `ΔU ./ transpose(S)` +the truncated pullbacks. The terms of that series scale like `ΔA`, so the tolerance is set by +the cotangents that scale like `ΔA`: `ΔD` and `ΔV ./ transpose(D)`, or `ΔS`, `ΔU ./ transpose(S)` and `ΔVᴴ ./ S`. """ function default_pullback_complement_atol(D, ΔD, ΔV)