Skip to content

Commit 2ecf6c8

Browse files
authored
Merge pull request #141 from SciML/sparse_regression_performance
Switch to iip for regression and edmd
2 parents 753479e + 276fcf4 commit 2ecf6c8

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/koopman/extended_dmd.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ function EDMD(X::AbstractArray, Y::AbstractArray, Ψ::AbstractBasis; p::Abstract
3232
N,M = size(X)
3333

3434
# Compute the transformed data
35-
Ψ₀ = Ψ(X, p, t)
36-
Ψ₁ = Ψ(Y, p, t)
35+
Ψ₀ = zeros(eltype(X), length(Ψ), size(X, 2))
36+
Ψ₁ = zeros(eltype(X), length(Ψ), size(Y, 2))
37+
Ψ(Ψ₀, X, p, t)
38+
Ψ(Ψ₁, Y, p, t)
3739

3840
A = alg(Ψ₀, Ψ₁)
3941

@@ -56,7 +58,10 @@ function gEDMD(X::AbstractArray, DX::AbstractArray, Ψ::AbstractBasis; p::Abstra
5658
N,M = size(X)
5759

5860
# Compute the transformed data
59-
Ψ₀ = Ψ(X, p, t)
61+
62+
# Compute the transformed data
63+
Ψ₀ = zeros(eltype(X), length(Ψ), size(X, 2))
64+
Ψ(Ψ₀, X, p, t)
6065

6166
# The jacobian to get d/dt(Ψ) = d/dx(Ψ) dx/dt
6267
= jacobian(Ψ)

src/sindy/isindy.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function ISINDy(X::AbstractArray, Ẋ::AbstractArray, Ψ::Basis, opt::T = ADM();
3232
@assert size(X)[end] == size(Ẋ)[end]
3333

3434
# Compute the library and the corresponding nullspace
35-
θ = Ψ(X, p, t)
35+
θ = zeros(eltype(X), length(Ψ), size(X, 2))
36+
Ψ(θ, X, p, t)
3637

3738
# Init for sweep over the differential variables
3839
Ξ = zeros(eltype(θ), length(Ψ)*2, size(Ẋ, 1))
@@ -47,7 +48,9 @@ function ISINDy(X::AbstractArray, Ẋ::AbstractArray, Ψ::Basis, opt::T; f::Func
4748
@assert size(X)[end] == size(Ẋ)[end]
4849

4950
# Compute the library and the corresponding nullspace
50-
θ = Ψ(X, p, t)
51+
θ = zeros(eltype(X), length(Ψ), size(X, 2))
52+
Ψ(θ, X, p, t)
53+
5154
= zeros(eltype(θ), size(θ, 1)*2, size(θ, 2))
5255
dθ[size(θ, 1)+1:end, :] .= θ
5356

@@ -77,9 +80,11 @@ end
7780

7881
function ISINDy(X::AbstractArray, Ẋ::AbstractArray, Ψ::Basis, thresholds::AbstractVector, opt::T = STRRidge(); f::Function = (xi, theta)->[norm(xi, 0); norm(theta'*xi, 2)], g::Function = x->norm(x), maxiter::Int64 = 10, rtol::Float64 = 0.99, p::AbstractArray = [], t::AbstractVector = [], convergence_error = eps(), normalize::Bool = true, denoise::Bool = false) where T <: DataDrivenDiffEq.Optimize.AbstractOptimizer
7982
@assert size(X)[end] == size(Ẋ)[end]
80-
83+
8184
# Compute the library and the corresponding nullspace
82-
θ = Ψ(X, p, t)
85+
θ = zeros(eltype(X), length(Ψ), size(X, 2))
86+
Ψ(θ, X, p, t)
87+
8388
= zeros(eltype(θ), size(θ, 1)*2, size(θ, 2))
8489
dθ[size(θ, 1)+1:end, :] .= θ
8590

src/sindy/sindy.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ function sparse_regression(X::AbstractArray, Ẋ::AbstractArray, Ψ::Basis, p::A
6161

6262
Ξ = zeros(eltype(X), length(Ψ), ny)
6363
scales = ones(eltype(X), length(Ψ))
64-
θ = Ψ(X, p, t)
64+
θ = zeros(eltype(X), length(Ψ), nm)
65+
Ψ(θ, X, p, t)
66+
6567

6668
denoise ? optimal_shrinkage!') : nothing
6769
normalize ? normalize_theta!(scales, θ) : nothing
@@ -81,8 +83,9 @@ function sparse_regression!(Ξ::AbstractArray, X::AbstractArray, Ẋ::AbstractAr
8183
@assert size(Ξ) == (length(Ψ), ny)
8284

8385
scales = ones(eltype(X), length(Ψ))
84-
θ = Ψ(X, p, t)
85-
86+
θ = zeros(eltype(X), length(Ψ), nm)
87+
Ψ(θ, X, p, t)
88+
8689
denoise ? optimal_shrinkage!') : nothing
8790
normalize ? normalize_theta!(scales, θ) : nothing
8891

0 commit comments

Comments
 (0)