|
4 | 4 |
|
5 | 5 | Fix the residual gauge degrees of freedom in the eigen or singular vectors, that are |
6 | 6 | obtained from the decomposition performed by `f_eig` or `f_svd`. |
7 | | -This is achieved by ensuring that the entry with the largest magnitude in `V` or `U` |
8 | | -is real and positive. |
| 7 | +This is achieved by ensuring that, in every column of `V` or `U`, the first entry whose |
| 8 | +magnitude is within a relative tolerance of the largest magnitude is real and positive |
| 9 | +(see [`gauge_index`](@ref)). |
9 | 10 | """ gaugefix! |
10 | 11 |
|
11 | 12 | # Helper functions |
12 | 13 | _argmaxabs(x) = reduce(_largest, x; init = zero(eltype(x))) |
13 | 14 | _largest(x::Real, y::Real) = abs(x) < abs(y) ? y : x |
14 | 15 | _largest(x::Complex, y::Complex) = abs2(x) < abs2(y) ? y : x |
15 | 16 |
|
| 17 | +default_gauge_rtol(V) = sqrt(eps(real(float(eltype(V))))) |
| 18 | + |
| 19 | +""" |
| 20 | + gauge_index(V; dims = 1, rtol = sqrt(eps(real(eltype(V))))) |
| 21 | +
|
| 22 | +Indices of the entries of `V` used to fix the gauge along dimension `dims`: for every slice, the |
| 23 | +first entry whose magnitude is at least `(1 - rtol)` times the largest magnitude in that slice. |
| 24 | +""" |
| 25 | +function gauge_index(V::AbstractArray; dims::Int = 1, rtol::Real = default_gauge_rtol(V)) |
| 26 | + thr = maximum(abs, V; dims) .* (1 - rtol) |
| 27 | + # `findmax` returns the first index among equal maxima (also for GPU arrays) |
| 28 | + return last(findmax(abs.(V) .>= thr; dims)) |
| 29 | +end |
| 30 | + |
16 | 31 | function gaugefix!(::typeof(qr_householder!), Q, R, Rd) |
17 | 32 | ax = Base.OneTo(length(Rd)) |
18 | 33 | if !isempty(Q) |
@@ -40,8 +55,8 @@ function gaugefix!(::typeof(lq_householder!), L, Q, Ld) |
40 | 55 | end |
41 | 56 |
|
42 | 57 | function gaugefix!(::Union{typeof(eig_full!), typeof(eigh_full!), typeof(gen_eig_full!)}, V::AbstractMatrix) |
43 | | - signs = reduce(_largest, V; dims = 1, init = zero(eltype(V))) |
44 | | - @. signs = sign(signs) |
| 58 | + isempty(V) && return V |
| 59 | + signs = sign.(V[gauge_index(V; dims = 1)]) |
45 | 60 | @. V = V * conj(signs) |
46 | 61 | return V |
47 | 62 | end |
|
52 | 67 | function gaugefix!(::typeof(svd_full!), U, Vᴴ) |
53 | 68 | m, n = size(U, 2), size(Vᴴ, 1) |
54 | 69 | k = min(m, n) |
55 | | - signs = reduce(_largest, U; dims = 1, init = zero(eltype(U))) |
56 | | - @. signs = sign(signs) |
| 70 | + isempty(U) && return (U, Vᴴ) |
| 71 | + signs = sign.(U[gauge_index(U; dims = 1)]) |
57 | 72 | @. U = U * conj(signs) |
58 | 73 | Vᴴk = view(Vᴴ, axes(Vᴴ, 1)[1:k], :) |
59 | 74 | signs_t = transpose(view(signs, :, axes(signs, 2)[1:k])) |
60 | 75 | @. Vᴴk = signs_t * Vᴴk |
61 | 76 | if n > m |
62 | 77 | Vᴴr = view(Vᴴ, axes(Vᴴ, 1)[(k + 1):n], :) |
63 | | - signs_r = reduce(_largest, Vᴴr; dims = 2, init = zero(eltype(Vᴴ))) |
64 | | - @. signs_r = sign(signs_r) |
| 78 | + signs_r = sign.(Vᴴr[gauge_index(Vᴴr; dims = 2)]) |
65 | 79 | @. Vᴴr = conj(signs_r) * Vᴴr |
66 | 80 | end |
67 | 81 | return (U, Vᴴ) |
68 | 82 | end |
69 | 83 |
|
70 | 84 | function gaugefix!(::Union{typeof(svd_compact!), typeof(svd_trunc!)}, U, Vᴴ) |
71 | 85 | @assert axes(U, 2) == axes(Vᴴ, 1) |
72 | | - signs = reduce(_largest, U; dims = 1, init = zero(eltype(U))) |
73 | | - @. signs = sign(signs) |
| 86 | + isempty(U) && return (U, Vᴴ) |
| 87 | + signs = sign.(U[gauge_index(U; dims = 1)]) |
74 | 88 | signs_t = transpose(signs) |
75 | 89 | @. U = U * conj(signs) |
76 | 90 | @. Vᴴ = signs_t * Vᴴ |
|
0 commit comments