File size: 2,528 Bytes
767a051 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """Small deterministic knowledge operators used by the included source-derived demo."""
from __future__ import annotations
import math
from dataclasses import dataclass
from typing import Any
def active_bit_positions(value: int) -> list[int]:
if value < 0:
raise ValueError("active-bit projection expects a non-negative integer")
return [k for k in range(value.bit_length()) if (value >> k) & 1]
def popcount(value: int) -> int:
return len(active_bit_positions(value))
def regular_pulse_geometry(count: int, radius: float = 1.0, phase: float = 0.0) -> dict[str, Any]:
if count < 0:
raise ValueError("pulse count must be non-negative")
if count == 0:
return {"kind": "empty", "points": []}
if count == 1:
return {"kind": "point", "points": [[radius * math.cos(phase), radius * math.sin(phase)]]}
if count == 2:
return {
"kind": "segment",
"points": [[radius * math.cos(phase), radius * math.sin(phase)],
[-radius * math.cos(phase), -radius * math.sin(phase)]],
}
points = [
[radius * math.cos(phase + 2.0 * math.pi * j / count),
radius * math.sin(phase + 2.0 * math.pi * j / count)]
for j in range(count)
]
return {"kind": "triangle" if count == 3 else "regular_polygon", "points": points}
@dataclass(slots=True)
class KnowledgeResult:
output: dict[str, Any]
trace: list[dict[str, Any]]
def nineteen_demo() -> KnowledgeResult:
value = 19
segments = ["ne", "gen", "tien"]
active = active_bit_positions(value)
trace: list[dict[str, Any]] = [
{"operator": "TOM.ACTIVE", "input": value, "output": active},
{"operator": "TOM.POPCOUNT", "input": active, "output": len(active)},
{"operator": "TOM.PULSE", "input": segments, "output": len(segments)},
]
equal = len(active) == len(segments)
trace.append({"operator": "TOM.EQ", "left": len(active), "right": len(segments), "output": equal})
geometry = regular_pulse_geometry(len(segments))
trace.append({"operator": "TOM.PROJECT", "input": len(segments), "output": geometry["kind"]})
output = {
"value": value,
"binary": format(value, "b"),
"active_bit_positions": active,
"active_bit_count": len(active),
"profile_segments": segments,
"pulse_count": len(segments),
"equal_feature_counts": equal,
"projection": geometry,
}
return KnowledgeResult(output=output, trace=trace)
|