rlabbe / filterpy

Python Kalman filtering and optimal estimation library. Implements Kalman filter, particle filter, Extended Kalman filter, Unscented Kalman filter, g-h (alpha-beta), least squares, H Infinity, smoothers, and more. Has companion book 'Kalman and Bayesian Filters in Python'.
MIT License
3.22k stars 612 forks source link

Permit Jacobian function to also return h(x) #269

Closed stangelandm1 closed 2 years ago

stangelandm1 commented 2 years ago

Without breaking the existing API, permit the Jacobian function to be a complete "model" function that returns both the Jacobian and h(x).

In complex systems the constructs for h(x) and the Jacobian are often similar. Separate functions result in duplicate calculations.

stangelandm1 commented 2 years ago

The functionality of this patchset could have been implemented differently. I'm open to other ideas if this isn't suitable.

rlabbe commented 2 years ago

It's been a really long time since I've looked at this, and I don't quite follow what the problem is. duplicate code? Extra computation? Etc. Is this added functionality, or just more efficient in some space? Maybe an example of the old way and new way would be helpful. Regardless, we'd need documentation on how to use the feature.

stangelandm1 commented 2 years ago

For sure. Yes, this is just a performance issue.

To be honest, I'm not totally happy with what I've proposed as an API change. It's functional, backwards compatible, but not elegant. I'm certainly open to other ideas.

My application involves tracking rotations, both the h(x) and H(x) functions make many of the same calculations (transforms and matrix rotations). For performance I would rather do the common calculations once.

Instead of modifying filterpy, I was tempted to do all the calculations (h and H) before calling ekf.update(), and then have H(x) and h(x) just return the stored value. It's a bit of a hack though and relies on the state not changing. It wouldn't work for the ekf.predict_update(), as predict changes the state 'x', but I'm not using it anyway.