code wiki / (root) / nx_root.nx

nx_root.nx

buildroot/runtime/nx_root.nx

8278 B238 linesdepth 4pulls 4 transitivereach 27 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_root.nx -- Newton-Raphson nth-root for i64 + Q14 fixed point. Bits-up math primitive sibling of nx_isqrt.nx. Computes floor of the integer nth-root and the Q14-scaled fractional nth-root via the same iteration shape. Unblocks two queued upgrades: * nx_sampler.nx Karras schedule (Karras et al. 2022 EDM): needs (sigma_min / sigma_max)^(1/rho) with rho = 7. * nx_ms_ssim.nx Wang weighted geometric mean (Wang et al. 2003): needs fractional alpha^a * c^b * s^g powers per scale. ===== Math ======================================================= Newton-Raphson iteration for x^n = T, x > 0: x_{k+1} = ((n-1) * x_k + T / x_k^(n-1)) / n Starting from x_0 = max(T, 1) (an upper bound), each iteration strictly DECREASES x until convergence at floor(T^(1/n)). Quadratic convergence near the root: O(log log n) iterations after the initial linear phase, O(log n) total. Integer convergence test: terminate when x_{k+1} >= x_k. ===== Q14 wrapper =============================================== For fractional nth-root with target represented in Q14: nth_root_q14(target_q14, n) ≈ target^(1/n) in Q14 We iterate directly in Q14 using a Q14-aware power helper `nx_pow_q14` that does (x_q14 * x_q14 / Q14) chain. This keeps intermediate values bounded near the target magnitude rather than overflowing via a target * Q14^(n-1) scale-up. genealogy_id: heron_alexandria_iterative_root + newton_raphson_1671 + wikipedia_nth_root_algorithm lineage_id: substrate_root_v1

dependencies 3 imports · 2 importers

nx_syscalls.nx nx_tier.nx nx_loop.nx nx_root.nx nx_rope.nx nx_sampler.nx

imports: nx_syscalls.nxnx_tier.nxnx_loop.nx

imported by: nx_rope.nxnx_sampler.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nx_int_nth_root nx_root_ipow nx_root_ipow ↻ nx_nth_root_q14 nx_pow_q14 nx_root_verdict_is_valid

structs

none

consts

52const NX_ROOT_Q14_ONE: nx_int = 16384 // 1 << 14
53const NX_ROOT_MAX_ITERS: nx_int = 64 // Newton converges in <30 for i64 range
57const NX_ROOT_OK: nx_int = 0
58const NX_ROOT_ERR_NEG_TARGET: nx_int = 1
59const NX_ROOT_ERR_BAD_N: nx_int = 2
60const NX_ROOT_N_VERDICTS: nx_int = 3

functions

62func nx_root_verdict_is_valid(v: nx_int) -> nx_int
called by 1: main
73func nx_root_ipow(base: nx_int, exp: nx_int) -> nx_int
called by 2: nx_int_nth_rootmain
98func nx_int_nth_root(target: nx_int, n: nx_int) -> nx_int
called by 1: main calls 1: nx_root_ipow
136func nx_pow_q14(x_q14: nx_int, exp: nx_int) -> nx_int
148func nx_nth_root_q14(target_q14: nx_int, n: nx_int) -> nx_int
193func main() -> i64