Skip to content

Support keyword arguments in kalman and lqr with dual numbers - #1106

Merged
baggepinnen merged 3 commits into
masterfrom
fb/kalman-dual-kwargs
Nov 19, 2026
Merged

baggepinnen merged 3 commits into
masterfrom
fb/kalman-dual-kwargs

Conversation

@baggepinnen

@baggepinnen baggepinnen commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Fixes #1105

The methods of kalman and lqr specialized on dual-number covariance matrices in the ImplicitDifferentiation extension accepted no keyword arguments. Any keyword argument (direct, extra) therefore caused dispatch to the generic method, which does not support dual numbers. In addition, the discrete-time kalman method for dual numbers returned the transpose of the gain.

Changes:

  • The dual-number kalman methods are removed. The generic kalman calls lqr(...; extra = Val(true)), which dispatches to the dual-number lqr method, so direct, extra and the continuous-time error for direct = true are handled by the generic code, and the gain has size (nx, ny).
  • The dual-number lqr and are methods pass their keyword arguments to arec/ared as a constant argument of the implicit function. Previously, keywords such as as = true were ignored.
  • With extra = Val(true), the dual-number lqr returns the same outputs as the generic method, including the additional outputs of the Riccati solver. The closed-loop eigenvalues and the additional outputs are the non-differentiated byproduct of the forward Riccati solve; K and X are differentiable.
  • The kalman docstring is corrected: with direct = true, p are the eigenvalues of A-AKC, not A-KC.
  • Tests compare the outputs (number, sizes, values, eigenvalues) with the generic methods and the full Jacobians of K and X with finite differences, for continuous and discrete time, direct = false/true, extra = Val(false)/Val(true), and are(...; as = true), including the example from the issue.

Positional arguments of the generic methods (the cross term S) remain unsupported for dual numbers.


Host: demeter2, session 5a6c4396-72cd-4580-a020-cb80f89c3346, working directory ~/.julia/dev/ControlSystems

🤖 Generated with Claude Code

The methods of kalman and lqr for dual-number covariance matrices in the
ImplicitDifferentiation extension now accept the keyword arguments
`extra` and, for kalman, `direct`. Previously, any keyword argument caused
dispatch to the generic method, which does not support dual numbers.
The discrete-time kalman method for dual numbers returned the transpose
of the Kalman gain; it now returns a matrix of size (nx, ny).

Fixes #1105

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuliaControlBot

Copy link
Copy Markdown

This is an automated message.
Plots were compared to references. 11/11 images have changed, see differences below.
After pulling this PR, please update the reference images by creating a PR to ControlExamplePlots.jl here.

Difference Reference Image New Image
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New
❌ 1.0 Reference New

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.32%. Comparing base (6f28683) to head (6aff7ce).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1106      +/-   ##
==========================================
+ Coverage   92.29%   92.32%   +0.02%     
==========================================
  Files          42       42              
  Lines        6177     6173       -4     
==========================================
- Hits         5701     5699       -2     
+ Misses        476      474       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The forward functions of the implicit functions now call arec/ared and
return the closed-loop eigenvalues as the non-differentiated byproduct,
instead of computing eigvals of the primal closed-loop matrix afterwards.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuliaControlBot

Copy link
Copy Markdown

This is an automated message.
Plots were compared to references. 11/11 images have changed, see differences below.
After pulling this PR, please update the reference images by creating a PR to ControlExamplePlots.jl here.

Difference Reference Image New Image
❌ 0.049 Reference New
❌ 0.049 Reference New
⚠️ 0.026 Reference New
❌ 0.04 Reference New
⚠️ 0.024 Reference New
⚠️ 0.021 Reference New
⚠️ 0.029 Reference New
❌ 0.049 Reference New
❌ 0.044 Reference New
✔️ 0.005 Reference New
⚠️ 0.026 Reference New

- Remove the dual-number kalman methods. The generic kalman calls the
  dual-number lqr method, which handles direct, extra and the
  continuous-time error without duplicated code.
- Pass the keyword arguments of are and lqr to arec/ared as a constant
  argument of the implicit function; previously, keywords such as
  as = true were ignored.
- Return all additional outputs of the Riccati solver with
  extra = Val(true), so that the number of outputs equals that of the
  generic methods.
- Correct the kalman docstring: with direct = true, p are the
  eigenvalues of A-AKC.
- Compare full Jacobians of K and X with finite differences in the tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuliaControlBot

Copy link
Copy Markdown

This is an automated message.
Plots were compared to references. 11/11 images have changed, see differences below.
After pulling this PR, please update the reference images by creating a PR to ControlExamplePlots.jl here.

Difference Reference Image New Image
❌ 0.049 Reference New
❌ 0.049 Reference New
⚠️ 0.026 Reference New
❌ 0.04 Reference New
⚠️ 0.024 Reference New
⚠️ 0.021 Reference New
⚠️ 0.029 Reference New
❌ 0.049 Reference New
❌ 0.044 Reference New
✔️ 0.005 Reference New
⚠️ 0.026 Reference New

@baggepinnen
baggepinnen merged commit 4db98b3 into master Nov 19, 2026
6 checks passed
baggepinnen referenced this pull request Nov 19, 2026
@franckgaga

Copy link
Copy Markdown
Member

Thanks @baggepinnen 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot differentiate through kalman with explicit kwargs and ForwardDiff.jl

3 participants