-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexponential.jl
More file actions
50 lines (43 loc) · 1.49 KB
/
exponential.jl
File metadata and controls
50 lines (43 loc) · 1.49 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
# 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 check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm)
m, n = size(A)
m == n || throw(DimensionMismatch("square input matrix expected"))
@assert expA isa AbstractMatrix
@check_size(expA, (m, m))
return @check_scalar(expA, A)
end
# Outputs
# -------
function initialize_output(::typeof(exponential!), A::AbstractMatrix, ::AbstractAlgorithm)
n = size(A, 1) # square check will happen later
expA = similar(A, (n, n))
return expA
end
# Implementation
# --------------
function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaLA)
copyto!(expA, LinearAlgebra.exp(A))
return expA
end
function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh)
D, V = eigh_full(A, alg.eigh_alg)
copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V))
return expA
end
function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEig)
D, V = eig_full(A, alg.eig_alg)
copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V))
return expA
end
# Diagonal logic
# --------------
function exponential!(A::Diagonal, expA, alg::DiagonalAlgorithm)
check_input(exponential!, A, expA, alg)
copyto!(expA, Diagonal(LinearAlgebra.exp.(diagview(A))))
return expA
end