diff --git a/src/common/gauge.jl b/src/common/gauge.jl index 9de1010e0..d2d0c690d 100644 --- a/src/common/gauge.jl +++ b/src/common/gauge.jl @@ -4,8 +4,9 @@ Fix the residual gauge degrees of freedom in the eigen or singular vectors, that are obtained from the decomposition performed by `f_eig` or `f_svd`. -This is achieved by ensuring that the entry with the largest magnitude in `V` or `U` -is real and positive. +This is achieved by ensuring that, in every column of `V` or `U`, the first entry whose +magnitude is within a relative tolerance of the largest magnitude is real and positive +(see [`gauge_index`](@ref)). """ gaugefix! # Helper functions @@ -13,6 +14,20 @@ _argmaxabs(x) = reduce(_largest, x; init = zero(eltype(x))) _largest(x::Real, y::Real) = abs(x) < abs(y) ? y : x _largest(x::Complex, y::Complex) = abs2(x) < abs2(y) ? y : x +default_gauge_rtol(V) = sqrt(eps(real(float(eltype(V))))) + +""" + gauge_index(V; dims = 1, rtol = sqrt(eps(real(eltype(V))))) + +Indices of the entries of `V` used to fix the gauge along dimension `dims`: for every slice, the +first entry whose magnitude is at least `(1 - rtol)` times the largest magnitude in that slice. +""" +function gauge_index(V::AbstractArray; dims::Int = 1, rtol::Real = default_gauge_rtol(V)) + thr = maximum(abs, V; dims) .* (1 - rtol) + # `findmax` returns the first index among equal maxima (also for GPU arrays) + return last(findmax(abs.(V) .>= thr; dims)) +end + function gaugefix!(::typeof(qr_householder!), Q, R, Rd) ax = Base.OneTo(length(Rd)) if !isempty(Q) @@ -40,8 +55,8 @@ function gaugefix!(::typeof(lq_householder!), L, Q, Ld) end function gaugefix!(::Union{typeof(eig_full!), typeof(eigh_full!), typeof(gen_eig_full!)}, V::AbstractMatrix) - signs = reduce(_largest, V; dims = 1, init = zero(eltype(V))) - @. signs = sign(signs) + isempty(V) && return V + signs = sign.(V[gauge_index(V; dims = 1)]) @. V = V * conj(signs) return V end @@ -52,16 +67,15 @@ end function gaugefix!(::typeof(svd_full!), U, Vᴴ) m, n = size(U, 2), size(Vᴴ, 1) k = min(m, n) - signs = reduce(_largest, U; dims = 1, init = zero(eltype(U))) - @. signs = sign(signs) + isempty(U) && return (U, Vᴴ) + signs = sign.(U[gauge_index(U; dims = 1)]) @. U = U * conj(signs) Vᴴk = view(Vᴴ, axes(Vᴴ, 1)[1:k], :) signs_t = transpose(view(signs, :, axes(signs, 2)[1:k])) @. Vᴴk = signs_t * Vᴴk if n > m Vᴴr = view(Vᴴ, axes(Vᴴ, 1)[(k + 1):n], :) - signs_r = reduce(_largest, Vᴴr; dims = 2, init = zero(eltype(Vᴴ))) - @. signs_r = sign(signs_r) + signs_r = sign.(Vᴴr[gauge_index(Vᴴr; dims = 2)]) @. Vᴴr = conj(signs_r) * Vᴴr end return (U, Vᴴ) @@ -69,8 +83,8 @@ end function gaugefix!(::Union{typeof(svd_compact!), typeof(svd_trunc!)}, U, Vᴴ) @assert axes(U, 2) == axes(Vᴴ, 1) - signs = reduce(_largest, U; dims = 1, init = zero(eltype(U))) - @. signs = sign(signs) + isempty(U) && return (U, Vᴴ) + signs = sign.(U[gauge_index(U; dims = 1)]) signs_t = transpose(signs) @. U = U * conj(signs) @. Vᴴ = signs_t * Vᴴ diff --git a/src/pushforwards/eig.jl b/src/pushforwards/eig.jl index b698804a8..c5051ded4 100644 --- a/src/pushforwards/eig.jl +++ b/src/pushforwards/eig.jl @@ -15,7 +15,7 @@ function eig_pushforward!( if eltype(V) <: Real # fix norm conservation ΔV .-= V .* real.(sum(conj.(V) .* ΔV; dims = 1)) else # also fix gauge for `gaugefix!` compatibility - _, I = findmax(abs, V; dims = 1) + I = gauge_index(V; dims = 1) ΔV .-= V .* (real.(sum(conj.(V) .* ΔV; dims = 1)) .+ im .* imag.(ΔV[I] ./ V[I])) end end diff --git a/src/pushforwards/eigh.jl b/src/pushforwards/eigh.jl index 894bd97cb..56af8cf2f 100644 --- a/src/pushforwards/eigh.jl +++ b/src/pushforwards/eigh.jl @@ -13,7 +13,7 @@ function eigh_pushforward!( ∂K .*= inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol) ΔV = mul!(ΔV, V, ∂K) if eltype(V) <: Complex # fix gauge for `gaugefix!` compatibility - _, I = findmax(abs, V; dims = 1) + I = gauge_index(V; dims = 1) infinitesimal_phases = imag.(ΔV[I] ./ V[I]) ΔV .-= im .* V .* infinitesimal_phases end diff --git a/src/pushforwards/svd.jl b/src/pushforwards/svd.jl index 08fd081f4..b9b86c4af 100644 --- a/src/pushforwards/svd.jl +++ b/src/pushforwards/svd.jl @@ -61,7 +61,7 @@ function svd_pushforward!( if eltype(U) <: Complex && !iszerotangent(ΔU) && !iszerotangent(ΔVᴴ) # fix gauge for `gaugefix!` compatibility ΔU₁ = view(ΔU, :, 1:r) ΔV₁ᴴ = view(ΔVᴴ, 1:r, :) - _, I = findmax(abs, U₁; dims = 1) + I = gauge_index(U₁; dims = 1) infinitesimal_phases = imag.(ΔU₁[I] .* inv_safe.(U₁[I])) ΔU₁ .-= im .* U₁ .* infinitesimal_phases ΔV₁ᴴ .+= im .* transpose(infinitesimal_phases) .* V₁ᴴ diff --git a/test/common/gauge.jl b/test/common/gauge.jl new file mode 100644 index 000000000..99dea351f --- /dev/null +++ b/test/common/gauge.jl @@ -0,0 +1,33 @@ +using MatrixAlgebraKit +using MatrixAlgebraKit: gauge_index +using Test +using StableRNGs + +@testset "gauge_index tie-breaking ($T)" for T in (Float64, ComplexF64) + rng = StableRNG(123) + # the first two entries of every column have (nearly) equal, largest magnitude + V = T[1 -2 3 -4; -1 2 -3 4; 0 0 0 0; 0 0 0 0] .+ randn(rng, T, 4, 4) .* [0, 0, 1.0e-1, 1.0e-1] + V[2, :] .*= 1 + 1.0e-14 + I = gauge_index(V; dims = 1) + @test vec(I) == [CartesianIndex(1, j) for j in 1:4] + # rounding-level perturbations of the magnitudes do not change the selected entries + V′ = copy(V) + V′[1, :] .*= 1 - 4eps() + @test gauge_index(V′; dims = 1) == I + @test vec(gauge_index(permutedims(V); dims = 2)) == [CartesianIndex(j, 1) for j in 1:4] +end + +@testset "svd gauge stable for exactly tied magnitudes ($T)" for T in (Float64, ComplexF64) + rng = StableRNG(1234) + m, n = 10, 4 + B = randn(rng, T, m ÷ 2, n) + # rows of A come in pairs related by a sign flip, so every left singular vector has pairs + # of entries with exactly equal magnitude (in exact arithmetic) + A = vcat(B, -B) + U, S, Vᴴ = svd_compact(A) + for A′ in (A * (1 + eps()), A * (1 - eps()), A .* (1 .+ eps() .* rand(rng, m, n))) + U′, S′, Vᴴ′ = svd_compact(A′) + @test U′ ≈ U + @test Vᴴ′ ≈ Vᴴ + end +end