Skip to content

Commit 09073ca

Browse files
committed
debug: ensure that empty cov have correct type on LTS
1 parent 8893aab commit 09073ca

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎src/estimator/construct.jl‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ struct KalmanCovariances{
9090
Q̂::Q̂C, R̂::R̂C, P̂_0=nothing, He=nothing
9191
) where {NT<:Real, Q̂C<:AbstractMatrix{NT}, R̂C<:AbstractMatrix{NT}}
9292
if isnothing(P̂_0)
93-
P̂_0 = Hermitian(zeros(NT, 0, 0), :L) # don't apply to SteadyKalmanFilter
94-
invP̄ = Hermitian(zeros(NT, 0, 0), :L) # don't apply to SteadyKalmanFilter
9593
P̂ = Hermitian(zeros(NT, size(Q̂)), :L) # will hold steady-state error covariance
94+
P̂_0 = empty_similar(P̂) # don't apply to SteadyKalmanFilter
95+
invP̄ = empty_similar(P̂) # don't apply to SteadyKalmanFilter
9696
else
9797
P̂_0 = Hermitian(P̂_0, :L)
9898
P̂ = copy(P̂_0)
9999
if isnothing(He) # invP̄ is only necessary for MovingHorizonEstimator:
100-
invP̄ = Hermitian(zeros(NT, 0, 0), :L)
100+
invP̄ = empty_similar(P̂_0)
101101
else
102102
invP̄ = copy(P̂_0)
103103
try
@@ -114,8 +114,8 @@ struct KalmanCovariances{
114114
Q̂ = Hermitian(Q̂, :L)
115115
R̂ = Hermitian(R̂, :L)
116116
if isnothing(He) # the following variables are only for MovingHorizonEstimator:
117-
invQ̂_He = Hermitian(zeros(NT, 0, 0), :L)
118-
invR̂_He = Hermitian(zeros(NT, 0, 0), :L)
117+
invQ̂_He = empty_similar(Q̂)
118+
invR̂_He = empty_similar(R̂)
119119
else
120120
invQ̂, invR̂ = copy(Q̂), copy(R̂)
121121
try

‎src/general.jl‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,17 @@ to_mat(A::AbstractVector, _ ...) = reshape(A, length(A), 1)
269269
to_mat(A::AbstractMatrix, _ ...) = A
270270
to_mat(A::Real, dims...) = fill(A, dims)
271271

272-
"Convert 1-element vectors and normal matrices to Hermitians."
272+
"Convert 1-element vectors and normal matrices to `Hermitian`s."
273273
to_hermitian(A::AbstractVector) = Hermitian(reshape(A, 1, 1), :L)
274274
to_hermitian(A::AbstractMatrix) = Hermitian(A, :L)
275275
to_hermitian(A::Hermitian) = A
276276
to_hermitian(A) = A
277277

278+
"Return a 0×0 matrix with the same type as `A`, and assert an identical returned type."
279+
empty_similar(A::AbstractMatrix) = similar(A, (0, 0))::typeof(A)
280+
empty_similar(A::Diagonal) = Diagonal(similar(A.diag, (0,)))::typeof(A)
281+
empty_similar(A::Hermitian) = to_hermitian(empty_similar(parent(A)))::typeof(A)
282+
278283
"""
279284
Compute the inverse of a the Hermitian positive definite matrix `A` in-place and return it.
280285

0 commit comments

Comments
 (0)