diff --git a/src/common/defaults.jl b/src/common/defaults.jl index c64c24ad4..b74977bf9 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 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) + 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/pullbacks/eig.jl b/src/pullbacks/eig.jl index 11fbc02c9..b4ee584a2 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]), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) 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 `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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...), 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)), complement_atol, maxiter) Z .+= X if eltype(ΔA) <: Real ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ @@ -178,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 2b3afacd0..3d3c22214 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]), + complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...) ) 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 `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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...), 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), 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 @@ -180,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 576997b69..e90d1150d 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ᴴ...), + 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 @@ -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 `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_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 @@ -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, complement_atol, 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, complement_atol, maxiter) X = rmul!(AP * Y, Diagonal(S⁻¹)) X .+= X₀ ΔA = mul!(ΔA, X, Vᴴ, 1, 1) @@ -279,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