forked from MilesCranmer/DataDrivenDiffEq.jl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.jl
More file actions
180 lines (137 loc) · 4.61 KB
/
type.jl
File metadata and controls
180 lines (137 loc) · 4.61 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
"""
$(TYPEDEF)
# Fields
$(FIELDS)
## Note
The keyword argument `eval_expression` controls the function creation
behavior. `eval_expression=true` means that `eval` is used, so normal
world-age behavior applies (i.e. the functions cannot be called from
the function that generates them). If `eval_expression=false`,
then construction via GeneralizedGenerated.jl is utilized to allow for
same world-age evaluation. However, this can cause Julia to segfault
on sufficiently large basis functions. By default eval_expression=false.
"""
struct Koopman{T, B <: AbstractBasis, K, DISCRETE} <: AbstractKoopman
"""The basis of observables"""
basis::B
"""The operator/generator of the dynamics"""
K::K
"""Mapping back onto the observed states"""
C::AbstractMatrix{T}
"""Internal matrix `Q` used for updating"""
Q::AbstractMatrix{T}
"""Internal matrix `P` used for updating"""
P::AbstractMatrix{T}
end
Base.eltype(k::Koopman{T}) where {T} = T
## Koopman methods
# We assume that we only have real valued observed
Base.Matrix(k::AbstractKoopman) = real.(Matrix(__get_K(k)))
# Get K
__get_K(k::AbstractKoopman) = getfield(k, :K)
"""
$(SIGNATURES)
Returns `true` if the `AbstractKoopmanOperator` `k` is discrete in time.
"""
is_discrete(k::Koopman{<:Any, <:Any, <:Any, D}) where {D} = D
"""
$(SIGNATURES)
Returns `true` if the `AbstractKoopmanOperator` `k` is continuous in time.
"""
is_continuous(k::AbstractKoopman) = !is_discrete(k)
"""
$(SIGNATURES)
Return the eigendecomposition of the `AbstractKoopmanOperator`.
"""
LinearAlgebra.eigen(k::AbstractKoopman) = begin
K = __get_K(k)
isa(K, Eigen) && return K
eigen(K)
end
"""
$(SIGNATURES)
Return the eigenvalues of the `AbstractKoopmanOperator`.
"""
LinearAlgebra.eigvals(k::AbstractKoopman) = eigvals(__get_K(k))
"""
$(SIGNATURES)
Return the eigenvectors of the `AbstractKoopmanOperator`.
"""
LinearAlgebra.eigvecs(k::AbstractKoopman) = eigvecs(__get_K(k))
"""
$(SIGNATURES)
Return the eigenvectors of a continuous `AbstractKoopmanOperator`.
"""
modes(k::Koopman{<:Any, <:Any, <:Any, true}) = throw(AssertionError("Koopman is discrete."))
modes(k::Koopman{<:Any, <:Any, <:Any, false}) = eigvecs(k)
"""
$(SIGNATURES)
Return the eigenvalues of a continuous `AbstractKoopmanOperator`.
"""
frequencies(k::Koopman{<:Any, <:Any, <:Any, false}) = eigvals(k)
function frequencies(k::Koopman{<:Any, <:Any, <:Any, true})
throw(AssertionError("Koopman is discrete."))
end
"""
$(SIGNATURES)
Return the approximation of the discrete Koopman operator stored in `k`.
"""
operator(k::Koopman{<:Any, <:Any, <:Any, true}) = __get_K(k)
function operator(k::Koopman{<:Any, <:Any, <:Any, false})
throw(AssertionError("Koopman is continuous."))
end
"""
_
$(SIGNATURES)
Return the approximation of the continuous Koopman generator stored in `k`.
"""
generator(k::Koopman{<:Any, <:Any, <:Any}) = __get_K(k)
generator(k::Koopman{<:Any, true}) = throw(AssertionError("Koopman is discrete."))
"""
$(SIGNATURES)
Return the array `C`, mapping the Koopman space back onto the state space.
"""
outputmap(k::AbstractKoopman) = Symbolics.unwrap(getfield(k, :C))
"""
$(SIGNATURES)
Returns `true` if the `AbstractKoopmanOperator` is updatable.
"""
updatable(k::AbstractKoopman) = !isempty(k.Q) && !isempty(k.P)
"""
$(SIGNATURES)
Returns `true` if either:
- the Koopman operator has just eigenvalues with magnitude less than one or
- the Koopman generator has just eigenvalues with a negative real part
"""
is_stable(k::Koopman{<:Any, true}) = all(real.(eigvals(k)) .< real.(one(eltype(k))))
is_stable(k::Koopman{<:Any, false}) = all(real.(eigvals(k)) .< real.(zero(eltype(k))))
# TODO This does not work, since we are using the reduced basis instead of the
# original, lifted dynamics...
"""
$(SIGNATURES)
Update the Koopman `k` given new data `X` and `Y`. The operator is updated in place if
the L2 error of the prediction exceeds the `threshold`.
`p` and `t` are the parameters of the basis and the vector of timepoints, if necessary.
"""
function update!(k::AbstractKoopman,
X::AbstractArray, Y::AbstractArray;
p::AbstractArray = [], t::AbstractVector = [],
U::AbstractArray = [],
threshold::T = eps()) where {T <: Real}
@assert updatable(k) "Linear Koopman is not updatable."
Ψ₀ = k(X, p, t, U)
Ψ₁ = k(Y, p, t, U)
ϵ = norm(Ψ₁ - Matrix(k) * Ψ₀, 2)
if ϵ < threshold
return
end
k.Q += Ψ₁ * Ψ₀'
k.P += Ψ₀ * Ψ₀'
k.operator .= k.Q / k.P
if norm(Y - outputmap(k) * Matrix(k) * Ψ₀) < threshold
return
end
# TODO Make this a proper rank 1 update
k.output .= X / Ψ₀
return
end