interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / estimation / est_kf.c

352 lines · 12972 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* EST-1 linear Kalman filter. See est_kf.h for the equations and refusals.
18 * Row-major dense matrices, at most EST_MAX_DIM per side, all on the stack.
19 * No allocation, no globals, no I/O. Outputs are built in locals and copied
20 * to the caller only on success, so a refusal never leaves a half-written
21 * record. Inputs are const and never modified. */
22#include "est_kf.h"
23#include "omega_native.h"
24
25#include <math.h>
26#include <string.h>
27
28#define D EST_MAX_DIM
29#define MAT (EST_MAX_DIM * EST_MAX_DIM)
30
31/* C(r x c) = A(r x k) * B(k x c) */
32static void mm(const double *A, const double *B, double *C, uint32_t r, uint32_t k, uint32_t c)
33{
34 uint64_t dims[3] = { r, k, c }, u = 0;
35 (void)omega2_est_om_mm(&u, (const uint64_t *)A, MAT, (const uint64_t *)B, MAT, (uint64_t *)C, MAT, dims, 3);
36}
37
38/* C(r x c) = A(r x k) * B^T where B is (c x k) */
39static void mmt(const double *A, const double *B, double *C, uint32_t r, uint32_t k, uint32_t c)
40{
41 uint64_t dims[3] = { r, k, c }, u = 0;
42 (void)omega2_est_om_mmt(&u, (const uint64_t *)A, MAT, (const uint64_t *)B, MAT, (uint64_t *)C, MAT, dims, 3);
43}
44
45static void symmetrize(double *A, uint32_t n)
46{
47 uint64_t u = 0;
48 (void)omega2_est_om_symmetrize(&u, (uint64_t *)A, (size_t)n * n, n);
49}
50
51/* Cholesky S = L L^T (lower). Refuses a pivot that is not strictly positive
52 * or not finite. */
53static int chol(const double *S, double *L, uint32_t n)
54{
55 uint64_t ok = 0;
56 if (omega2_est_om_chol(&ok, (const uint64_t *)S, (size_t)n * n, (uint64_t *)L, MAT, n) != 0)
57 return 0;
58 return (int)ok;
59}
60
61/* Solve L y = b (forward). */
62static void fwd(const double *L, const double *b, double *y, uint32_t n)
63{
64 uint64_t u = 0;
65 (void)omega2_est_om_fwd(&u, (const uint64_t *)L, MAT, (const uint64_t *)b, D, (uint64_t *)y, D, n);
66}
67/* Solve L^T x = y (backward). */
68static void bwd(const double *L, const double *y, double *x, uint32_t n)
69{
70 uint64_t u = 0;
71 (void)omega2_est_om_bwd(&u, (const uint64_t *)L, MAT, (const uint64_t *)y, D, (uint64_t *)x, D, n);
72}
73
74static est_status model_digest_matches(const est_model *mdl, const est_digest *want)
75{
76 est_digest d;
77 est_status st = est_digest_model(mdl, &d);
78 if (st != EST_OK) return st;
79 return memcmp(d.b, want->b, EST_DIGEST_SIZE) == 0 ? EST_OK : EST_ERR_MODEL;
80}
81
82static est_status units_match(const est_unit *a, const est_unit *b, uint32_t n)
83{
84 for (uint32_t i = 0; i < n; i++)
85 if (a[i] != b[i]) return EST_ERR_UNIT;
86 return EST_OK;
87}
88
89est_status est_kf_prior(const est_model *mdl, const double *x0, const double *P0,
90 int64_t t_ns, est_belief *out)
91{
92 if (!mdl || !x0 || !P0 || !out) return EST_ERR_NULL;
93 est_status st = est_check_model(mdl);
94 if (st != EST_OK) return st;
95 est_belief b;
96 memset(&b, 0, sizeof b);
97 b.n = mdl->n;
98 for (uint32_t i = 0; i < mdl->n; i++) {
99 b.unit[i] = mdl->state_unit[i];
100 b.x[i] = x0[i];
101 }
102 memcpy(b.P, P0, sizeof(double) * mdl->n * mdl->n);
103 b.t_ns = t_ns;
104 st = est_digest_model(mdl, &b.model);
105 if (st != EST_OK) return st;
106 st = est_check_belief(&b);
107 if (st != EST_OK) return st;
108 *out = b;
109 return EST_OK;
110}
111
112est_status est_kf_predict(const est_model *mdl, const est_belief *bel,
113 const double *Bu, uint32_t horizon, est_prediction *out)
114{
115 if (!mdl || !bel || !out) return EST_ERR_NULL;
116 est_status st = est_check_model(mdl);
117 if (st != EST_OK) return st;
118 st = est_check_belief(bel);
119 if (st != EST_OK) return st;
120 uint32_t n = mdl->n, m = mdl->m;
121 if (bel->n != n) return EST_ERR_DIM;
122 st = units_match(bel->unit, mdl->state_unit, n);
123 if (st != EST_OK) return st;
124 st = model_digest_matches(mdl, &bel->model);
125 if (st != EST_OK) return st;
126 if (horizon < 1u || horizon > EST_MAX_HORIZON) return EST_ERR_TIME;
127 if (Bu) {
128 for (uint32_t i = 0; i < n; i++)
129 if (!isfinite(Bu[i])) return EST_ERR_NONFINITE;
130 }
131 if (bel->generation == UINT64_MAX) return EST_ERR_STALE;
132 /* t = bel.t + horizon * step, refusing overflow */
133 if ((int64_t)horizon > INT64_MAX / mdl->step_ns) return EST_ERR_TIME;
134 int64_t span = (int64_t)horizon * mdl->step_ns;
135 if (bel->t_ns > INT64_MAX - span) return EST_ERR_TIME;
136
137 est_prediction p;
138 memset(&p, 0, sizeof p);
139 est_digest prior;
140 st = est_digest_belief(bel, &prior);
141 if (st != EST_OK) return st;
142
143 double x[D] = {0}, xn[D] = {0}, P[MAT] = {0}, T[MAT] = {0}, P2[MAT] = {0};
144 memcpy(x, bel->x, sizeof(double) * n);
145 memcpy(P, bel->P, sizeof(double) * n * n);
146 for (uint32_t h = 0; h < horizon; h++) {
147 for (uint32_t i = 0; i < n; i++) {
148 double s = 0.0;
149 for (uint32_t j = 0; j < n; j++) s += mdl->F[i * n + j] * x[j];
150 xn[i] = Bu ? s + Bu[i] : s;
151 }
152 memcpy(x, xn, sizeof(double) * n);
153 mm(mdl->F, P, T, n, n, n);
154 mmt(T, mdl->F, P2, n, n, n);
155 for (uint32_t i = 0; i < n * n; i++) P[i] = P2[i] + mdl->Q[i];
156 symmetrize(P, n);
157 for (uint32_t i = 0; i < n; i++) {
158 if (!isfinite(x[i])) return EST_ERR_NONFINITE;
159 }
160 }
161
162 p.prior = prior;
163 p.model = bel->model;
164 p.horizon = horizon;
165 p.generation = bel->generation + 1u;
166 p.t_ns = bel->t_ns + span;
167 p.n = n;
168 p.m = m;
169 if (Bu) {
170 p.has_control = 1u;
171 for (uint32_t i = 0; i < n; i++) p.Bu[i] = Bu[i] == 0.0 ? 0.0 : Bu[i];
172 }
173 memcpy(p.x, x, sizeof(double) * n);
174 memcpy(p.P, P, sizeof(double) * n * n);
175 for (uint32_t i = 0; i < m; i++) {
176 double s = 0.0;
177 for (uint32_t j = 0; j < n; j++) s += mdl->H[i * n + j] * x[j];
178 p.y_mean[i] = s;
179 }
180 double PHt[MAT] = {0}, S[MAT] = {0};
181 mmt(P, mdl->H, PHt, n, n, m);
182 mm(mdl->H, PHt, S, m, n, m);
183 for (uint32_t i = 0; i < m * m; i++) S[i] += mdl->R[i];
184 symmetrize(S, m);
185 memcpy(p.S, S, sizeof(double) * m * m);
186 st = est_check_prediction(&p);
187 if (st != EST_OK) return st;
188 *out = p;
189 return EST_OK;
190}
191
192/* Shared staleness / binding checks for update and coast. The prediction is
193 * not trusted: it is recomputed from the prior belief, the model and the
194 * control input it records, and must match bit for bit (canonical encoding). */
195static est_status bind_checks(const est_model *mdl, const est_belief *prior,
196 const est_prediction *pred, est_digest *pred_digest,
197 est_digest *prior_digest)
198{
199 est_status st = est_check_model(mdl);
200 if (st != EST_OK) return st;
201 st = est_check_belief(prior);
202 if (st != EST_OK) return st;
203 st = est_check_prediction(pred);
204 if (st != EST_OK) return st;
205 est_digest pd;
206 st = est_digest_belief(prior, &pd);
207 if (st != EST_OK) return st;
208 *prior_digest = pd;
209 if (memcmp(pd.b, pred->prior.b, EST_DIGEST_SIZE) != 0) return EST_ERR_STALE;
210 if (pred->generation != prior->generation + 1u || prior->generation == UINT64_MAX) return EST_ERR_STALE;
211 st = model_digest_matches(mdl, &prior->model);
212 if (st != EST_OK) return st;
213 if (memcmp(pred->model.b, prior->model.b, EST_DIGEST_SIZE) != 0) return EST_ERR_MODEL;
214 if (pred->n != mdl->n || pred->m != mdl->m || prior->n != mdl->n) return EST_ERR_DIM;
215 est_prediction re;
216 st = est_kf_predict(mdl, prior, pred->has_control ? pred->Bu : NULL, pred->horizon, &re);
217 if (st != EST_OK) return st;
218 est_digest rd;
219 st = est_digest_prediction(&re, &rd);
220 if (st != EST_OK) return st;
221 st = est_digest_prediction(pred, pred_digest);
222 if (st != EST_OK) return st;
223 return memcmp(rd.b, pred_digest->b, EST_DIGEST_SIZE) == 0 ? EST_OK : EST_ERR_STALE;
224}
225
226static int dig_eq(const est_digest *a, const est_digest *b)
227{
228 return memcmp(a->b, b->b, EST_DIGEST_SIZE) == 0;
229}
230
231est_status est_kf_update(const est_model *mdl, const est_belief *prior_belief,
232 const est_prediction *pred, const est_observation *obs,
233 est_belief *posterior, est_innovation *innovation)
234{
235 if (!mdl || !prior_belief || !pred || !obs || !posterior) return EST_ERR_NULL;
236 est_digest pdig, priordig;
237 est_status st = bind_checks(mdl, prior_belief, pred, &pdig, &priordig);
238 if (st != EST_OK) return st;
239 st = est_check_observation(obs);
240 if (st != EST_OK) return st;
241 /* A prediction or belief must not be passed off as evidence. */
242 if (dig_eq(&obs->evidence, &pdig) || dig_eq(&obs->evidence, &priordig) ||
243 dig_eq(&obs->evidence, &prior_belief->parent) ||
244 dig_eq(&obs->evidence, &prior_belief->evidence_root) ||
245 dig_eq(&obs->evidence, &prior_belief->model))
246 return EST_ERR_KIND;
247 uint32_t n = mdl->n, m = mdl->m;
248 if (obs->m != m) return EST_ERR_DIM;
249 if (obs->t_ns != pred->t_ns) return EST_ERR_TIME;
250 st = units_match(obs->unit, mdl->obs_unit, m);
251 if (st != EST_OK) return st;
252
253 est_digest odig, root;
254 st = est_digest_observation(obs, &odig);
255 if (st != EST_OK) return st;
256 st = est_evidence_root_extend(&prior_belief->evidence_root, &odig, &root);
257 if (st != EST_OK) return st;
258
259 double nu[D] = {0}, PHt[MAT] = {0}, S[MAT] = {0}, L[MAT] = {0};
260 for (uint32_t i = 0; i < m; i++) {
261 double s = 0.0;
262 for (uint32_t j = 0; j < n; j++) s += mdl->H[i * n + j] * pred->x[j];
263 nu[i] = obs->z[i] - s;
264 }
265 mmt(pred->P, mdl->H, PHt, n, n, m); /* P' H^T, n x m */
266 mm(mdl->H, PHt, S, m, n, m);
267 for (uint32_t i = 0; i < m * m; i++) S[i] += obs->R[i];
268 symmetrize(S, m);
269 if (!chol(S, L, m)) return EST_ERR_NOT_PD;
270
271 double y[D] = {0}, K[MAT] = {0}, row[D] = {0}, krow[D] = {0};
272 fwd(L, nu, y, m);
273 double nis = 0.0;
274 for (uint32_t i = 0; i < m; i++) nis += y[i] * y[i];
275 for (uint32_t i = 0; i < n; i++) {
276 for (uint32_t j = 0; j < m; j++) row[j] = PHt[i * m + j];
277 fwd(L, row, y, m);
278 bwd(L, y, krow, m);
279 for (uint32_t j = 0; j < m; j++) K[i * m + j] = krow[j];
280 }
281
282 est_belief b;
283 memset(&b, 0, sizeof b);
284 b.n = n;
285 for (uint32_t i = 0; i < n; i++) {
286 double s = 0.0;
287 for (uint32_t j = 0; j < m; j++) s += K[i * m + j] * nu[j];
288 b.x[i] = pred->x[i] + s;
289 b.unit[i] = mdl->state_unit[i];
290 }
291 /* Joseph form: A = I - K H, P = A P' A^T + K R K^T */
292 double A[MAT] = {0}, T[MAT] = {0}, P1[MAT] = {0}, KR[MAT] = {0}, P2[MAT] = {0};
293 mm(K, mdl->H, T, n, m, n);
294 for (uint32_t i = 0; i < n; i++)
295 for (uint32_t j = 0; j < n; j++) A[i * n + j] = (i == j ? 1.0 : 0.0) - T[i * n + j];
296 mm(A, pred->P, T, n, n, n);
297 mmt(T, A, P1, n, n, n);
298 mm(K, obs->R, KR, n, m, m);
299 mmt(KR, K, P2, n, m, n);
300 for (uint32_t i = 0; i < n * n; i++) b.P[i] = P1[i] + P2[i];
301 symmetrize(b.P, n);
302 b.generation = pred->generation + 1u;
303 b.t_ns = pred->t_ns;
304 b.model = prior_belief->model;
305 b.parent = pdig;
306 b.evidence_root = root;
307 st = est_check_belief(&b);
308 if (st != EST_OK) return st;
309
310 est_innovation iv;
311 memset(&iv, 0, sizeof iv);
312 iv.prediction = pdig;
313 iv.observation = odig;
314 iv.model = prior_belief->model;
315 iv.m = m;
316 memcpy(iv.nu, nu, sizeof(double) * m);
317 memcpy(iv.S, S, sizeof(double) * m * m);
318 iv.nis = nis;
319 iv.t_ns = pred->t_ns;
320 st = est_check_innovation(&iv);
321 if (st != EST_OK) return st;
322
323 *posterior = b;
324 if (innovation) *innovation = iv;
325 return EST_OK;
326}
327
328est_status est_kf_coast(const est_model *mdl, const est_prediction *pred,
329 const est_belief *prior_belief, est_belief *out)
330{
331 if (!mdl || !pred || !prior_belief || !out) return EST_ERR_NULL;
332 est_digest pdig, priordig;
333 est_status st = bind_checks(mdl, prior_belief, pred, &pdig, &priordig);
334 if (st != EST_OK) return st;
335 est_belief b;
336 memset(&b, 0, sizeof b);
337 b.n = pred->n;
338 for (uint32_t i = 0; i < b.n; i++) {
339 b.unit[i] = mdl->state_unit[i];
340 b.x[i] = pred->x[i];
341 }
342 memcpy(b.P, pred->P, sizeof(double) * b.n * b.n);
343 b.generation = pred->generation + 1u;
344 b.t_ns = pred->t_ns;
345 b.model = prior_belief->model;
346 b.parent = pdig;
347 b.evidence_root = prior_belief->evidence_root;
348 st = est_check_belief(&b);
349 if (st != EST_OK) return st;
350 *out = b;
351 return EST_OK;
352}