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
34 changes: 24 additions & 10 deletions src/common/gauge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@

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
_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)
Expand Down Expand Up @@ -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
Expand All @@ -52,25 +67,24 @@ 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ᴴ)
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ᴴ
Expand Down
2 changes: 1 addition & 1 deletion src/pushforwards/eig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pushforwards/eigh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pushforwards/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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₁ᴴ
Expand Down
33 changes: 33 additions & 0 deletions test/common/gauge.jl
Original file line number Diff line number Diff line change
@@ -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
Loading