Skip to content

Commit e44b2e1

Browse files
committed
remove useless .data on hermitians
1 parent 701d29b commit e44b2e1

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

src/estimator/kalman.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,12 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
368368
B̂d::Matrix{NT}
369369
D̂d::Matrix{NT}
370370
P̂0::Hermitian{NT, Matrix{NT}}
371-
::Hermitian{NT, Matrix{NT}}
372-
::Hermitian{NT, Matrix{NT}}
371+
::Hermitian{NT, Matrix{NT}}
372+
::Hermitian{NT, Matrix{NT}}
373373
::Matrix{NT}
374+
::Hermitian{NT, Matrix{NT}}
375+
::Matrix{NT}
376+
Ŷm::Matrix{NT}
374377
::Int
375378
γ::NT
376379
::Vector{NT}
@@ -391,14 +394,16 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
391394
P̂0 = Hermitian(P̂0, :L)
392395
= copy(P̂0)
393396
= zeros(NT, nx̂, nym)
397+
= Hermitian(zeros(NT, nym, nym), :L)
398+
X̂, Ŷm = zeros(NT, nx̂, nσ), zeros(NT, nym, nσ)
394399
return new{NT, SM}(
395400
model,
396401
lastu0, x̂, P̂,
397402
i_ym, nx̂, nym, nyu, nxs,
398403
As, Cs_u, Cs_y, nint_u, nint_ym,
399404
Â, B̂u, Ĉ, B̂d, D̂d,
400405
P̂0, Q̂, R̂,
401-
K̂,
406+
K̂, M̂, X̂, Ŷm,
402407
nσ, γ, m̂, Ŝ
403408
)
404409
end
@@ -565,18 +570,18 @@ noise, respectively.
565570
ISBN9780470045343.
566571
"""
567572
function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:Real
568-
x̂, P̂, Q̂, R̂, K̂ = estim.x̂, estim.P̂, estim.Q̂, estim.R̂, estim.
573+
x̂, P̂, Q̂, R̂, K̂, M̂ = estim.x̂, estim.P̂, estim.Q̂, estim.R̂, estim.K̂, estim.
574+
X̂, Ŷm = estim.X̂, estim.Ŷm
569575
nym, nx̂, nσ = estim.nym, estim.nx̂, estim.
570576
γ, m̂, Ŝ = estim.γ, estim.m̂, estim.
571577
# --- initialize matrices ---
572-
X̂, X̂_next = Matrix{NT}(undef, nx̂, nσ), Matrix{NT}(undef, nx̂, nσ)
578+
X̂_next = Matrix{NT}(undef, nx̂, nσ)
573579
= Vector{NT}(undef, estim.model.nu)
574580
ŷm = Vector{NT}(undef, nym)
575581
= Vector{NT}(undef, estim.model.ny)
576-
Ŷm = Matrix{NT}(undef, nym, nσ)
577582
sqrt_P̂ = LowerTriangular{NT, Matrix{NT}}(Matrix{NT}(undef, nx̂, nx̂))
578583
# --- correction step ---
579-
sqrt_P̂.data .= cholesky(P̂).L
584+
sqrt_P̂ .= cholesky(P̂).L
580585
γ_sqrt_P̂ = lmul!(γ, sqrt_P̂)
581586
X̂ .=
582587
X̂[:, 2:nx̂+1] .+= γ_sqrt_P̂
@@ -589,7 +594,7 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
589594
X̄, Ȳm = X̂, Ŷm
590595
X̄ .=.-
591596
Ȳm .= Ŷm .- ŷm
592-
= Hermitian(Ȳm ** Ȳm' +, :L)
597+
.= Hermitian(Ȳm ** Ȳm' .+ R̂)
593598
mul!(K̂, X̄, lmul!(Ŝ, Ȳm'))
594599
rdiv!(K̂, cholesky(M̂))
595600
= ŷm
@@ -598,19 +603,18 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
598603
P̂_cor =- Hermitian(K̂ **', :L)
599604
# --- prediction step ---
600605
X̂_cor, sqrt_P̂_cor = X̂, sqrt_P̂
601-
sqrt_P̂_cor.data .= cholesky(P̂_cor).L
606+
sqrt_P̂_cor .= cholesky(P̂_cor).L
602607
γ_sqrt_P̂_cor = lmul!(γ, sqrt_P̂_cor)
603608
X̂_cor .= x̂_cor
604609
X̂_cor[:, 2:nx̂+1] .+= γ_sqrt_P̂_cor
605610
X̂_cor[:, nx̂+2:end] .-= γ_sqrt_P̂_cor
606-
X̂_next = similar(X̂_cor)
607611
for j in axes(X̂_next, 2)
608612
@views f̂!(X̂_next[:, j], û, estim, estim.model, X̂_cor[:, j], u, d)
609613
end
610614
x̂_next = mul!(x̂, X̂_next, m̂)
611615
X̄_next = X̂_next
612616
X̄_next .= X̂_next .- x̂_next
613-
.data .= X̄_next ** X̄_next' .+ # .data is necessary for Hermitians
617+
P̂ .= Hermitian(X̄_next ** X̄_next' .+)
614618
return nothing
615619
end
616620

@@ -781,7 +785,7 @@ end
781785
function init_estimate_cov!(
782786
estim::Union{KalmanFilter, UnscentedKalmanFilter, ExtendedKalmanFilter}, _ , _ , _
783787
)
784-
estim..data .= estim.P̂0 # .data is necessary for Hermitians
788+
estim.P̂ .= estim.P̂0
785789
return nothing
786790
end
787791

@@ -818,6 +822,7 @@ function update_estimate_kf!(estim::StateEstimator{NT}, u, ym, d, Â, Ĉm, P̂
818822
Q̂, R̂, M̂, K̂ = estim.Q̂, estim.R̂, estim.M̂, estim.
819823
nx̂, nu, ny = estim.nx̂, estim.model.nu, estim.model.ny
820824
x̂next, û, ŷ = Vector{NT}(undef, nx̂), Vector{NT}(undef, nu), Vector{NT}(undef, ny)
825+
P̂next = similar(P̂)
821826
mul!(M̂, P̂.data, Ĉm') # the ".data" weirdly removes a type instability in mul!
822827
rdiv!(M̂, cholesky!(Hermitian(Ĉm ** Ĉm' .+ R̂)))
823828
mul!(K̂, Â, M̂)
@@ -828,6 +833,6 @@ function update_estimate_kf!(estim::StateEstimator{NT}, u, ym, d, Â, Ĉm, P̂
828833
f̂!(x̂next, û, estim, estim.model, x̂, u, d)
829834
mul!(x̂next, K̂, v̂, 1, 1)
830835
estim.x̂ .= x̂next
831-
.data .=* (P̂ .-* Ĉm * P̂) *' .+ # .data is necessary for Hermitians
836+
P̂ .= Hermitian(* (P̂ .-* Ĉm * P̂) *' .+)
832837
return nothing
833838
end

src/estimator/mhe/execute.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"Reset the data windows and time-varying variables for the moving horizon estimator."
22
function init_estimate_cov!(estim::MovingHorizonEstimator, _ , _ , _ )
3-
estim.invP̄.data .= inv(estim.P̂0)
4-
estim.P̂arr_old.data .= estim.P̂0
5-
estim.x̂arr_old .= 0
6-
estim. .= 0
7-
estim. .= 0
8-
estim.Ym .= 0
9-
estim.U .= 0
10-
estim.D .= 0
11-
estim. .= 0
12-
estim.Nk .= 0
13-
estim..data .= 0
14-
estim. .= 0
15-
estim.p .= 0
3+
estim.invP̄ .= inv(estim.P̂0)
4+
estim.P̂arr_old .= estim.P̂0
5+
estim.x̂arr_old .= 0
6+
estim.Z̃ .= 0
7+
estim.X̂ .= 0
8+
estim.Ym .= 0
9+
estim.U .= 0
10+
estim.D .= 0
11+
estim.Ŵ .= 0
12+
estim.Nk .= 0
13+
estim.H̃ .= 0
14+
estim.q̃ .= 0
15+
estim.p .= 0
1616
return nothing
1717
end
1818

@@ -243,7 +243,7 @@ function initpred!(estim::MovingHorizonEstimator, model::LinModel)
243243
@views mul!(estim.q̃[1:nZ̃], M_Nk_ẼZ̃', FZ̃)
244244
@views lmul!(2, estim.q̃[1:nZ̃])
245245
estim.p .= dot(FZ̃, M_Nk, FZ̃)
246-
estim..data[1:nZ̃, 1:nZ̃] = Ñ_Nk
246+
estim..data[1:nZ̃, 1:nZ̃] .= Ñ_Nk
247247
@views mul!(estim..data[1:nZ̃, 1:nZ̃], ẼZ̃', M_Nk_ẼZ̃, 1, 1)
248248
@views lmul!(2, estim..data[1:nZ̃, 1:nZ̃])
249249
Z̃var_Nk::Vector{JuMP.VariableRef} = @views optim[:Z̃var][1:nZ̃]
@@ -337,11 +337,11 @@ end
337337
function update_cov!(estim::MovingHorizonEstimator)
338338
nu, nd, nym = estim.model.nu, estim.model.nd, estim.nym
339339
uarr, ymarr, darr = @views estim.U[1:nu], estim.Ym[1:nym], estim.D[1:nd]
340-
estim.covestim. .= estim.x̂arr_old
341-
estim.covestim..data .= estim.P̂arr_old # .data is necessary for Hermitian
340+
estim.covestim.x̂ .= estim.x̂arr_old
341+
estim.covestim.P̂ .= estim.P̂arr_old
342342
update_estimate!(estim.covestim, uarr, ymarr, darr)
343-
estim.P̂arr_old.data .= estim.covestim.
344-
estim.invP̄.data .= Hermitian(inv(estim.P̂arr_old), :L)
343+
estim.P̂arr_old .= estim.covestim.
344+
estim.invP̄ .= inv(estim.P̂arr_old)
345345
return nothing
346346
end
347347

0 commit comments

Comments
 (0)