From a2af280a5e89deb5d858de12edb2d37046fe4e4d Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 18 Nov 2024 08:15:22 +0100 Subject: [PATCH 1/3] copy input arrays that are mutated as state to avoid user-facing user objects --- Project.toml | 2 +- src/kalman.jl | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 27955ea..0c6f0d8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LowLevelParticleFilters" uuid = "d9d29d28-c116-5dba-9239-57a5fe23875b" authors = ["baggepinnen "] -version = "3.9.0" +version = "3.9.1" [deps] ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" diff --git a/src/kalman.jl b/src/kalman.jl index 46acf0c..95a43ea 100644 --- a/src/kalman.jl +++ b/src/kalman.jl @@ -2,7 +2,7 @@ abstract type AbstractKalmanFilter <: AbstractFilter end function convert_cov_type(R1, R) if R isa SMatrix || R isa Matrix - return R + return copy(R) elseif R1 isa SMatrix && size(R) == size(R1) return SMatrix{size(R1,1),size(R1,2)}(R) elseif R1 isa Matrix @@ -11,10 +11,9 @@ function convert_cov_type(R1, R) return Matrix(R) end end - function convert_x0_type(μ) if μ isa Vector || μ isa SVector - return μ + return copy(μ) else return Vector(μ) end From 427478bcf7ae0440424b82463c7e8a46ee5a814b Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 18 Nov 2024 08:20:16 +0100 Subject: [PATCH 2/3] only construct static x0 if R is static --- src/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 9586b70..3005a15 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -144,7 +144,8 @@ struct SimpleMvNormal{M,S} Σ::S end -SimpleMvNormal(Σ::AbstractMatrix) = SimpleMvNormal(@SVector(zeros(size(Σ,1))), Σ) +SimpleMvNormal(Σ::SMatrix) = SimpleMvNormal(@SVector(zeros(size(Σ,1))), Σ) +SimpleMvNormal(Σ::AbstractMatrix) = SimpleMvNormal(zeros(size(Σ,1)), Σ) # We define this new function extended_logpdf and overload that for Distributions.jl in the extension extended_logpdf(d::SimpleMvNormal, x) = mvnormal_c0(d) - PDMats.invquad(d.Σ, x .- d.μ)/2 From 262f510d3ab5e775098f9bb1c3a95a8f9c4ed88f Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 18 Nov 2024 08:34:29 +0100 Subject: [PATCH 3/3] detect more kinds of static matrices --- src/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 3005a15..bb29d47 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -144,9 +144,10 @@ struct SimpleMvNormal{M,S} Σ::S end -SimpleMvNormal(Σ::SMatrix) = SimpleMvNormal(@SVector(zeros(size(Σ,1))), Σ) +SimpleMvNormal(Σ::Union{SMatrix, PDMats.PDMat{<:Any, <:SMatrix}, Diagonal{<:Any, <:SVector}}) = SimpleMvNormal(@SVector(zeros(size(Σ,1))), Σ) SimpleMvNormal(Σ::AbstractMatrix) = SimpleMvNormal(zeros(size(Σ,1)), Σ) + # We define this new function extended_logpdf and overload that for Distributions.jl in the extension extended_logpdf(d::SimpleMvNormal, x) = mvnormal_c0(d) - PDMats.invquad(d.Σ, x .- d.μ)/2 const log2π = log(2π)