-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheig.jl
More file actions
173 lines (146 loc) · 5.2 KB
/
eig.jl
File metadata and controls
173 lines (146 loc) · 5.2 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
# 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(::typeof(eig_trunc), 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, n = size(A)
m == n || throw(DimensionMismatch("square 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, ::AbstractAlgorithm)
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected"))
@assert D isa AbstractVector
@check_size(D, (n,))
@check_scalar(D, A, complex)
return nothing
end
function check_input(::typeof(eig_full!), A::AbstractMatrix, DV, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
D, V = DV
@assert D isa Diagonal && V isa Diagonal
@check_size(D, (m, m))
@check_size(V, (m, m))
# Diagonal doesn't need to promote to complex scalartype since we know it is diagonalizable
@check_scalar(D, A)
@check_scalar(V, A)
return nothing
end
function check_input(::typeof(eig_vals!), A::AbstractMatrix, D, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert D isa AbstractVector
@check_size(D, (n,))
# Diagonal doesn't need to promote to complex scalartype since we know it is diagonalizable
@check_scalar(D, A)
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(::typeof(eig_trunc!), A, alg::TruncatedAlgorithm)
return initialize_output(eig_full!, A, alg.alg)
end
function initialize_output(::typeof(eig_full!), A::Diagonal, ::DiagonalAlgorithm)
return A, similar(A)
end
function initialize_output(::typeof(eig_vals!), A::Diagonal, ::DiagonalAlgorithm)
return diagview(A)
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, :gaugefix, default_gaugefix())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:gaugefix,)})
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{(:gaugefix,)})
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
# Diagonal logic
# --------------
function eig_full!(A::Diagonal, (D, V)::Tuple{Diagonal, Diagonal}, alg::DiagonalAlgorithm)
check_input(eig_full!, A, (D, V), alg)
D === A || copy!(D, A)
one!(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)
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, :gaugefix, default_gaugefix())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:gaugefix,)})
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{(:gaugefix,)})
if alg isa GPU_Simple
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_Simple"
_gpu_geev!(A, D, V)
end
return D
end