use std::ops::Mul;
use nalgebra::{Matrix2, Vector2};
use bayes_estimate::estimators::ud::UDState;
use bayes_estimate::models::{KalmanEstimator, KalmanState};
use bayes_estimate::noise::{CoupledNoise, UncorrelatedNoise};
fn main() {
let init = KalmanState {
x: Vector2::new(11., 1.), X: Matrix2::zeros(), };
let mut estimate = UDState::try_from(init).unwrap();
let estimate_init = estimate.kalman_state().unwrap();
println!("Initial x{:.1} X{:.2}", estimate_init.x, estimate_init.X);
let my_predict_model = Matrix2::identity();
let my_predict_noise = CoupledNoise::from_uncorrelated(UncorrelatedNoise {
q: Vector2::new(sqr(1.), sqr(1.)),
});
let predicted_x = my_predict_model * estimate.x;
estimate.predict(&my_predict_model, &predicted_x, &my_predict_noise).unwrap();
let estimate_predict = estimate.kalman_state().unwrap();
println!("Predict x{:.1} X{:.2}", estimate_predict.x, estimate_predict.X);
let xp = estimate_predict.x[0];
let yp = estimate_predict.x[1];
let rp2: f64 = sqr(xp) + sqr(yp);
let rp = rp2.sqrt();
let ap = yp.atan2(xp);
let my_observe_model = Matrix2::new(
xp / rp, yp / rp,
-yp / rp2, xp / rp2);
let z = Vector2::new(10., 0_f64.to_radians());
let my_observe_noise = UncorrelatedNoise {
q: Vector2::new(sqr(0.5), sqr(2_f64.to_radians())),
};
let innovation = z - Vector2::new(rp, ap);
estimate.observe_innovation(&innovation, &my_observe_model, &my_observe_noise).unwrap();
let estimate_state = estimate.kalman_state().unwrap();
println!("Observe x{:.1} X{:.2}", estimate_state.x, estimate_state.X);
}
fn sqr<T: Copy + Mul<Output = T>>(x: T) -> T {
x * x
}