Skip to content

let diffusion_matrix(::CoupledSDEs) export a matrix #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicalSystemsBase"
uuid = "6e36e845-645a-534a-86f2-f5d4aa5a06b4"
repo = "https://github.com/JuliaDynamics/DynamicalSystemsBase.jl.git"
version = "3.11.0"
version = "3.11.1"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand Down
11 changes: 6 additions & 5 deletions ext/src/CoupledSDEs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ If this is not the case, returns `nothing`.
Note: The diffusion matrix ``Σ`` is the square root of the noise covariance matrix ``Q`` (see
[`covariance_matrix`](@ref)), defined via the Cholesky decomposition ``Q = Σ Σ^\\top``.
"""
function diffusion_matrix(ds::CoupledSDEs{IIP,D}) where {IIP,D}
function diffusion_matrix(ds::CoupledSDEs{IIP,D})::AbstractMatrix where {IIP,D}
if ds.noise_type[:invertible]
diffusion = diffusion_function(ds)
A = diffusion(zeros(D), current_parameters(ds), 0.0)
A = A isa AbstractMatrix ? A : Diagonal(A)
else
@warn """
The diffusion function of the `CoupledSDEs` cannot be expressed as a constant
Expand All @@ -218,7 +219,7 @@ If this is not the case, returns `nothing`.

See also [`diffusion_matrix`](@ref).
"""
function covariance_matrix(ds::CoupledSDEs)
function covariance_matrix(ds::CoupledSDEs)::AbstractMatrix
A = diffusion_matrix(ds)
(A == nothing) ? nothing : A * A'
end
Expand All @@ -241,10 +242,10 @@ Returns `g, noise_prototype`.
"""
function construct_diffusion_function(
g, covariance, noise_prototype, noise_strength, D, IIP
)
)
if isnothing(g) # diagonal additive noise
cov = isnothing(covariance) ? LinearAlgebra.I(D) : covariance
size(cov) != (D,D) &&
size(cov) != (D, D) &&
throw(ArgumentError("Covariance matrix must be of size $((D, D))"))
A = sqrt(cov)
if IIP
Expand All @@ -269,4 +270,4 @@ function construct_diffusion_function(
end
end
return g, noise_prototype
end
end
3 changes: 2 additions & 1 deletion test/stochastic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ end
A = sqrt(Γ)
lorenz_oop = CoupledSDEs(lorenz_rule, u0, p0, covariance = Γ, diffeq=diffeq_cov)
@test A ≈ diffusion_matrix(lorenz_oop)
@test A isa AbstractMatrix
@test Γ ≈ A * A'
end

Expand All @@ -153,4 +154,4 @@ end
@test approx ≈ Γ atol=1e-1 broken = true
# I think I understand something wromg here
end
end
end
Loading