-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheig.jl
More file actions
191 lines (163 loc) · 5.78 KB
/
eig.jl
File metadata and controls
191 lines (163 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Inputs
# ------
function copy_input(::typeof(eig_full), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
copy_input(::typeof(eig_vals), A) = copy_input(eig_full, A)
copy_input(::Union{typeof(eig_trunc), typeof(eig_trunc_no_error)}, A) = copy_input(eig_full, A)
copy_input(::typeof(eig_full), A::Diagonal) = copy(A)
function check_input(::typeof(eig_full!), A::AbstractMatrix, DV, ::AbstractAlgorithm)
m = LinearAlgebra.checksquare(A)
D, V = DV
@assert D isa Diagonal && V isa AbstractMatrix
@check_size(D, (m, m))
@check_scalar(D, A, complex)
@check_size(V, (m, m))
@check_scalar(V, A, complex)
return nothing
end
function check_input(::typeof(eig_vals!), A::AbstractMatrix, D, ::AbstractAlgorithm)
m = LinearAlgebra.checksquare(A)
@assert D isa AbstractVector
@check_size(D, (m,))
@check_scalar(D, A, complex)
return nothing
end
function check_input(::typeof(eig_full!), A::AbstractMatrix, DV, ::DiagonalAlgorithm)
m = LinearAlgebra.checksquare(A)
isdiag(A) || throw(DimensionMismatch("diagonal input matrix expected"))
D, V = DV
@assert D isa Diagonal && V isa AbstractMatrix
@check_size(D, (m, m))
@check_scalar(D, A, complex)
@check_size(V, (m, m))
@check_scalar(V, A, complex)
return nothing
end
function check_input(::typeof(eig_vals!), A::AbstractMatrix, D, ::DiagonalAlgorithm)
m = LinearAlgebra.checksquare(A)
isdiag(A) || throw(DimensionMismatch("diagonal input matrix expected"))
@assert D isa AbstractVector
@check_size(D, (m,))
@check_scalar(D, A, complex)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(eig_full!), A::AbstractMatrix, ::AbstractAlgorithm)
n = size(A, 1) # square check will happen later
Tc = complex(eltype(A))
D = Diagonal(similar(A, Tc, n))
V = similar(A, Tc, (n, n))
return (D, V)
end
function initialize_output(::typeof(eig_vals!), A::AbstractMatrix, ::AbstractAlgorithm)
n = size(A, 1) # square check will happen later
Tc = complex(eltype(A))
D = similar(A, Tc, n)
return D
end
function initialize_output(::Union{typeof(eig_trunc!), typeof(eig_trunc_no_error!)}, A, alg::TruncatedAlgorithm)
return initialize_output(eig_full!, A, alg.alg)
end
function initialize_output(::typeof(eig_full!), A::Diagonal, ::DiagonalAlgorithm)
T = eltype(A)
Tc = complex(T)
D = T <: Complex ? A : Diagonal(similar(A, Tc, size(A, 1)))
return D, similar(A, Tc, size(A))
end
function initialize_output(::typeof(eig_vals!), A::Diagonal, ::DiagonalAlgorithm)
T = eltype(A)
return T <: Complex ? diagview(A) : similar(A, complex(T), size(A, 1))
end
# Implementation
# --------------
function eig_full!(A::AbstractMatrix, DV, alg::LAPACK_EigAlgorithm)
check_input(eig_full!, A, DV, alg)
D, V = DV
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa LAPACK_Simple
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_Simple"))
YALAPACK.geev!(A, D.diag, V)
else # alg isa LAPACK_Expert
YALAPACK.geevx!(A, D.diag, V; alg_kwargs...)
end
do_gauge_fix && (V = gaugefix!(eig_full!, V))
return D, V
end
function eig_vals!(A::AbstractMatrix, D, alg::LAPACK_EigAlgorithm)
check_input(eig_vals!, A, D, alg)
V = similar(A, complex(eltype(A)), (size(A, 1), 0))
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa LAPACK_Simple
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_Simple"))
YALAPACK.geev!(A, D, V)
else # alg isa LAPACK_Expert
YALAPACK.geevx!(A, D, V; alg_kwargs...)
end
return D
end
function eig_trunc!(A, DV, alg::TruncatedAlgorithm)
D, V = eig_full!(A, DV, alg.alg)
DVtrunc, ind = truncate(eig_trunc!, (D, V), alg.trunc)
return DVtrunc..., truncation_error!(diagview(D), ind)
end
function eig_trunc_no_error!(A, DV, alg::TruncatedAlgorithm)
D, V = eig_full!(A, DV, alg.alg)
DVtrunc, ind = truncate(eig_trunc!, (D, V), alg.trunc)
return DVtrunc
end
# Diagonal logic
# --------------
eig_sortby(x::T) where {T <: Number} = T <: Complex ? (real(x), imag(x)) : x
function eig_full!(A::Diagonal, DV, alg::DiagonalAlgorithm)
check_input(eig_full!, A, DV, alg)
D, V = DV
diagA = diagview(A)
I = sortperm(diagA; by = eig_sortby)
if D === A
permute!(diagA, I)
else
diagview(D) .= view(diagA, I)
end
zero!(V)
n = size(A, 1)
I .+= (0:(n - 1)) .* n
V[I] .= Ref(one(eltype(V)))
return D, V
end
function eig_vals!(A::Diagonal, D::AbstractVector, alg::DiagonalAlgorithm)
check_input(eig_vals!, A, D, alg)
Ad = diagview(A)
D === Ad || copy!(D, Ad)
sort!(D; by = eig_sortby)
return D
end
# GPU logic
# ---------
_gpu_geev!(A, D, V) = throw(MethodError(_gpu_geev!, (A, D, V)))
function eig_full!(A::AbstractMatrix, DV, alg::GPU_EigAlgorithm)
check_input(eig_full!, A, DV, alg)
D, V = DV
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa GPU_Simple
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_Simple"
_gpu_geev!(A, D.diag, V)
end
do_gauge_fix && (V = gaugefix!(eig_full!, V))
return D, V
end
function eig_vals!(A::AbstractMatrix, D, alg::GPU_EigAlgorithm)
check_input(eig_vals!, A, D, alg)
V = similar(A, complex(eltype(A)), (size(A, 1), 0))
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa GPU_Simple
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_Simple"
_gpu_geev!(A, D, V)
end
return D
end