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
omega-mv / src / estimation / est_kf.h
| 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: the first estimator behind the EST-0 contract. |
| 18 | * |
| 19 | * Pure functions over est_types.h records. Deterministic for identical inputs |
| 20 | * (build with -ffp-contract=off -fno-fast-math), bounded (EST_MAX_DIM), no |
| 21 | * allocation, no globals. Inputs are const; every output is a new record. |
| 22 | * |
| 23 | * predict: x' = F x (+ B u), P' = F P F^T + Q, repeated `horizon` times. |
| 24 | * Also fills y_mean = H x', S = H P' H^T + R. |
| 25 | * update: nu = z - H x', S = H P' H^T + R_obs, K = P' H^T S^-1, |
| 26 | * x = x' + K nu, P = (I - K H) P' (I - K H)^T + K R_obs K^T (Joseph), |
| 27 | * then P is symmetrized. R_obs is the observation's declared noise. |
| 28 | * A missing observation is simply no update: the prediction stands and its |
| 29 | * covariance keeps growing through repeated predict. |
| 30 | * |
| 31 | * Refusals (never silently repaired): stale generation or parent digest, |
| 32 | * model digest mismatch, unit mismatch, non-finite input or output, |
| 33 | * asymmetric / non-PSD covariance, singular S, time that does not advance. */ |
| 34 | #ifndef OMEGA_EST_KF_H |
| 35 | #define OMEGA_EST_KF_H |
| 36 | |
| 37 | #include "est_types.h" |
| 38 | |
| 39 | /* Declared prior: evidence_root zero, parent zero, generation 0. */ |
| 40 | est_status est_kf_prior(const est_model *mdl, const double *x0, const double *P0, |
| 41 | int64_t t_ns, est_belief *out); |
| 42 | |
| 43 | /* Optional known input u (length n, already multiplied by B); may be NULL. */ |
| 44 | est_status est_kf_predict(const est_model *mdl, const est_belief *b, |
| 45 | const double *Bu, uint32_t horizon, est_prediction *out); |
| 46 | |
| 47 | /* The prediction is recomputed from prior_belief, mdl and the control input it |
| 48 | * records, and must equal the supplied one bit for bit (else EST_ERR_STALE; |
| 49 | * EST_ERR_MODEL for a different model). Observation refusals: EST_ERR_KIND if |
| 50 | * source is zero or evidence is zero or equals a digest of the prediction, the |
| 51 | * prior belief, its parent, its evidence root or its model. The estimator cannot |
| 52 | * check that evidence digests match raw bytes: that binding is the caller's. */ |
| 53 | /* Needs the belief the prediction came from (checked by digest) so the |
| 54 | * posterior can extend its evidence root. The observation is only read. */ |
| 55 | est_status est_kf_update(const est_model *mdl, const est_belief *prior_belief, |
| 56 | const est_prediction *pred, const est_observation *obs, |
| 57 | est_belief *posterior, est_innovation *innovation); |
| 58 | |
| 59 | /* Turn a prediction into a belief without an observation (for a missing |
| 60 | * measurement); generation and parent follow the prediction. */ |
| 61 | est_status est_kf_coast(const est_model *mdl, const est_prediction *pred, |
| 62 | const est_belief *prior_belief, est_belief *out); |
| 63 | |
| 64 | #endif /* OMEGA_EST_KF_H */ |