forked from MilesCranmer/DataDrivenDiffEq.jl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithms.jl
More file actions
229 lines (180 loc) · 5.93 KB
/
algorithms.jl
File metadata and controls
229 lines (180 loc) · 5.93 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Tolerance
function truncated_svd(A::AbstractMatrix{T}, truncation::Real) where {T <: Number}
truncation = min(truncation, one(T))
U, S, V = svd(A)
r = vec(S .> truncation * maximum(S))
U = U[:, r]
S = S[r]
V = V[:, r]
return U, S, V
end
# Explicit rank
function truncated_svd(A::AbstractMatrix{T}, truncation::Int) where {T <: Number}
U, S, V = svd(A)
r = [((i <= truncation && S[i] > zero(T)) ? true : false) for i in 1:length(S)]
U = U[:, r]
S = S[r]
V = V[:, r]
return U, S, V
end
# General method with inputs
function (x::AbstractKoopmanAlgorithm)(
X::AbstractArray, Y::AbstractArray, U::AbstractArray,
B::AbstractArray)
K, _ = x(X, Y - B * U)
return (K, B)
end
function (x::AbstractKoopmanAlgorithm)(
X::AbstractArray, Y::AbstractArray, U::AbstractArray,
::Nothing)
return x(X, Y, U)
end
"""
$(TYPEDEF)
Approximates the Koopman operator `K` based on
```julia
K = Y / X
```
where `Y` and `X` are data matrices. Returns a `Eigen` factorization of the operator.
# Fields
$(FIELDS)
# Signatures
$(SIGNATURES)
"""
mutable struct DMDPINV <: AbstractKoopmanAlgorithm end;
# Fast but more allocations
function (x::DMDPINV)(X::AbstractArray, Y::AbstractArray)
K = Y / X
return (eigen(K), DataDrivenDiffEq.__EMPTY_MATRIX)
end
# DMDC
function (x::DMDPINV)(X::AbstractArray, Y::AbstractArray, U::AbstractArray)
isempty(U) && return x(X, Y)
nx, m = size(X)
nu, m = size(U)
K̃ = Y / [X; U]
K = K̃[:, 1:nx]
B = K̃[:, (nx + 1):end]
return (eigen(K), B)
end
"""
$(TYPEDEF)
Approximates the Koopman operator `K` based on the singular value decomposition
of `X` such that:
```julia
K = Y*V*Σ*U'
```
where `Y` and `X = U*Σ*V'` are data matrices. The singular value decomposition is truncated via
the `truncation` parameter, which can either be an `Int` indicating an index-based truncation or a `Real`
indicating a tolerance-based truncation. Returns a `Eigen` factorization of the operator.
# Fields
$(FIELDS)
# Signatures
$(SIGNATURES)
"""
mutable struct DMDSVD{T} <: AbstractKoopmanAlgorithm where {T <: Number}
"""Indicates the truncation"""
truncation::T
end;
DMDSVD() = DMDSVD(0.0)
# Slower but fewer allocations
function (x::DMDSVD{T})(X::AbstractArray, Y::AbstractArray) where {T <: Real}
U, S, V = truncated_svd(X, x.truncation)
xone = one(eltype(X))
# Computed the reduced operator
Sinv = Diagonal(xone ./ S)
B = Y * V * Sinv
à = U'B
# Compute the modes
λ, ω = eigen(Ã)
φ = B * ω
return (Eigen(λ, φ), DataDrivenDiffEq.__EMPTY_MATRIX)
end
# DMDc
function (x::DMDSVD{T})(X::AbstractArray, Y::AbstractArray,
U::AbstractArray) where {T <: Real}
isempty(U) && return x(X, Y)
nx, m = size(X)
nu, m = size(U)
# Input space svd
Ũ, S̃, Ṽ = truncated_svd([X; U], x.truncation)
# Output space svd
Û, _ = svd(Y)
# Split the svd
U₁, U₂ = Ũ[1:nx, :], Ũ[(nx + 1):end, :]
xone = one(eltype(X))
# Computed the reduced operator
C = Y * Ṽ * Diagonal(xone ./ S̃) # Common submatrix
# We do not project onto a reduced subspace here.
# This would mess up our initial conditions, since sometimes we have
# x1->x2, x2->x1
à = Û'C * U₁'Û
B̃ = C * U₂'
# Compute the modes
λ, ω = eigen(Ã)
φ = C * U₁'Û * ω
return (Eigen(λ, φ), B̃)
end
"""
$(TYPEDEF)
Approximates the Koopman operator `K` with the algorithm `alg` over the rank-reduced data
matrices `Xᵣ = X Qᵣ` and `Yᵣ = Y Qᵣ`, where `Qᵣ` originates from the singular value decomposition of
the joint data `Z = [X; Y]`. Based on [this paper](http://cwrowley.princeton.edu/papers/Hemati-2017a.pdf).
If `rtol` ∈ (0, 1) is given, the singular value decomposition is reduced to include only
entries bigger than `rtol*maximum(Σ)`. If `rtol` is an integer, the reduced SVD up to `rtol` is used
for computation.
# Fields
$(FIELDS)
# Signatures
$(SIGNATURES)
"""
mutable struct TOTALDMD{R, A} <:
AbstractKoopmanAlgorithm where {R <: Number, A <: AbstractKoopmanAlgorithm}
truncation::R
alg::A
end
TOTALDMD() = TOTALDMD(0.0, DMDPINV())
function (x::TOTALDMD)(X::AbstractArray, Y::AbstractArray)
_, _, Q = truncated_svd([X; Y], x.truncation)
return x.alg(X * Q, Y * Q)
end
function (x::TOTALDMD)(X::AbstractArray, Y::AbstractArray, U::AbstractArray)
isempty(U) && return x(X, Y)
_, _, Q = truncated_svd([X; Y], x.truncation)
return x.alg(X * Q, Y * Q, U * Q)
end
function (x::TOTALDMD)(X::AbstractArray, Y::AbstractArray, U::AbstractArray,
B::AbstractArray)
_, _, Q = truncated_svd([X; Y], x.truncation)
K, _ = x.alg(X * Q, (Y - B * U) * Q)
return (K, B)
end
"""
$(TYPEDEF)
Approximates the Koopman operator `K` via the forward-backward DMD.
It is assumed that `K = sqrt(K₁*inv(K₂))`, where `K₁` is the approximation via forward and `K₂` via [DMDSVD](@ref). Based on [this paper](https://arxiv.org/pdf/1507.02264).
If `truncation` ∈ (0, 1) is given, the singular value decomposition is reduced to include only
entries bigger than `truncation*maximum(Σ)`. If `truncation` is an integer, the reduced SVD up to `truncation` is used for computation.
# Fields
$(FIELDS)
# Signatures
$(SIGNATURES)
"""
mutable struct FBDMD{R} <: AbstractKoopmanAlgorithm where {R <: Number}
alg::DMDSVD{R}
end
FBDMD(truncation = 0.0) = FBDMD(DMDSVD(truncation))
function (x::FBDMD)(X::AbstractArray{T}, Y::AbstractArray{T}) where {T}
alg = x.alg
A₁, _ = alg(X, Y)
A₂, _ = alg(Y, X)
A₁ = Matrix(A₁)
à = sqrt(A₁ * inv(A₂))
# We do not want to lose sign information here
à .= abs.(Ã) .* sign.(A₁)
return (eigen(Ã), DataDrivenDiffEq.__EMPTY_MATRIX)
end
function (x::FBDMD)(X::AbstractArray{T}, Y::AbstractArray{T}, U::AbstractArray{T}) where {T}
@warn "FBDMD does not support exegenous signals without input matrix. Using DMDSVD."
return x.alg(X, Y, U)
end