:- module(ekf, [ekf_step/4]).
:- use_module(library(array)).
:- use_module(library(array/grad)).
:- use_module(ukf, [chol/4]).

:- meta_predicate ekf_step(3, +, +, -).

/** <module> One step of an extended Kalman filter, as boxes

The step on a batch of B states of dimension N, each with its own observation of dimension
M. The model is that of ukf.pl, a predicate Model(Xs, Ys, Zs): given the N reads of a state
it gives N value expressions, the state after the transition, and M, its observation. The
filter linearises it where the unscented filter samples it: the transition at the prior
mean, the observation at the predicted mean, each Jacobian a program made by jacobian/4 in
forward mode, N directions for every state of the batch at once, compiled with the step.

    ekf_step(+Model, +dims(B, N, M), +in(Mu, P, Q, R, Zobs), -Out)
        Mu[B, N], P[B, N, N], the prior; Q[N, N], R[M, M], the noises of the transition and
        of the observation; Zobs[B, M], the observation of each state
        Out = out(MuPred, PPred, F, H, MuPost, PPost)
        MuPred[B, N] = f(Mu), F[B, N, N] = df/dx at Mu, PPred = F P F' + Q,
        H[B, M, N] = dh/dx at MuPred, S = H PPred H' + R, K = PPred H' S^-1,
        MuPost[B, N] = MuPred + K (Zobs - h(MuPred)), PPost[B, N, N] = PPred - K H PPred.

N and M are the model's, ground; B may stay an extent variable.
*/

ekf_step(Model, dims(B, N, M), in(Mu, P, Q, R, Zobs), out(MuPred, PPred, F, H, MuPost, PPost)) :-
   shape(Mu, [B, N]), shape(P, [B, N, N]), shape(Q, [N, N]), shape(R, [M, M]), shape(Zobs, [B, M]),
   Nm1 is N-1, Mm1 is M-1, numlist(0, Nm1, Is), numlist(0, Mm1, Js),
   % The transition at the prior mean, and its Jacobian.
   shape(MuPred, [B, N]),
   maplist(read_of(Mu, Bi), Is, Xs),
   call(Model, Xs, Ys, _),
   maplist(write_of(MuPred, Bi), Is, Ys, EqY),
   over([Bi:B], EqY),
   jacobian(MuPred, Mu, F, [batch(1)]),
   % The observation at the predicted mean, and its Jacobian.
   shape(Z, [B, M]),
   maplist(read_of(MuPred, Bi), Is, Xp),
   call(Model, Xp, _, Zs),
   maplist(write_of(Z, Bi), Js, Zs, EqZ),
   over([Bi:B], EqZ),
   jacobian(Z, MuPred, H, [batch(1)]),
   % PPred = F P F' + Q.
   shape(FP, [B, N, N]), shape(PPred, [B, N, N]),
   over([Bi:B, I:N, J:N], [FP[Bi, I, J] := 0]),
   over([Bi:B, I:N, J:N, K:N], [FP[Bi, I, J] := FP[Bi, I, J] + F[Bi, I, K] * P[Bi, K, J]]),
   over([Bi:B, I:N, J:N], [PPred[Bi, I, J] := Q[I, J]]),
   over([Bi:B, I:N, J:N, K:N], [PPred[Bi, I, J] := PPred[Bi, I, J] + FP[Bi, I, K] * F[Bi, J, K]]),
   % S = H PPred H' + R, through PHt = PPred H'.
   shape(PHt, [B, N, M]), shape(S, [B, M, M]),
   over([Bi:B, I:N, J:M], [PHt[Bi, I, J] := 0]),
   over([Bi:B, I:N, J:M, K:N], [PHt[Bi, I, J] := PHt[Bi, I, J] + PPred[Bi, I, K] * H[Bi, J, K]]),
   over([Bi:B, I:M, J:M], [S[Bi, I, J] := R[I, J]]),
   over([Bi:B, I:M, J:M, K:N], [S[Bi, I, J] := S[Bi, I, J] + H[Bi, I, K] * PHt[Bi, K, J]]),
   % S^-1 = Linv' Linv, Linv the inverse of the Cholesky factor by forward substitution.
   chol(S, Ls, B, M),
   shape(Linv, [B, M, M]), shape(Acc, []), shape(Sinv, [B, M, M]),
   over([Bi:B, J:M], [over([I:M], [over([], [Acc := 0]),
                                   over([Kk:M], [Acc := Acc + where(Kk < I, Ls[Bi, I, Kk] * Linv[Bi, Kk, J], 0)]),
                                   Linv[Bi, I, J] := (where(I =:= J, 1.0, 0.0) - Acc) / Ls[Bi, I, I]])]),
   over([Bi:B, I:M, J:M], [Sinv[Bi, I, J] := 0]),
   over([Bi:B, I:M, J:M, Kk:M], [Sinv[Bi, I, J] := Sinv[Bi, I, J] + Linv[Bi, Kk, I] * Linv[Bi, Kk, J]]),
   % The gain K = PHt S^-1 and the update.
   shape(G, [B, N, M]), shape(MuPost, [B, N]), shape(PPost, [B, N, N]),
   over([Bi:B, I:N, J:M], [G[Bi, I, J] := 0]),
   over([Bi:B, I:N, J:M, K:M], [G[Bi, I, J] := G[Bi, I, J] + PHt[Bi, I, K] * Sinv[Bi, K, J]]),
   over([Bi:B, I:N], [MuPost[Bi, I] := MuPred[Bi, I]]),
   over([Bi:B, I:N, J:M], [MuPost[Bi, I] := MuPost[Bi, I] + G[Bi, I, J] * (Zobs[Bi, J] - Z[Bi, J])]),
   over([Bi:B, I:N, J:N], [PPost[Bi, I, J] := PPred[Bi, I, J]]),
   over([Bi:B, I:N, J:N, K:M], [PPost[Bi, I, J] := PPost[Bi, I, J] - G[Bi, I, K] * PHt[Bi, J, K]]).

read_of(A, Bi, I, A[Bi, I]).
write_of(A, Bi, I, V, A[Bi, I] := V).
