You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot differentiate through kalman with explicit kwargs and ForwardDiff.jl #1105
Using ControlSystemsBase.jl v1.25.4, I'm able to differentiate through kalman method, but not when I explicitly set the direct keyword argument. Here's a MRE:
using ControlSystemsBase
using ImplicitDifferentiation # needed to support AD through `kalman` according to docusing ComponentArrays # needed to support AD through `kalman` according to docusing ForwardDiff
using LinearAlgebra
functioncost(p::T) where T <:Real
A = [
0.00.00.0 p
]
C = [1.00.0]
R1 =Matrix{T}(undef, 2, 2)
R1 .= [
1.00.00.01.0
]
R2 =Matrix{T}(undef, 1, 1)
R2 .= [1.0]
#K = kalman(Discrete, A, C, R1, R2, direct=true) # fail#K = kalman(Discrete, A, C, R1, R2, direct=false) # fail#K, P = kalman(Discrete, A, C, R1, R2, extra=Val(false)) # fail#K, P = kalman(Discrete, A, C, R1, R2, extra=Val(true)) # fail
K =kalman(Discrete, A, C, R1, R2) # work@showsize(K)
returnsum(K)
endcost(0.1)
dcost_dp = ForwardDiff.derivative(cost, 0.1)
Context: this is required to support the SteadyKalmanFilter in the new feature introduced by this PR: JuliaControl/ModelPredictiveControl.jl#524. Now only the time-varying KalmanFilter is supported.
changed the title [-]Cannot differentiate through `kalman` with `direct` kwarg and `ForwardDiff.jl`[/-][+]Cannot differentiate through `kalman` with explicit kwargs and `ForwardDiff.jl`[/+]on Nov 18, 2026
Activity
franckgaga commentedon Nov 18, 2026
The same issue with the
extrakeyword argument, by the way. I've modified the MRE above.[-]Cannot differentiate through `kalman` with `direct` kwarg and `ForwardDiff.jl`[/-][+]Cannot differentiate through `kalman` with explicit kwargs and `ForwardDiff.jl`[/+]franckgaga commentedon Nov 18, 2026
There is another issue. The returned
Kmatrix has the incorrect size with autodiff. I've modified the MRE above once more. Here's what it prints:You can see the the size is
(2, 1)forFloat64and(1, 2)withForwardDiff.Dual. Atransposeis probably missing somewhere.