-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexponential.jl
More file actions
136 lines (118 loc) · 4.51 KB
/
exponential.jl
File metadata and controls
136 lines (118 loc) · 4.51 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
# Inputs
# ------
function copy_input(::typeof(exponential), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
copy_input(::typeof(exponential), A::Diagonal) = copy(A)
function copy_input(::typeof(exponentiali), τ::Number, A::AbstractMatrix)
return τ, copy!(similar(A, complex(eltype(A))), A)
end
copy_input(::typeof(exponentiali), τ::Number, A::Diagonal) = τ, copy(A)
function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm)
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)"))
@check_size(expA, (m, m))
return @check_scalar(expA, A)
end
function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh)
if !ishermitian(A)
throw(DomainError(A, "Hermitian matrix was expected. Use `project_hermitian` to project onto the nearest hermitian matrix)"))
end
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)"))
@check_size(expA, (m, m))
return @check_scalar(expA, A)
end
function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert expA isa Diagonal
@check_size(expA, (m, m))
@check_scalar(expA, A)
return nothing
end
function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm)
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)"))
return @check_size(expA, (m, m))
end
function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh)
if !ishermitian(A)
throw(DomainError(A, "Hermitian matrix was expected. Use `project_hermitian` to project onto the nearest hermitian matrix)"))
end
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)"))
return @check_size(expA, (m, m))
end
function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert expA isa Diagonal
return @check_size(expA, (m, m))
end
# Outputs
# -------
initialize_output(::typeof(exponential!), A::AbstractMatrix, ::AbstractAlgorithm) = A
initialize_output(::typeof(exponentiali!), τ::Number, A::AbstractMatrix, ::AbstractAlgorithm) =
complex(A)
# Implementation
# --------------
function exponential!(A, expA, alg::MatrixFunctionViaLA)
check_input(exponential!, A, expA, alg)
return LinearAlgebra.exp!(A)
end
function exponential!(A, expA, alg::MatrixFunctionViaEigh)
check_input(exponential!, A, expA, alg)
D, V = eigh_full!(A, alg.eigh_alg)
expD = map_diagonal!(x -> exp(x / 2), D, D)
VexpD = rmul!(V, expD)
return mul!(expA, VexpD, V')
end
function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEig)
check_input(exponential!, A, expA, alg)
D, V = eig_full!(A, alg.eig_alg)
expD = map_diagonal!(exp, D, D)
iV = inv(V)
VexpD = rmul!(V, expD)
if eltype(A) <: Real
expA .= real.(VexpD * iV)
else
mul!(expA, VexpD, iV)
end
return expA
end
function exponentiali!(τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaLA)
check_input(exponentiali!, A, expA, alg)
expA .= A .* (im * τ)
return LinearAlgebra.exp!(expA)
end
function exponentiali!(τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh)
check_input(exponentiali!, A, expA, alg)
D, V = eigh_full!(A, alg.eigh_alg)
expD = map_diagonal(x -> exp(x * im * τ), D)
if eltype(A) <: Real
VexpD = V * expD
return expA .= real.(VexpD * V')
else
VexpD = rmul!(V, expD)
return mul!(expA, VexpD, V')
end
end
function exponentiali!(τ::Number, A, expA, alg::MatrixFunctionViaEig)
check_input(exponentiali!, A, expA, alg)
D, V = eig_full!(A, alg.eig_alg)
expD = map_diagonal!(x -> exp(x * im * τ), D, D)
iV = inv(V)
VexpD = rmul!(V, expD)
return mul!(expA, VexpD, iV)
end
# Diagonal logic
# --------------
function exponential!(A, expA, alg::DiagonalAlgorithm)
check_input(exponential!, A, expA, alg)
return map_diagonal!(exp, expA, A)
end
function exponentiali!(τ::Number, A, expA, alg::DiagonalAlgorithm)
check_input(exponentiali!, A, expA, alg)
return map_diagonal!(x -> exp(x * im * τ), expA, A)
end