-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheigh.jl
More file actions
90 lines (85 loc) · 2.95 KB
/
eigh.jl
File metadata and controls
90 lines (85 loc) · 2.95 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
# Inputs
# ------
function copy_input(::typeof(eigh_full), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
function copy_input(::typeof(eigh_vals), A::AbstractMatrix)
return copy_input(eigh_full, A)
end
copy_input(::typeof(eigh_trunc), A) = copy_input(eigh_full, A)
function check_input(::typeof(eigh_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, real)
@check_size(V, (m, m))
@check_scalar(V, A)
return nothing
end
function check_input(::typeof(eigh_vals!), A::AbstractMatrix, D, ::AbstractAlgorithm)
m, n = size(A)
@assert D isa AbstractVector
@check_size(D, (n,))
@check_scalar(D, A, real)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(eigh_full!), A::AbstractMatrix, ::AbstractAlgorithm)
n = size(A, 1) # square check will happen later
D = Diagonal(similar(A, real(eltype(A)), n))
V = similar(A, (n, n))
return (D, V)
end
function initialize_output(::typeof(eigh_vals!), A::AbstractMatrix, ::AbstractAlgorithm)
n = size(A, 1) # square check will happen later
D = similar(A, real(eltype(A)), n)
return D
end
function initialize_output(::typeof(eigh_trunc!), A::AbstractMatrix,
alg::TruncatedAlgorithm)
return initialize_output(eigh_full!, A, alg.alg)
end
# Implementation
# --------------
function eigh_full!(A::AbstractMatrix, DV, alg::LAPACK_EighAlgorithm)
check_input(eigh_full!, A, DV, alg)
D, V = DV
Dd = D.diag
if alg isa LAPACK_MultipleRelativelyRobustRepresentations
YALAPACK.heevr!(A, Dd, V; alg.kwargs...)
elseif alg isa LAPACK_DivideAndConquer
YALAPACK.heevd!(A, Dd, V; alg.kwargs...)
elseif alg isa LAPACK_Simple
YALAPACK.heev!(A, Dd, V; alg.kwargs...)
else # alg isa LAPACK_Expert
YALAPACK.heevx!(A, Dd, V; alg.kwargs...)
end
# TODO: make this controllable using a `gaugefix` keyword argument
for j in 1:size(V, 2)
v = view(V, :, j)
s = conj(sign(argmax(abs, v)))
v .*= s
end
return D, V
end
function eigh_vals!(A::AbstractMatrix, D, alg::LAPACK_EighAlgorithm)
check_input(eigh_vals!, A, D, alg)
V = similar(A, (size(A, 1), 0))
if alg isa LAPACK_MultipleRelativelyRobustRepresentations
YALAPACK.heevr!(A, D, V; alg.kwargs...)
elseif alg isa LAPACK_DivideAndConquer
YALAPACK.heevd!(A, D, V; alg.kwargs...)
elseif alg isa LAPACK_QRIteration # == LAPACK_Simple
YALAPACK.heev!(A, D, V; alg.kwargs...)
else # alg isa LAPACK_Bisection == LAPACK_Expert
YALAPACK.heevx!(A, D, V; alg.kwargs...)
end
return D
end
function eigh_trunc!(A::AbstractMatrix, DV, alg::TruncatedAlgorithm)
D, V = eigh_full!(A, DV, alg.alg)
return truncate!(eigh_trunc!, (D, V), alg.trunc)
end