-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheig.jl
More file actions
130 lines (120 loc) · 4.2 KB
/
eig.jl
File metadata and controls
130 lines (120 loc) · 4.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
# Inputs
# ------
function copy_input(::typeof(eig_full), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
function copy_input(::typeof(eig_vals), A::AbstractMatrix)
return copy_input(eig_full, A)
end
copy_input(::typeof(eig_trunc), A) = copy_input(eig_full, 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
# 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::AbstractMatrix, alg::TruncatedAlgorithm)
return initialize_output(eig_full!, A, alg.alg)
end
# Implementation
# --------------
# actual implementation
function eig_full!(A::AbstractMatrix, DV, alg::LAPACK_EigAlgorithm)
check_input(eig_full!, A, DV, alg)
D, V = DV
if alg isa LAPACK_Simple
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_Simple (geev) does not accept any keyword arguments"))
YALAPACK.geev!(A, D.diag, V)
else # alg isa LAPACK_Expert
YALAPACK.geevx!(A, D.diag, V; alg.kwargs...)
end
# TODO: make this controllable using a `gaugefix` keyword argument
V = gaugefix!(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))
if alg isa LAPACK_Simple
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_Simple (geev) does not accept any keyword arguments"))
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::AbstractMatrix, DV, alg::TruncatedAlgorithm)
D, V = eig_full!(A, DV, alg.alg)
return truncate!(eig_trunc!, (D, V), alg.trunc)
end
_gpu_geev!(A::AbstractMatrix, D, V) = throw(MethodError(_gpu_geev!, (A, D, V)))
function initialize_output(::typeof(eig_full!), A::AbstractMatrix, ::CUSOLVER_Simple)
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 check_input(::typeof(eig_full!), A::AbstractMatrix, DV, ::CUSOLVER_Simple)
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 eig_full!(A::AbstractMatrix, DV, alg::GPU_EigAlgorithm)
check_input(eig_full!, A, DV, alg)
D, V = DV
if alg isa GPU_Simple
isempty(alg.kwargs) ||
throw(ArgumentError("GPU_Simple (geev) does not accept any keyword arguments"))
_gpu_geev!(A, D.diag, V)
end
# TODO: make this controllable using a `gaugefix` keyword argument
V = gaugefix!(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))
if alg isa GPU_Simple
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_Simple (geev) does not accept any keyword arguments"))
_gpu_geev!(A, D, V)
end
return D
end