| """Additional demo utilities module.
|
|
|
| This module is a self-contained collection of deterministic math and
|
| utility functions used by the demo tests. It's intentionally pure Python
|
| and dependency-free.
|
| """
|
|
|
| from __future__ import annotations
|
|
|
| import math
|
| from typing import List, Tuple
|
|
|
|
|
| def sq(x: float) -> float:
|
| return x * x
|
|
|
|
|
| def cube(x: float) -> float:
|
| return x * x * x
|
|
|
|
|
| def clamp(x: float, lo: float, hi: float) -> float:
|
| if x < lo:
|
| return lo
|
| if x > hi:
|
| return hi
|
| return x
|
|
|
|
|
| def lerp(a: float, b: float, t: float) -> float:
|
| return a + (b - a) * t
|
|
|
|
|
| def frange(n: int) -> List[float]:
|
| return [float(i) for i in range(n)]
|
|
|
|
|
| def pairwise_sums(xs: List[float]) -> List[float]:
|
| return [xs[i] + xs[i + 1] for i in range(len(xs) - 1)]
|
|
|
|
|
|
|
|
|
| _FUNCS = {}
|
|
|
|
|
| def _make_fn(i: int):
|
| def f(x: float) -> float:
|
|
|
| return clamp((x + i * 0.001) * math.cos(i * 0.01), -1e6, 1e6)
|
|
|
| f.__name__ = f"demo_fn_{i}"
|
| return f
|
|
|
|
|
| for i in range(1, 801):
|
| _FUNCS[f"demo_fn_{i}"] = _make_fn(i)
|
|
|
|
|
| def apply_all(x: float) -> List[float]:
|
| return [fn(x) for fn in _FUNCS.values()]
|
|
|
|
|
| def accumulate(xs: List[float]) -> List[float]:
|
| out = []
|
| s = 0.0
|
| for v in xs:
|
| s += v
|
| out.append(s)
|
| return out
|
|
|
|
|
| def sliding_max(xs: List[float], k: int) -> List[float]:
|
| if k <= 0:
|
| return []
|
| out = []
|
| for i in range(len(xs) - k + 1):
|
| out.append(max(xs[i : i + k]))
|
| return out
|
|
|
|
|
| def demo_extra_run() -> Tuple[int, float]:
|
| xs = frange(50)
|
| vals = apply_all(0.5)
|
| acc = accumulate(vals[:50])
|
| s = sum(acc)
|
| return (len(acc), s)
|
|
|
|
|
| if __name__ == "__main__":
|
| print("demo_extra:", demo_extra_run())
|
|
|