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 / physics0 / pd0_relation.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 | /* PD-0 relations in PDLAW1 delta form (spec sections 6.2 and 7): |
| 18 | * D target = sum over terms of coef * monomial, monomial = prod var^ex * prod chan^ex |
| 19 | * coef in micro-units, monomial products via pd0_mul; a term with all |
| 20 | * exponents 0 is the constant monomial 1.0 (1_000_000 micro). |
| 21 | * Variable order: observed, then latent (latent start at 0 on every reset). |
| 22 | * Also here: rollout prediction, one-step residual, NRMSE, size |R|, and the |
| 23 | * PDLAW1 law record encoder. No generator knowledge. */ |
| 24 | #ifndef PD0_RELATION_H |
| 25 | #define PD0_RELATION_H |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | #include "physics0/pd0_wire.h" |
| 31 | |
| 32 | #define PD0_MAX_LATENT 2 |
| 33 | #define PD0_MAX_VARS (PD0_MAX_OBS + PD0_MAX_LATENT) |
| 34 | #define PD0_MAX_EX (PD0_MAX_VARS + PD0_MAX_CH) |
| 35 | #define PD0_MAX_TERMS 12 |
| 36 | #define PD0_MAX_DEG 3 |
| 37 | #define PD0_SCHED_STEPS 20 |
| 38 | |
| 39 | typedef struct { int64_t coef; uint8_t ex[PD0_MAX_EX]; } pd0_term; |
| 40 | typedef struct { uint8_t target, n_terms; pd0_term t[PD0_MAX_TERMS]; } pd0_eq; |
| 41 | typedef struct { |
| 42 | uint8_t n_vars, n_latent, n_channels, n_eq; |
| 43 | pd0_eq eq[PD0_MAX_VARS]; |
| 44 | } pd0_relation; |
| 45 | |
| 46 | /* a schedule: reset state (observed vars) + up to PD0_SCHED_STEPS steps */ |
| 47 | typedef struct { |
| 48 | uint8_t n_obs, n_steps; |
| 49 | int64_t reset[PD0_MAX_OBS]; |
| 50 | uint8_t channel[PD0_SCHED_STEPS]; |
| 51 | int64_t value[PD0_SCHED_STEPS]; |
| 52 | } pd0_sched; |
| 53 | |
| 54 | /* a trajectory of observed states: traj[0] = reset, traj[i] after step i */ |
| 55 | typedef struct { uint8_t n_obs, n; int64_t s[PD0_SCHED_STEPS + 1][PD0_MAX_OBS]; } pd0_traj; |
| 56 | |
| 57 | int pd0_relation_valid(const pd0_relation *r); /* 0 ok, -1 malformed */ |
| 58 | int pd0_relation_size(const pd0_relation *r); /* |R| of section 6.2 */ |
| 59 | int pd0_relation_degree_ok(const pd0_relation *r); /* every monomial degree <= 3 */ |
| 60 | /* rev 2 section 6.2 rule: sum over terms (8*(n_vars+n_channels) + 24) + 8*n_latent */ |
| 61 | uint32_t pd0_relation_description_bits(const pd0_relation *r); |
| 62 | /* evaluate the monomial of term t at state (vars incl latent) + channels */ |
| 63 | int64_t pd0_monomial(const pd0_term *t, const int64_t *vars, const int64_t *u, int n_vars, int n_ch); |
| 64 | /* one tick: vars_in (n_vars) + u -> vars_out; equations without a target |
| 65 | * leave the variable unchanged. */ |
| 66 | void pd0_relation_step(const pd0_relation *r, const int64_t *vars_in, const int64_t *u, int64_t *vars_out); |
| 67 | /* roll out a schedule from its reset state (latent 0) into traj */ |
| 68 | void pd0_relation_rollout(const pd0_relation *r, const pd0_sched *s, pd0_traj *out); |
| 69 | /* NRMSE of section 6.1 over a set of (predicted, true) trajectories: |
| 70 | * max_j RMSE_j / std_j. Trajectory step 0 (reset) is excluded (it is given). |
| 71 | * std_j == 0 gives +inf unless RMSE_j == 0 too. */ |
| 72 | double pd0_nrmse(const pd0_traj *pred, const pd0_traj *truth, int n_traj); |
| 73 | /* SHA-256 of a schedule's canonical bytes */ |
| 74 | void pd0_sched_hash(const pd0_sched *s, uint8_t out[PD0_HASH]); |
| 75 | /* 1 if every true term's monomial appears in r (6.3 support) */ |
| 76 | int pd0_relation_supports(const pd0_relation *r, const pd0_relation *truth); |
| 77 | /* max over true terms of |coef - true| / |true|, in ppm; -1 if support fails */ |
| 78 | int64_t pd0_relation_coef_err_ppm(const pd0_relation *r, const pd0_relation *truth); |
| 79 | |
| 80 | /* ---- PDLAW1 (section 7) ---- */ |
| 81 | enum { PD0_LAW_REJECTED = 0, PD0_LAW_REFUTED = 1, PD0_LAW_HYPOTHESIS = 2, PD0_LAW_PROVISIONAL = 3 }; |
| 82 | #define PD0_MAX_EXCEPT 16 |
| 83 | #define PD0_MAX_EXPER 16 |
| 84 | |
| 85 | typedef struct { uint64_t seq; uint8_t hash[PD0_HASH]; int64_t predicted, observed, error_micro; } pd0_exception; |
| 86 | typedef struct { |
| 87 | uint8_t id[PD0_HASH], kind, prereg[PD0_HASH], outcome[PD0_HASH], result; |
| 88 | uint64_t first_seq, last_seq; |
| 89 | } pd0_experiment; |
| 90 | typedef struct { |
| 91 | int64_t var_min[PD0_MAX_OBS], var_max[PD0_MAX_OBS]; |
| 92 | int64_t ch_min[PD0_MAX_CH], ch_max[PD0_MAX_CH]; |
| 93 | int64_t dt_micro; |
| 94 | uint64_t n_observations; |
| 95 | uint32_t n_episodes; |
| 96 | int64_t reset_min, reset_max; |
| 97 | uint32_t episode_len; |
| 98 | int64_t latent_reset; |
| 99 | } pd0_domain; |
| 100 | typedef struct { |
| 101 | uint8_t state; |
| 102 | uint8_t law_id[PD0_HASH]; |
| 103 | pd0_relation rel; |
| 104 | pd0_domain dom; |
| 105 | uint32_t confidence_ppm; |
| 106 | uint16_t n_exceptions, n_experiments; |
| 107 | pd0_exception exc[PD0_MAX_EXCEPT]; |
| 108 | pd0_experiment exp[PD0_MAX_EXPER]; |
| 109 | uint8_t chain_root[PD0_HASH]; |
| 110 | uint16_t n_refutations; /* rev 2: passes through REFUTED before this record */ |
| 111 | uint32_t eps_micro; /* declared eps (NRMSE as ppm of 1.0) for claim_text */ |
| 112 | } pd0_law; |
| 113 | |
| 114 | #define PD0_LAW_MAX 4096 |
| 115 | /* encodes canonical bytes, computes law_id; returns bytes or 0 */ |
| 116 | size_t pd0_law_encode(pd0_law *l, uint8_t *out, size_t cap); |
| 117 | int pd0_law_decode(const uint8_t *in, size_t len, pd0_law *out); /* 0 ok; -1 refused */ |
| 118 | /* section 7 rule: floor(1e6 * (p+1) / (p+f+2)) */ |
| 119 | uint32_t pd0_confidence_ppm(uint32_t p, uint32_t f); |
| 120 | /* the fixed claim_text template into buf; returns length */ |
| 121 | size_t pd0_claim_text(const pd0_law *l, char *buf, size_t cap); |
| 122 | /* human JSON projection (non-canonical) */ |
| 123 | size_t pd0_law_json(const pd0_law *l, char *buf, size_t cap); |
| 124 | void pd0_hex(const uint8_t *h, size_t n, char *out); /* 2n+1 bytes */ |
| 125 | |
| 126 | #endif |