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_types.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note EST-0 pure seeded OM-2 implementation: validation, covariance PSD/PD checks, |
| 3 | note dense matrix/Cholesky/triangular-solve primitives, canonical LE codecs, |
| 4 | note domain-separated SHA-256 digests, and evidence root chaining. |
| 5 | |
| 6 | text EST_DOM_MODEL "omega.est.model.v1" |
| 7 | text EST_DOM_OBS "omega.est.observation.v1" |
| 8 | text EST_DOM_BELIEF "omega.est.belief.v1" |
| 9 | text EST_DOM_PRED "omega.est.prediction.v1" |
| 10 | text EST_DOM_INNOV "omega.est.innovation.v1" |
| 11 | text EST_DOM_ROOT "omega.est.belief.v1-root" |
| 12 | |
| 13 | fn est_in_dim d ulong to bool |
| 14 | ret and ge d 1 le d EST_MAX_DIM |
| 15 | end |
| 16 | |
| 17 | fn est_digest_is_zero d view uchar 32 to bool |
| 18 | acc = ulong 0 |
| 19 | for i ulong 32 |
| 20 | acc = or acc ulong get d i |
| 21 | end |
| 22 | ret eq acc 0 |
| 23 | end |
| 24 | |
| 25 | fn est_digest_eq a view uchar 32 b view uchar 32 to bool |
| 26 | diff = ulong 0 |
| 27 | for i ulong 32 |
| 28 | diff = or diff xor ulong get a i ulong get b i |
| 29 | end |
| 30 | ret eq diff 0 |
| 31 | end |
| 32 | |
| 33 | fn est_rec_dig_is_zero rec view ulong * off ulong to bool |
| 34 | acc = ulong 0 |
| 35 | for i ulong 4 |
| 36 | acc = or acc get rec add off i |
| 37 | end |
| 38 | ret eq acc 0 |
| 39 | end |
| 40 | |
| 41 | fn est_rec_get_dig rec view ulong * off ulong dst edit uchar 32 to void |
| 42 | for w ulong 4 |
| 43 | v = get rec add off w |
| 44 | for b ulong 8 |
| 45 | put dst add mul w 8 b uchar and shr v mul b 8 0xff |
| 46 | end |
| 47 | end |
| 48 | end |
| 49 | |
| 50 | fn est_rec_put_dig rec edit ulong * off ulong src view uchar 32 to void |
| 51 | for w ulong 4 |
| 52 | v = ulong 0 |
| 53 | for b ulong 8 |
| 54 | v = or v shl ulong get src add mul w 8 b mul b 8 |
| 55 | end |
| 56 | put rec add off w v |
| 57 | end |
| 58 | end |
| 59 | |
| 60 | fn est_rec_copy src view ulong * dst edit ulong * n ulong to void |
| 61 | for i ulong n |
| 62 | put dst i get src i |
| 63 | end |
| 64 | end |
| 65 | |
| 66 | fn est_cov_maxabs a view f64 * n ulong to f64 |
| 67 | maxabs = f64 0.0 |
| 68 | for i ulong n |
| 69 | for j ulong n |
| 70 | v = fabs get a add mul i n j |
| 71 | if gt v maxabs |
| 72 | maxabs = v |
| 73 | end |
| 74 | end |
| 75 | end |
| 76 | ret maxabs |
| 77 | end |
| 78 | |
| 79 | fn est_cov_sym a view f64 * n ulong to long |
| 80 | for i ulong n |
| 81 | if lt get a add mul i n i 0.0f64 |
| 82 | ret EST_ERR_NOT_PSD |
| 83 | end |
| 84 | end |
| 85 | stol = mul 1.0e-9f64 est_cov_maxabs a n |
| 86 | if lt stol 1.0e-300f64 |
| 87 | stol = 1.0e-300f64 |
| 88 | end |
| 89 | for i ulong n |
| 90 | for j ulong i |
| 91 | aij = get a add mul i n j |
| 92 | aji = get a add mul j n i |
| 93 | if gt fabs sub aij aji stol |
| 94 | ret EST_ERR_ASYMMETRIC |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | ret EST_OK |
| 99 | end |
| 100 | |
| 101 | fn est_cov_scale_row a view f64 * work edit f64 72 base ulong i ulong to long |
| 102 | si = get work i |
| 103 | rmax = add 1.0f64 1.0e-9f64 |
| 104 | for j ulong i |
| 105 | idx = add base j |
| 106 | c = get a idx |
| 107 | sj = get work j |
| 108 | if or eq si 0.0f64 eq sj 0.0f64 |
| 109 | if ne c 0.0f64 |
| 110 | ret EST_ERR_NOT_PSD |
| 111 | end |
| 112 | put work add 8 idx 0.0f64 |
| 113 | else |
| 114 | r = div div c si sj |
| 115 | if gt fabs r rmax |
| 116 | ret EST_ERR_NOT_PSD |
| 117 | end |
| 118 | put work add 8 idx r |
| 119 | end |
| 120 | end |
| 121 | ret EST_OK |
| 122 | end |
| 123 | |
| 124 | fn est_cov_scale a view f64 * n ulong work edit f64 72 to long |
| 125 | for i ulong n |
| 126 | put work i fsqrt get a add mul i n i |
| 127 | end |
| 128 | for i ulong n |
| 129 | off = add 8 add mul i n i |
| 130 | if gt get work i 0.0f64 |
| 131 | put work off 1.0f64 |
| 132 | else |
| 133 | put work off 0.0f64 |
| 134 | end |
| 135 | end |
| 136 | for i ulong n |
| 137 | rc = est_cov_scale_row a work mul i n i |
| 138 | if ne rc EST_OK |
| 139 | ret rc |
| 140 | end |
| 141 | end |
| 142 | ret EST_OK |
| 143 | end |
| 144 | |
| 145 | fn est_cov_ldlt_null n ulong k ulong work edit f64 72 to long |
| 146 | tol = f64 1.0e-12 |
| 147 | i = add k 1 |
| 148 | while lt i n bound 16 |
| 149 | c = get work add 8 add mul i n k |
| 150 | if gt mul c c tol |
| 151 | ret EST_ERR_NOT_PSD |
| 152 | end |
| 153 | i = add i 1 |
| 154 | end |
| 155 | ret EST_OK |
| 156 | end |
| 157 | |
| 158 | fn est_cov_ldlt_elim n ulong k ulong work edit f64 72 d f64 to void |
| 159 | i = add k 1 |
| 160 | while lt i n bound 16 |
| 161 | wik = get work add 8 add mul i n k |
| 162 | j = add k 1 |
| 163 | while le j i bound 16 |
| 164 | wjk = get work add 8 add mul j n k |
| 165 | off = add 8 add mul i n j |
| 166 | cur = get work off |
| 167 | put work off sub cur div mul wik wjk d |
| 168 | j = add j 1 |
| 169 | end |
| 170 | i = add i 1 |
| 171 | end |
| 172 | end |
| 173 | |
| 174 | fn est_cov_ldlt n ulong work edit f64 72 to long |
| 175 | tol = f64 1.0e-12 |
| 176 | ntol = neg tol |
| 177 | for k ulong n |
| 178 | d = get work add 8 add mul k n k |
| 179 | if lt d ntol |
| 180 | ret EST_ERR_NOT_PSD |
| 181 | end |
| 182 | if le d tol |
| 183 | rc = est_cov_ldlt_null n k work |
| 184 | if ne rc EST_OK |
| 185 | ret rc |
| 186 | end |
| 187 | else |
| 188 | est_cov_ldlt_elim n k work d |
| 189 | end |
| 190 | end |
| 191 | ret EST_OK |
| 192 | end |
| 193 | |
| 194 | fn est_check_covariance a view f64 * n ulong to long |
| 195 | if not est_in_dim n |
| 196 | ret EST_ERR_DIM |
| 197 | end |
| 198 | nn = mul n n |
| 199 | for i ulong nn |
| 200 | if not est_isfinite get a i |
| 201 | ret EST_ERR_NONFINITE |
| 202 | end |
| 203 | end |
| 204 | rc = est_cov_sym a n |
| 205 | if ne rc EST_OK |
| 206 | ret rc |
| 207 | end |
| 208 | work = new f64 72 0.0 |
| 209 | rc = est_cov_scale a n work |
| 210 | if ne rc EST_OK |
| 211 | ret rc |
| 212 | end |
| 213 | ret est_cov_ldlt n work |
| 214 | end |
| 215 | |
| 216 | fn est_check_cov_rec rec view ulong * off ulong n ulong to long |
| 217 | mat = new f64 64 0.0 |
| 218 | for i ulong mul n n |
| 219 | put mat i bitcast f64 get rec add off i |
| 220 | end |
| 221 | ret est_check_covariance view mat n |
| 222 | end |
| 223 | |
| 224 | fn est_chol_dot l view f64 64 n ulong i ulong j ulong to f64 |
| 225 | acc = f64 0.0 |
| 226 | for k ulong j |
| 227 | acc = add acc mul get l add mul i n k get l add mul j n k |
| 228 | end |
| 229 | ret acc |
| 230 | end |
| 231 | |
| 232 | fn est_chol s view f64 * l edit f64 64 n ulong to bool |
| 233 | for i ulong 64 |
| 234 | put l i 0.0f64 |
| 235 | end |
| 236 | for i ulong n |
| 237 | for j ulong add i 1 |
| 238 | acc = sub get s add mul i n j est_chol_dot view l n i j |
| 239 | if eq i j |
| 240 | if or not gt acc 0.0f64 not est_isfinite acc |
| 241 | ret false |
| 242 | end |
| 243 | put l add mul i n i fsqrt acc |
| 244 | else |
| 245 | ljj = get l add mul j n j |
| 246 | put l add mul i n j div acc ljj |
| 247 | end |
| 248 | end |
| 249 | end |
| 250 | ret true |
| 251 | end |
| 252 | |
| 253 | fn est_fwd l view f64 64 b view f64 8 y edit f64 8 n ulong to void |
| 254 | for i ulong n |
| 255 | acc = get b i |
| 256 | for k ulong i |
| 257 | acc = sub acc mul get l add mul i n k get y k |
| 258 | end |
| 259 | put y i div acc get l add mul i n i |
| 260 | end |
| 261 | end |
| 262 | |
| 263 | fn est_bwd l view f64 64 y view f64 8 x edit f64 8 n ulong to void |
| 264 | ii = n |
| 265 | while gt ii 0 bound 16 |
| 266 | i = sub ii 1 |
| 267 | acc = get y i |
| 268 | k = add i 1 |
| 269 | while lt k n bound 16 |
| 270 | acc = sub acc mul get l add mul k n i get x k |
| 271 | k = add k 1 |
| 272 | end |
| 273 | put x i div acc get l add mul i n i |
| 274 | ii = sub ii 1 |
| 275 | end |
| 276 | end |
| 277 | |
| 278 | fn est_mm_dot a view f64 64 b view f64 64 dims view ulong 3 ij ulong to f64 |
| 279 | k = get dims 1 |
| 280 | cols = get dims 2 |
| 281 | i = div ij 8 |
| 282 | j = rem ij 8 |
| 283 | s = f64 0.0 |
| 284 | for t ulong k |
| 285 | av = get a add mul i k t |
| 286 | bv = get b add mul t cols j |
| 287 | s = add s mul av bv |
| 288 | end |
| 289 | ret s |
| 290 | end |
| 291 | |
| 292 | fn est_mmt_dot a view f64 64 b view f64 64 dims view ulong 3 ij ulong to f64 |
| 293 | k = get dims 1 |
| 294 | i = div ij 8 |
| 295 | j = rem ij 8 |
| 296 | s = f64 0.0 |
| 297 | for t ulong k |
| 298 | av = get a add mul i k t |
| 299 | bv = get b add mul j k t |
| 300 | s = add s mul av bv |
| 301 | end |
| 302 | ret s |
| 303 | end |
| 304 | |
| 305 | fn est_mm a view f64 64 b view f64 64 c edit f64 64 dims view ulong 3 to void |
| 306 | r = get dims 0 |
| 307 | cols = get dims 2 |
| 308 | for i ulong r |
| 309 | for j ulong cols |
| 310 | put c add mul i cols j est_mm_dot a b dims add mul i 8 j |
| 311 | end |
| 312 | end |
| 313 | end |
| 314 | |
| 315 | fn est_mmt a view f64 64 b view f64 64 c edit f64 64 dims view ulong 3 to void |
| 316 | r = get dims 0 |
| 317 | cols = get dims 2 |
| 318 | for i ulong r |
| 319 | for j ulong cols |
| 320 | put c add mul i cols j est_mmt_dot a b dims add mul i 8 j |
| 321 | end |
| 322 | end |
| 323 | end |
| 324 | |
| 325 | fn est_symmetrize a edit f64 * n ulong to void |
| 326 | for i ulong n |
| 327 | for j ulong i |
| 328 | p1 = add mul i n j |
| 329 | p2 = add mul j n i |
| 330 | v = mul 0.5f64 add get a p1 get a p2 |
| 331 | put a p1 v |
| 332 | put a p2 v |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | |
| 337 | note ---- Record validation ---- |
| 338 | |
| 339 | fn est_check_model mdl view ulong 280 to long |
| 340 | n = get mdl EM_N |
| 341 | m = get mdl EM_M |
| 342 | if or not est_in_dim n not est_in_dim m |
| 343 | ret EST_ERR_DIM |
| 344 | end |
| 345 | if ne get mdl EM_EST EST_ESTIMATOR_LINEAR_KALMAN |
| 346 | ret EST_ERR_UNIT |
| 347 | end |
| 348 | if ne get mdl EM_MEAN EST_UNCERTAINTY_GAUSSIAN |
| 349 | ret EST_ERR_UNIT |
| 350 | end |
| 351 | for i ulong n |
| 352 | v = get mdl add EM_SUNIT i |
| 353 | if or lt v 1 ge v EST_UNIT_MAX |
| 354 | ret EST_ERR_UNIT |
| 355 | end |
| 356 | end |
| 357 | for i ulong m |
| 358 | v = get mdl add EM_OUNIT i |
| 359 | if or lt v 1 ge v EST_UNIT_MAX |
| 360 | ret EST_ERR_UNIT |
| 361 | end |
| 362 | end |
| 363 | if le bitcast long get mdl EM_STEP 0 |
| 364 | ret EST_ERR_TIME |
| 365 | end |
| 366 | for i ulong mul n n |
| 367 | if not est_isfinite bitcast f64 get mdl add EM_F i |
| 368 | ret EST_ERR_NONFINITE |
| 369 | end |
| 370 | end |
| 371 | for i ulong mul m n |
| 372 | if not est_isfinite bitcast f64 get mdl add EM_H i |
| 373 | ret EST_ERR_NONFINITE |
| 374 | end |
| 375 | end |
| 376 | st = est_check_cov_rec mdl EM_Q n |
| 377 | if ne st EST_OK |
| 378 | ret st |
| 379 | end |
| 380 | rmat = new f64 64 0.0 |
| 381 | for i ulong mul m m |
| 382 | put rmat i bitcast f64 get mdl add EM_R i |
| 383 | end |
| 384 | st = est_check_covariance view rmat m |
| 385 | if ne st EST_OK |
| 386 | ret st |
| 387 | end |
| 388 | lmat = new f64 64 0.0 |
| 389 | if not est_chol view rmat lmat m |
| 390 | ret EST_ERR_NOT_PD |
| 391 | end |
| 392 | ret EST_OK |
| 393 | end |
| 394 | |
| 395 | fn est_check_observation o view ulong 96 to long |
| 396 | m = get o EO_M |
| 397 | if not est_in_dim m |
| 398 | ret EST_ERR_DIM |
| 399 | end |
| 400 | for i ulong m |
| 401 | v = get o add EO_UNIT i |
| 402 | if or lt v 1 ge v EST_UNIT_MAX |
| 403 | ret EST_ERR_UNIT |
| 404 | end |
| 405 | if not est_isfinite bitcast f64 get o add EO_Z i |
| 406 | ret EST_ERR_NONFINITE |
| 407 | end |
| 408 | end |
| 409 | st = est_check_cov_rec o EO_R m |
| 410 | if ne st EST_OK |
| 411 | ret st |
| 412 | end |
| 413 | if est_rec_dig_is_zero o EO_EV |
| 414 | ret EST_ERR_KIND |
| 415 | end |
| 416 | if est_rec_dig_is_zero o EO_SRC |
| 417 | ret EST_ERR_KIND |
| 418 | end |
| 419 | ret EST_OK |
| 420 | end |
| 421 | |
| 422 | fn est_check_belief b view ulong 96 to long |
| 423 | n = get b EB_N |
| 424 | if not est_in_dim n |
| 425 | ret EST_ERR_DIM |
| 426 | end |
| 427 | for i ulong n |
| 428 | v = get b add EB_UNIT i |
| 429 | if or lt v 1 ge v EST_UNIT_MAX |
| 430 | ret EST_ERR_UNIT |
| 431 | end |
| 432 | if not est_isfinite bitcast f64 get b add EB_X i |
| 433 | ret EST_ERR_NONFINITE |
| 434 | end |
| 435 | end |
| 436 | if est_rec_dig_is_zero b EB_MDL |
| 437 | ret EST_ERR_MODEL |
| 438 | end |
| 439 | if eq get b EB_GEN 0 |
| 440 | if or not est_rec_dig_is_zero b EB_PAR not est_rec_dig_is_zero b EB_ROOT |
| 441 | ret EST_ERR_STALE |
| 442 | end |
| 443 | else |
| 444 | if est_rec_dig_is_zero b EB_PAR |
| 445 | ret EST_ERR_STALE |
| 446 | end |
| 447 | end |
| 448 | ret est_check_cov_rec b EB_P n |
| 449 | end |
| 450 | |
| 451 | fn est_check_prediction p view ulong 176 to long |
| 452 | n = get p EP_N |
| 453 | m = get p EP_M |
| 454 | if or not est_in_dim n not est_in_dim m |
| 455 | ret EST_ERR_DIM |
| 456 | end |
| 457 | hz = get p EP_HZ |
| 458 | if or lt hz 1 gt hz EST_MAX_HORIZON |
| 459 | ret EST_ERR_TIME |
| 460 | end |
| 461 | hc = get p EP_HC |
| 462 | if gt hc 1 |
| 463 | ret EST_ERR_ENCODING |
| 464 | end |
| 465 | for i ulong n |
| 466 | if not est_isfinite bitcast f64 get p add EP_X i |
| 467 | ret EST_ERR_NONFINITE |
| 468 | end |
| 469 | end |
| 470 | if eq hc 1 |
| 471 | for i ulong n |
| 472 | if not est_isfinite bitcast f64 get p add EP_BU i |
| 473 | ret EST_ERR_NONFINITE |
| 474 | end |
| 475 | end |
| 476 | end |
| 477 | for i ulong m |
| 478 | if not est_isfinite bitcast f64 get p add EP_YM i |
| 479 | ret EST_ERR_NONFINITE |
| 480 | end |
| 481 | end |
| 482 | st = est_check_cov_rec p EP_P n |
| 483 | if ne st EST_OK |
| 484 | ret st |
| 485 | end |
| 486 | ret est_check_cov_rec p EP_S m |
| 487 | end |
| 488 | |
| 489 | fn est_check_innovation iv view ulong 96 to long |
| 490 | m = get iv EI_M |
| 491 | if not est_in_dim m |
| 492 | ret EST_ERR_DIM |
| 493 | end |
| 494 | for i ulong m |
| 495 | if not est_isfinite bitcast f64 get iv add EI_NU i |
| 496 | ret EST_ERR_NONFINITE |
| 497 | end |
| 498 | end |
| 499 | nis = bitcast f64 get iv EI_NIS |
| 500 | if not est_isfinite nis |
| 501 | ret EST_ERR_NONFINITE |
| 502 | end |
| 503 | if lt nis 0.0f64 |
| 504 | ret EST_ERR_NOT_PSD |
| 505 | end |
| 506 | ret est_check_cov_rec iv EI_S m |
| 507 | end |
| 508 | |
| 509 | note ---- Canonical LE byte helpers ---- |
| 510 | |
| 511 | fn est_put_u32 dst edit uchar * pos ulong v ulong to ulong |
| 512 | for i ulong 4 |
| 513 | put dst add pos i uchar and shr v mul i 8 0xff |
| 514 | end |
| 515 | ret add pos 4 |
| 516 | end |
| 517 | |
| 518 | fn est_put_u64 dst edit uchar * pos ulong v ulong to ulong |
| 519 | for i ulong 8 |
| 520 | put dst add pos i uchar and shr v mul i 8 0xff |
| 521 | end |
| 522 | ret add pos 8 |
| 523 | end |
| 524 | |
| 525 | fn est_put_f64_canon dst edit uchar * pos ulong d f64 to ulong |
| 526 | bits = bitcast ulong d |
| 527 | if eq and bits 0x7fffffffffffffff 0 |
| 528 | bits = 0 |
| 529 | end |
| 530 | ret est_put_u64 dst pos bits |
| 531 | end |
| 532 | |
| 533 | fn est_put_f64_bits dst edit uchar * pos ulong bits ulong to ulong |
| 534 | u = bits |
| 535 | if eq and u 0x7fffffffffffffff 0 |
| 536 | u = 0 |
| 537 | end |
| 538 | ret est_put_u64 dst pos u |
| 539 | end |
| 540 | |
| 541 | fn est_put_rec_dig dst edit uchar * pos ulong rec view ulong * off ulong to ulong |
| 542 | p = pos |
| 543 | for w ulong 4 |
| 544 | p = est_put_u64 dst p get rec add off w |
| 545 | end |
| 546 | ret p |
| 547 | end |
| 548 | |
| 549 | fn est_get_u32 src view uchar * pos ulong to ulong |
| 550 | v = ulong 0 |
| 551 | for i ulong 4 |
| 552 | v = or v shl ulong get src add pos i mul i 8 |
| 553 | end |
| 554 | ret v |
| 555 | end |
| 556 | |
| 557 | fn est_get_u64 src view uchar * pos ulong to ulong |
| 558 | v = ulong 0 |
| 559 | for i ulong 8 |
| 560 | v = or v shl ulong get src add pos i mul i 8 |
| 561 | end |
| 562 | ret v |
| 563 | end |
| 564 | |
| 565 | fn est_get_rec_dig src view uchar * pos ulong rec edit ulong * off ulong to ulong |
| 566 | p = pos |
| 567 | for w ulong 4 |
| 568 | put rec add off w est_get_u64 src p |
| 569 | p = add p 8 |
| 570 | end |
| 571 | ret p |
| 572 | end |
| 573 | |
| 574 | fn est_write_hdr dst edit uchar * kind ulong to ulong |
| 575 | put dst 0 uchar 79 |
| 576 | put dst 1 uchar 69 |
| 577 | put dst 2 uchar 83 |
| 578 | put dst 3 uchar 84 |
| 579 | put dst 4 uchar and kind 0xff |
| 580 | put dst 5 uchar 1 |
| 581 | put dst 6 uchar 0 |
| 582 | put dst 7 uchar 0 |
| 583 | ret 8 |
| 584 | end |
| 585 | |
| 586 | fn est_check_hdr src view uchar * slen ulong want ulong to long |
| 587 | if lt slen 8 |
| 588 | ret EST_ERR_ENCODING |
| 589 | end |
| 590 | if or ne ulong get src 0 79 ne ulong get src 1 69 |
| 591 | ret EST_ERR_ENCODING |
| 592 | end |
| 593 | if or ne ulong get src 2 83 ne ulong get src 3 84 |
| 594 | ret EST_ERR_ENCODING |
| 595 | end |
| 596 | if ne ulong get src 4 want |
| 597 | ret EST_ERR_KIND |
| 598 | end |
| 599 | if or ne ulong get src 5 1 or ne ulong get src 6 0 ne ulong get src 7 0 |
| 600 | ret EST_ERR_ENCODING |
| 601 | end |
| 602 | ret EST_OK |
| 603 | end |
| 604 | |
| 605 | note ---- Encoders ---- |
| 606 | |
| 607 | fn est_encode_model o view ulong 280 buf edit uchar * out_len edit ulong 1 to long |
| 608 | st = est_check_model o |
| 609 | if ne st EST_OK |
| 610 | ret st |
| 611 | end |
| 612 | n = get o EM_N |
| 613 | m = get o EM_M |
| 614 | fslots = add mul 2 mul n n add mul m n mul m m |
| 615 | need = add 32 add mul 4 add n m mul 8 fslots |
| 616 | if gt need len buf |
| 617 | ret EST_ERR_ENCODING |
| 618 | end |
| 619 | pos = est_write_hdr buf EST_KIND_MODEL |
| 620 | pos = est_put_u32 buf pos n |
| 621 | pos = est_put_u32 buf pos m |
| 622 | pos = est_put_u32 buf pos get o EM_EST |
| 623 | pos = est_put_u32 buf pos get o EM_MEAN |
| 624 | for i ulong n |
| 625 | pos = est_put_u32 buf pos get o add EM_SUNIT i |
| 626 | end |
| 627 | for i ulong m |
| 628 | pos = est_put_u32 buf pos get o add EM_OUNIT i |
| 629 | end |
| 630 | for i ulong mul n n |
| 631 | pos = est_put_f64_bits buf pos get o add EM_F i |
| 632 | end |
| 633 | for i ulong mul n n |
| 634 | pos = est_put_f64_bits buf pos get o add EM_Q i |
| 635 | end |
| 636 | for i ulong mul m n |
| 637 | pos = est_put_f64_bits buf pos get o add EM_H i |
| 638 | end |
| 639 | for i ulong mul m m |
| 640 | pos = est_put_f64_bits buf pos get o add EM_R i |
| 641 | end |
| 642 | pos = est_put_u64 buf pos get o EM_STEP |
| 643 | put out_len 0 pos |
| 644 | ret EST_OK |
| 645 | end |
| 646 | |
| 647 | fn est_encode_observation o view ulong 96 buf edit uchar * out_len edit ulong 1 to long |
| 648 | st = est_check_observation o |
| 649 | if ne st EST_OK |
| 650 | ret st |
| 651 | end |
| 652 | m = get o EO_M |
| 653 | need = add 92 add mul 4 m mul 8 add m mul m m |
| 654 | if gt need len buf |
| 655 | ret EST_ERR_ENCODING |
| 656 | end |
| 657 | pos = est_write_hdr buf EST_KIND_OBSERVATION |
| 658 | pos = est_put_u32 buf pos m |
| 659 | for i ulong m |
| 660 | pos = est_put_u32 buf pos get o add EO_UNIT i |
| 661 | end |
| 662 | for i ulong m |
| 663 | pos = est_put_f64_bits buf pos get o add EO_Z i |
| 664 | end |
| 665 | for i ulong mul m m |
| 666 | pos = est_put_f64_bits buf pos get o add EO_R i |
| 667 | end |
| 668 | pos = est_put_u64 buf pos get o EO_TNS |
| 669 | pos = est_put_u64 buf pos get o EO_SEQ |
| 670 | pos = est_put_rec_dig buf pos o EO_SRC |
| 671 | pos = est_put_rec_dig buf pos o EO_EV |
| 672 | put out_len 0 pos |
| 673 | ret EST_OK |
| 674 | end |
| 675 | |
| 676 | fn est_encode_belief o view ulong 96 buf edit uchar * out_len edit ulong 1 to long |
| 677 | st = est_check_belief o |
| 678 | if ne st EST_OK |
| 679 | ret st |
| 680 | end |
| 681 | n = get o EB_N |
| 682 | need = add 124 add mul 4 n mul 8 add n mul n n |
| 683 | if gt need len buf |
| 684 | ret EST_ERR_ENCODING |
| 685 | end |
| 686 | pos = est_write_hdr buf EST_KIND_BELIEF |
| 687 | pos = est_put_u32 buf pos n |
| 688 | for i ulong n |
| 689 | pos = est_put_u32 buf pos get o add EB_UNIT i |
| 690 | end |
| 691 | for i ulong n |
| 692 | pos = est_put_f64_bits buf pos get o add EB_X i |
| 693 | end |
| 694 | for i ulong mul n n |
| 695 | pos = est_put_f64_bits buf pos get o add EB_P i |
| 696 | end |
| 697 | pos = est_put_u64 buf pos get o EB_GEN |
| 698 | pos = est_put_u64 buf pos get o EB_TNS |
| 699 | pos = est_put_rec_dig buf pos o EB_MDL |
| 700 | pos = est_put_rec_dig buf pos o EB_PAR |
| 701 | pos = est_put_rec_dig buf pos o EB_ROOT |
| 702 | put out_len 0 pos |
| 703 | ret EST_OK |
| 704 | end |
| 705 | |
| 706 | fn est_put_f64_arr dst edit uchar * pos ulong rec view ulong * oc ulong to ulong |
| 707 | off = shr oc 16 |
| 708 | cnt = and oc 0xffff |
| 709 | p = pos |
| 710 | for i ulong cnt |
| 711 | p = est_put_f64_bits dst p get rec add off i |
| 712 | end |
| 713 | ret p |
| 714 | end |
| 715 | |
| 716 | fn est_encode_prediction o view ulong 176 buf edit uchar * out_len edit ulong 1 to long |
| 717 | st = est_check_prediction o |
| 718 | if ne st EST_OK |
| 719 | ret st |
| 720 | end |
| 721 | n = get o EP_N |
| 722 | m = get o EP_M |
| 723 | pos = add add n mul n n add m mul m m |
| 724 | if eq get o EP_HC 1 |
| 725 | pos = add pos n |
| 726 | end |
| 727 | if gt add 104 mul 8 pos len buf |
| 728 | ret EST_ERR_ENCODING |
| 729 | end |
| 730 | pos = est_write_hdr buf EST_KIND_PREDICTION |
| 731 | pos = est_put_rec_dig buf pos o EP_PRIOR |
| 732 | pos = est_put_rec_dig buf pos o EP_MDL |
| 733 | pos = est_put_u32 buf pos get o EP_HZ |
| 734 | pos = est_put_u64 buf pos get o EP_GEN |
| 735 | pos = est_put_u64 buf pos get o EP_TNS |
| 736 | pos = est_put_u32 buf pos n |
| 737 | pos = est_put_u32 buf pos m |
| 738 | pos = est_put_f64_arr buf pos o add shl EP_X 16 n |
| 739 | pos = est_put_f64_arr buf pos o add shl EP_P 16 mul n n |
| 740 | pos = est_put_f64_arr buf pos o add shl EP_YM 16 m |
| 741 | pos = est_put_f64_arr buf pos o add shl EP_S 16 mul m m |
| 742 | pos = est_put_u32 buf pos get o EP_HC |
| 743 | if eq get o EP_HC 1 |
| 744 | pos = est_put_f64_arr buf pos o add shl EP_BU 16 n |
| 745 | end |
| 746 | put out_len 0 pos |
| 747 | ret EST_OK |
| 748 | end |
| 749 | |
| 750 | fn est_encode_innovation o view ulong 96 buf edit uchar * out_len edit ulong 1 to long |
| 751 | st = est_check_innovation o |
| 752 | if ne st EST_OK |
| 753 | ret st |
| 754 | end |
| 755 | m = get o EI_M |
| 756 | need = add 124 mul 8 add m mul m m |
| 757 | if gt need len buf |
| 758 | ret EST_ERR_ENCODING |
| 759 | end |
| 760 | pos = est_write_hdr buf EST_KIND_INNOVATION |
| 761 | pos = est_put_rec_dig buf pos o EI_PRED |
| 762 | pos = est_put_rec_dig buf pos o EI_OBS |
| 763 | pos = est_put_rec_dig buf pos o EI_MDL |
| 764 | pos = est_put_u32 buf pos m |
| 765 | for i ulong m |
| 766 | pos = est_put_f64_bits buf pos get o add EI_NU i |
| 767 | end |
| 768 | for i ulong mul m m |
| 769 | pos = est_put_f64_bits buf pos get o add EI_S i |
| 770 | end |
| 771 | pos = est_put_f64_bits buf pos get o EI_NIS |
| 772 | pos = est_put_u64 buf pos get o EI_TNS |
| 773 | put out_len 0 pos |
| 774 | ret EST_OK |
| 775 | end |
| 776 | |
| 777 | note ---- Decoders ---- |
| 778 | |
| 779 | fn est_get_f64_arr src view uchar * pos ulong rec edit ulong * oc ulong to ulong |
| 780 | off = shr oc 16 |
| 781 | cnt = and oc 0xffff |
| 782 | p = pos |
| 783 | for i ulong cnt |
| 784 | put rec add off i est_get_u64 src p |
| 785 | p = add p 8 |
| 786 | end |
| 787 | ret p |
| 788 | end |
| 789 | |
| 790 | fn est_decode_model buf view uchar * blen ulong dst edit ulong 280 to long |
| 791 | st = est_check_hdr buf blen EST_KIND_MODEL |
| 792 | if ne st EST_OK |
| 793 | ret st |
| 794 | end |
| 795 | if lt blen 24 |
| 796 | ret EST_ERR_ENCODING |
| 797 | end |
| 798 | n = est_get_u32 buf 8 |
| 799 | m = est_get_u32 buf 12 |
| 800 | if or not est_in_dim n not est_in_dim m |
| 801 | ret EST_ERR_DIM |
| 802 | end |
| 803 | if or ne est_get_u32 buf 16 EST_ESTIMATOR_LINEAR_KALMAN ne est_get_u32 buf 20 EST_UNCERTAINTY_GAUSSIAN |
| 804 | ret EST_ERR_UNIT |
| 805 | end |
| 806 | pos = add mul 2 mul n n add mul m n mul m m |
| 807 | if ne blen add 32 add mul 4 add n m mul 8 pos |
| 808 | ret EST_ERR_ENCODING |
| 809 | end |
| 810 | t = new ulong 280 0 |
| 811 | put t EM_N n |
| 812 | put t EM_M m |
| 813 | put t EM_EST EST_ESTIMATOR_LINEAR_KALMAN |
| 814 | put t EM_MEAN EST_UNCERTAINTY_GAUSSIAN |
| 815 | pos = 24 |
| 816 | for i ulong n |
| 817 | u = est_get_u32 buf pos |
| 818 | pos = add pos 4 |
| 819 | if ge u EST_UNIT_MAX |
| 820 | ret EST_ERR_UNIT |
| 821 | end |
| 822 | put t add EM_SUNIT i u |
| 823 | end |
| 824 | for i ulong m |
| 825 | u = est_get_u32 buf pos |
| 826 | pos = add pos 4 |
| 827 | if ge u EST_UNIT_MAX |
| 828 | ret EST_ERR_UNIT |
| 829 | end |
| 830 | put t add EM_OUNIT i u |
| 831 | end |
| 832 | pos = est_get_f64_arr buf pos t add shl EM_F 16 mul n n |
| 833 | pos = est_get_f64_arr buf pos t add shl EM_Q 16 mul n n |
| 834 | pos = est_get_f64_arr buf pos t add shl EM_H 16 mul m n |
| 835 | pos = est_get_f64_arr buf pos t add shl EM_R 16 mul m m |
| 836 | put t EM_STEP est_get_u64 buf pos |
| 837 | st = est_check_model view t |
| 838 | if ne st EST_OK |
| 839 | ret st |
| 840 | end |
| 841 | est_rec_copy view t dst EST_MODEL_LEN |
| 842 | ret EST_OK |
| 843 | end |
| 844 | |
| 845 | fn est_decode_observation buf view uchar * blen ulong dst edit ulong 96 to long |
| 846 | st = est_check_hdr buf blen EST_KIND_OBSERVATION |
| 847 | if ne st EST_OK |
| 848 | ret st |
| 849 | end |
| 850 | if lt blen 12 |
| 851 | ret EST_ERR_ENCODING |
| 852 | end |
| 853 | m = est_get_u32 buf 8 |
| 854 | if not est_in_dim m |
| 855 | ret EST_ERR_DIM |
| 856 | end |
| 857 | need = add 92 add mul 4 m mul 8 add m mul m m |
| 858 | if ne blen need |
| 859 | ret EST_ERR_ENCODING |
| 860 | end |
| 861 | t = new ulong 96 0 |
| 862 | put t EO_M m |
| 863 | pos = ulong 12 |
| 864 | for i ulong m |
| 865 | u = est_get_u32 buf pos |
| 866 | pos = add pos 4 |
| 867 | if ge u EST_UNIT_MAX |
| 868 | ret EST_ERR_UNIT |
| 869 | end |
| 870 | put t add EO_UNIT i u |
| 871 | end |
| 872 | for i ulong m |
| 873 | put t add EO_Z i est_get_u64 buf pos |
| 874 | pos = add pos 8 |
| 875 | end |
| 876 | for i ulong mul m m |
| 877 | put t add EO_R i est_get_u64 buf pos |
| 878 | pos = add pos 8 |
| 879 | end |
| 880 | put t EO_TNS est_get_u64 buf pos |
| 881 | pos = add pos 8 |
| 882 | put t EO_SEQ est_get_u64 buf pos |
| 883 | pos = add pos 8 |
| 884 | pos = est_get_rec_dig buf pos t EO_SRC |
| 885 | pos = est_get_rec_dig buf pos t EO_EV |
| 886 | st = est_check_observation view t |
| 887 | if ne st EST_OK |
| 888 | ret st |
| 889 | end |
| 890 | est_rec_copy view t dst EST_OBS_LEN |
| 891 | ret EST_OK |
| 892 | end |
| 893 | |
| 894 | fn est_decode_belief buf view uchar * blen ulong dst edit ulong 96 to long |
| 895 | st = est_check_hdr buf blen EST_KIND_BELIEF |
| 896 | if ne st EST_OK |
| 897 | ret st |
| 898 | end |
| 899 | if lt blen 12 |
| 900 | ret EST_ERR_ENCODING |
| 901 | end |
| 902 | n = est_get_u32 buf 8 |
| 903 | if not est_in_dim n |
| 904 | ret EST_ERR_DIM |
| 905 | end |
| 906 | need = add 124 add mul 4 n mul 8 add n mul n n |
| 907 | if ne blen need |
| 908 | ret EST_ERR_ENCODING |
| 909 | end |
| 910 | t = new ulong 96 0 |
| 911 | put t EB_N n |
| 912 | pos = ulong 12 |
| 913 | for i ulong n |
| 914 | u = est_get_u32 buf pos |
| 915 | pos = add pos 4 |
| 916 | if ge u EST_UNIT_MAX |
| 917 | ret EST_ERR_UNIT |
| 918 | end |
| 919 | put t add EB_UNIT i u |
| 920 | end |
| 921 | for i ulong n |
| 922 | put t add EB_X i est_get_u64 buf pos |
| 923 | pos = add pos 8 |
| 924 | end |
| 925 | for i ulong mul n n |
| 926 | put t add EB_P i est_get_u64 buf pos |
| 927 | pos = add pos 8 |
| 928 | end |
| 929 | put t EB_GEN est_get_u64 buf pos |
| 930 | pos = add pos 8 |
| 931 | put t EB_TNS est_get_u64 buf pos |
| 932 | pos = add pos 8 |
| 933 | pos = est_get_rec_dig buf pos t EB_MDL |
| 934 | pos = est_get_rec_dig buf pos t EB_PAR |
| 935 | pos = est_get_rec_dig buf pos t EB_ROOT |
| 936 | st = est_check_belief view t |
| 937 | if ne st EST_OK |
| 938 | ret st |
| 939 | end |
| 940 | est_rec_copy view t dst EST_BELIEF_LEN |
| 941 | ret EST_OK |
| 942 | end |
| 943 | |
| 944 | fn est_decode_prediction buf view uchar * blen ulong dst edit ulong 176 to long |
| 945 | st = est_check_hdr buf blen EST_KIND_PREDICTION |
| 946 | if ne st EST_OK |
| 947 | ret st |
| 948 | end |
| 949 | if lt blen 100 |
| 950 | ret EST_ERR_ENCODING |
| 951 | end |
| 952 | n = est_get_u32 buf 92 |
| 953 | m = est_get_u32 buf 96 |
| 954 | if or not est_in_dim n not est_in_dim m |
| 955 | ret EST_ERR_DIM |
| 956 | end |
| 957 | pos = add 100 mul 8 add add n mul n n add m mul m m |
| 958 | if lt blen add pos 4 |
| 959 | ret EST_ERR_ENCODING |
| 960 | end |
| 961 | hc = est_get_u32 buf pos |
| 962 | if gt hc 1 |
| 963 | ret EST_ERR_ENCODING |
| 964 | end |
| 965 | pos = add pos 4 |
| 966 | if eq hc 1 |
| 967 | pos = add pos mul 8 n |
| 968 | end |
| 969 | if ne blen pos |
| 970 | ret EST_ERR_ENCODING |
| 971 | end |
| 972 | t = new ulong 176 0 |
| 973 | pos = est_get_rec_dig buf 8 t EP_PRIOR |
| 974 | pos = est_get_rec_dig buf pos t EP_MDL |
| 975 | put t EP_HZ est_get_u32 buf 72 |
| 976 | put t EP_GEN est_get_u64 buf 76 |
| 977 | put t EP_TNS est_get_u64 buf 84 |
| 978 | put t EP_N n |
| 979 | put t EP_M m |
| 980 | pos = est_get_f64_arr buf 100 t add shl EP_X 16 n |
| 981 | pos = est_get_f64_arr buf pos t add shl EP_P 16 mul n n |
| 982 | pos = est_get_f64_arr buf pos t add shl EP_YM 16 m |
| 983 | pos = est_get_f64_arr buf pos t add shl EP_S 16 mul m m |
| 984 | put t EP_HC hc |
| 985 | pos = add pos 4 |
| 986 | if eq hc 1 |
| 987 | pos = est_get_f64_arr buf pos t add shl EP_BU 16 n |
| 988 | end |
| 989 | st = est_check_prediction view t |
| 990 | if ne st EST_OK |
| 991 | ret st |
| 992 | end |
| 993 | est_rec_copy view t dst EST_PRED_LEN |
| 994 | ret EST_OK |
| 995 | end |
| 996 | |
| 997 | fn est_decode_innovation buf view uchar * blen ulong dst edit ulong 96 to long |
| 998 | st = est_check_hdr buf blen EST_KIND_INNOVATION |
| 999 | if ne st EST_OK |
| 1000 | ret st |
| 1001 | end |
| 1002 | if lt blen 108 |
| 1003 | ret EST_ERR_ENCODING |
| 1004 | end |
| 1005 | m = est_get_u32 buf 104 |
| 1006 | if not est_in_dim m |
| 1007 | ret EST_ERR_DIM |
| 1008 | end |
| 1009 | need = add 124 mul 8 add m mul m m |
| 1010 | if ne blen need |
| 1011 | ret EST_ERR_ENCODING |
| 1012 | end |
| 1013 | t = new ulong 96 0 |
| 1014 | pos = est_get_rec_dig buf 8 t EI_PRED |
| 1015 | pos = est_get_rec_dig buf pos t EI_OBS |
| 1016 | pos = est_get_rec_dig buf pos t EI_MDL |
| 1017 | put t EI_M m |
| 1018 | pos = ulong 108 |
| 1019 | for i ulong m |
| 1020 | put t add EI_NU i est_get_u64 buf pos |
| 1021 | pos = add pos 8 |
| 1022 | end |
| 1023 | for i ulong mul m m |
| 1024 | put t add EI_S i est_get_u64 buf pos |
| 1025 | pos = add pos 8 |
| 1026 | end |
| 1027 | put t EI_NIS est_get_u64 buf pos |
| 1028 | pos = add pos 8 |
| 1029 | put t EI_TNS est_get_u64 buf pos |
| 1030 | st = est_check_innovation view t |
| 1031 | if ne st EST_OK |
| 1032 | ret st |
| 1033 | end |
| 1034 | est_rec_copy view t dst EST_INNOV_LEN |
| 1035 | ret EST_OK |
| 1036 | end |
| 1037 | |
| 1038 | note ---- Digests ---- |
| 1039 | |
| 1040 | fn est_digest_model o view ulong 280 dst edit uchar 32 to long |
| 1041 | buf = new uchar 2304 0 |
| 1042 | elen = new ulong 1 0 |
| 1043 | st = est_encode_model o buf elen |
| 1044 | if ne st EST_OK |
| 1045 | ret st |
| 1046 | end |
| 1047 | est_sha256_domain EST_DOM_MODEL view buf get elen 0 dst |
| 1048 | ret EST_OK |
| 1049 | end |
| 1050 | |
| 1051 | fn est_digest_observation o view ulong 96 dst edit uchar 32 to long |
| 1052 | buf = new uchar 2304 0 |
| 1053 | elen = new ulong 1 0 |
| 1054 | st = est_encode_observation o buf elen |
| 1055 | if ne st EST_OK |
| 1056 | ret st |
| 1057 | end |
| 1058 | est_sha256_domain EST_DOM_OBS view buf get elen 0 dst |
| 1059 | ret EST_OK |
| 1060 | end |
| 1061 | |
| 1062 | fn est_digest_belief o view ulong 96 dst edit uchar 32 to long |
| 1063 | buf = new uchar 2304 0 |
| 1064 | elen = new ulong 1 0 |
| 1065 | st = est_encode_belief o buf elen |
| 1066 | if ne st EST_OK |
| 1067 | ret st |
| 1068 | end |
| 1069 | est_sha256_domain EST_DOM_BELIEF view buf get elen 0 dst |
| 1070 | ret EST_OK |
| 1071 | end |
| 1072 | |
| 1073 | fn est_digest_prediction o view ulong 176 dst edit uchar 32 to long |
| 1074 | buf = new uchar 2304 0 |
| 1075 | elen = new ulong 1 0 |
| 1076 | st = est_encode_prediction o buf elen |
| 1077 | if ne st EST_OK |
| 1078 | ret st |
| 1079 | end |
| 1080 | est_sha256_domain EST_DOM_PRED view buf get elen 0 dst |
| 1081 | ret EST_OK |
| 1082 | end |
| 1083 | |
| 1084 | fn est_digest_innovation o view ulong 96 dst edit uchar 32 to long |
| 1085 | buf = new uchar 2304 0 |
| 1086 | elen = new ulong 1 0 |
| 1087 | st = est_encode_innovation o buf elen |
| 1088 | if ne st EST_OK |
| 1089 | ret st |
| 1090 | end |
| 1091 | est_sha256_domain EST_DOM_INNOV view buf get elen 0 dst |
| 1092 | ret EST_OK |
| 1093 | end |
| 1094 | |
| 1095 | fn est_evidence_root_extend root view uchar 32 obs view uchar 32 dst edit uchar 32 to long |
| 1096 | tmp = new uchar 64 0 |
| 1097 | for i ulong 32 |
| 1098 | put tmp i get root i |
| 1099 | put tmp add 32 i get obs i |
| 1100 | end |
| 1101 | est_sha256_domain EST_DOM_ROOT view tmp 64 dst |
| 1102 | ret EST_OK |
| 1103 | end |