Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

dyadic — Six-Axiom 2-Adic Operator Calculus

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition and reversion. Umbrella header dyadic.h includes all sub-headers from include/dyadic/.

#include<dyadic.h>
#include<cstdio>usingnamespacedyadic;intmain() {
Polynomial<4, uint64_t> p{{1, 2, 3, 4}};
// Formal derivative: D(P)(t) = dP/dtauto dp = formal_derivative(p); // {2, 6, 12, 0}for (auto c : dp) std::printf("%lu ", c);
// Forward difference: Δ(P)(t) = P(t+1) − P(t)auto fp = forward_difference(p); // {9, 18, 12, 0}// Basis conversions (exact roundtrip)auto ff = change_basis<FallingFactorialBasis>(p);
auto back = change_basis<MonomialBasis>(ff); // == p// Evaluate P(5) = 1 + 2·5 + 3·25 + 4·125 = 586auto y = p.eval(5);
// Witt vector ring operations
WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b; // Witt addition (via ghost map)auto prod = a * b; // Witt multiplication// Power series reversion (Lagrange inversion)
Polynomial<6, uint64_t> P{{0, 1, 1}}; // t + t²auto Q = reversion(P);
// Q = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored as unsigned ℤ₂ values: −1 ≡ 2⁶⁴−1, etc.)
}

Requirements

  • C++20 compiler (GCC 12+, Clang 17+, MSVC 2022+)
  • No external dependencies
  • detail::uint128_t (software 128-bit pair) provides uint64_t word support without unsigned __int128
  • Clang needs -fconstexpr-steps=50000000 for compile-time proofs; GCC needs -fconstexpr-ops-limit=200000000

Features

AreaWhat
2-Adic Primitivesv2 (valuation), modinv_odd, div_2k_adic, artin_schreier (℘(x)=x²−x)
PolynomialsPolynomial<N,W,Basis> with eval, +, , *, basis conversion (Monomial / FallingFactorial / Taylor), GCD, resultant, discriminant, pseudo-remainder, divmod, square-free check
Calculusformal_derivative (D), forward_difference (Δ), taylor_shift, indefinite_sum (Σ = Δ⁻¹)
Witt VectorsWittVector<N,W> with ghost map, Frobenius, Verschiebung, +, *, exp, log, inverse, adams_operation, teichmueller_lift
Carry ChainFull-width carry propagation C = (I−N)⁻¹ — converges in one pass
Compose / ReversionPower series composition P(Q(t)) and Lagrange inversion
Compile-Time Proofs24 named static_assert proofs (~55 total assertions) verifying ring axioms, basis roundtrips, D∘Δ=Δ∘D, ghost homomorphism, carry idempotence, Witt exp/log roundtrip (see dyadic/verify.h)
Combinatorialbinom, stirling_2, stirling_1, stirling_1_unsigned — all constexpr, cached at compile time

More Examples

Witt vector ring

WittVector<3, uint64_t> a{{1, 2, 3}}, b{{4, 5, 6}};
auto sum = a + b;
auto prod = a * b;
// Frobenius and Verschiebungauto fa = a.frobenius();
auto vb = b.verschiebung();
// F(V(a)) == V(F(a)) — verified at compile time// Adams operation ψⁿauto psi = adams_operation(a, 3);
// Teichmüller lift: τ(x) = (x, 0, 0, …) (N is required)auto tau = teichmueller_lift<4>(uint64_t{7});
// ghost_j(τ(ab)) = ghost_j(τ(a)) · ghost_j(τ(b)) — verified at compile time

Polynomial GCD, divmod, and ring semantics

// NOTE: operator* uses carry-chain (2-adic ring).// poly_divmod_cw / poly_gcd_cw use coefficient-wise (standard ring).// Use poly_mul_cw() and verify_divmod_cw() for verification.
Polynomial<3, uint64_t> A{{1, 2, 1}}; // (x+1)²
Polynomial<2, uint64_t> B{{1, 1}}; // (x+1)auto [Q, R] = poly_divmod_cw(A, B); // Q=(x+1), R=0bool ok = verify_divmod_cw(A, Q, B, R); // true: A=Q·B+R ✓auto G = poly_gcd_cw(A, B); // G=(x+1) — divides both

Polynomial composition and reversion

Polynomial<6, uint64_t> P{{0, 1, 1}}; // P(t) = t + t²
Polynomial<6, uint64_t> Q{{0, 1, 2}}; // Q(t) = t + 2t²autoPQ = compose(P, Q); // P(Q(t)) — degree (N-1)*(M-1)+1 = 26auto R = reversion(P); // Lagrange inverse: P(R(t)) = t// R(t) = t − t² + 2t³ − 5t⁴ + 14t⁵ − 42t⁶ + …// (stored in ℤ₂: negative coefficients wrap modulo 2^W)

Compile-time verification

#include<dyadic/verify.h>// triggers all static_asserts at compile time// If it compiles, all 22 named static_assert proofs passed.

Day-to-day compiles include a sampled subset. For the full exhaustive suite (256² cases, 8K+ multiplication cases), define DYADIC_HEAVY_PROOFS.

Precision window checkers

Polynomial<6, uint8_t> p{{0, 0, 0, 0, 0, 255}};
if (!check_taylor_roundtrip_precision(p))
// T₅ = 5! · 255 = 30600 > 256 — high bits lost
WittVector<4, uint32_t> w{{1, 2, 3, 4}};
if (!check_witt_recovery_precision(w))
// Some rⱼ ≥ 2³²⁻ʲ — ghost recovery inexact

Documentation

FileWhat
docs/precision.mdFive precision windows unified under a single principle
docs/theory.mdFour-domain unified theory: operator calculus ↔ Witt ghosts ↔ Mersenne ghosts ↔ thermodynamic classification

Build & Integrate

As a header collection — copy dyadic.h and the include/dyadic/ directory into your project's include path, then #include <dyadic.h> (umbrella) or #include <dyadic/core.h> (individual header).

With CMake:

cmake -B build -DDYADIC_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build

Optional: -DDYADIC_HEAVY_PROOFS=ON for exhaustive compile-time proofs.

Via CI script:

./ci_compile_check.sh

Tests

FileWhat
test_core.cppCore axiom unit tests: six axioms, carry chain, arithmetic, evaluation
test_verify.cpp24 named compile-time proofs (~55 assertions) + runtime verification across 9 (N,W) combos
test_property.cppRandomized property-based tests: 18 invariants × 10 (N,W) combos
test_full.cpp17 functional test groups covering the entire API surface
test_negatives.cppFork-based assertion verification (6 precondition checks)
benchmark.cppRuntime benchmarks for key operations (build manually: g++ -O2 -std=c++20 -I. -Iinclude test/benchmark.cpp)

All tests pass under GCC 14+ and Clang 17+ with ASan+UBSan. CI covers GCC (light + heavy proofs), Clang, and MSVC on Windows.

Design Notes

  • Two ring semantics: operator* uses carry-chain poly_mul (correct ℤ₂ arithmetic with overflow propagation). Functions with _cw suffix (poly_divmod_cw, poly_gcd_cw, poly_mul_cw) operate coefficient-wise (standard ring). These are different rings — use poly_mul_cw() and verify_divmod_cw() to verify divmod/gcd results.
  • quad_width<W> alias: Shorthand for widen_t<dword_t<W>> (up to 4× word width), used by ghost-map accumulation and unsaturated polynomial products.
  • assert preconditions: All silent-failure points (poly_divmod_cw with zero/even divisor, witt_exp with v₂(a₀) < 2, witt_log/witt_inverse with even a₀, reversion with even P[1]) now use assert(). Invalid inputs trigger SIGABRT in debug builds. A few functions return 0 for invalid inputs by design rather than asserting: poly_discriminant_cw returns 0 when degree < 1 or leading coefficient is even; det_laplace returns 0 for dim > 6 (use Bareiss via determinant_cw instead).
  • Compile-time cached Stirling/Pascal tables: detail::STIRLING_CACHE<N,W> and detail::PASCAL_CACHE<N,W> are inline constexpr globals — computed once per (N,W) at compile time, shared across all basis conversion and difference calls.
  • Auto-vectorization hints: poly_mul_unsaturated and poly_mul use DYADIC_RESTRICT (__restrict__) and #pragma GCC ivdep on their inner multiply-accumulate loops, enabling the compiler to generate SIMD code for runtime invocations without breaking constexpr.

Complexity

See docs/complexity.md for a complete function-by-function breakdown.

OperationTimeSpaceNotes
v2, modinv_odd, exact_divide, div_2k_adicO(1)O(1)Single-instruction or fixed-iteration 2-adic primitives
eval (Monomial)O(N)O(1)Horner's method
eval (FF / Taylor)O(N)O(1)Incremental falling product / binomial coefficient
formal_derivative (Monomial)O(N)O(N)Direct coefficient scaling
formal_derivative (FF / Taylor)O(N²)O(N)Convert → monomial D → convert back
forward_difference (FF / Taylor)O(N)O(N)FF diagonalizes Δ: scale+shift; Taylor: pure shift
forward_difference (Monomial)O(N²)O(N)Pascal-cache binomial transform
taylor_shiftO(N²)O(N)Pascal-cache / falling-factorial binomial transform
indefinite_sum (FF)O(N)O(N)Σ(FFₙ₋₁) = FFₙ / n
indefinite_sum (Monomial / Taylor)O(N²)O(N)Converts to FF, sums, converts back
poly_exp / poly_logO(N²)O(N)Double-loop recurrence with dword intermediates
poly_mul / operator*O(N·M)O(1) chunkedNaive convolution + carry chain; chunked 256-bit buffer; unsigned __int128 for uint64_t (~2.7× speedup deg 63)
mul_unsignedO(N·M)O(1) chunkedBig-integer unsigned multiply (N+M limbs); same chunked unsigned __int128 pattern as poly_mul
poly_mul_cwO(N·M)O(N+M)Standard coefficient-wise convolution
composeO(N²·M²)O(N·M)Naive power-series accumulation; result ≤ 4095 coeffs
reversionO(N³)O(N²)Incremental Lagrange inversion (not Newton)
change_basisO(N²)O(N)Stirling-cache triangular matrix multiply; 1–2 conversions per call
witt_add / witt_mulO(N²)O(N²)Ghost-map + Newton recovery; quad_width accumulators
witt_log / witt_expO(N² + N·T)O(N²)T ≈ 2·bit_width (ghost p-adic series terms)
witt_inverseO(N²)O(N²)Ghost inverse via modinv_odd per component
adams_operationO(N² + N·n)O(N²)Ghost ^ n powering
poly_divmod_cwO(N·(N+M))O(max(N,M))Long division (requires odd lc)
pseudo_remainder_cwO(N·(N+M))O(N)Subresultant PRS (no lc requirement)
poly_gcd_cwO(K³) worstO(K)Euclidean PRS; K = max(N,M)
polynomial_resultant_cwO(1) boundedO(1)Sylvester matrix (dim ≤ 6) + Laplace det (≤ 6! = 720)
poly_discriminant_cwO(N) + O(1)O(N)Derivative + resultant
poly_is_square_free_cwO(N³)O(N)gcd(P, P′)
Matrix::operator* (M×N × N×P)O(M·N·P)O(M·P)i-k-j loop with zero-skip
Matrix::determinantO(M³)O(M·N)Bareiss fraction-free elimination
Matrix::inverse / solve / rrefO(M³)O(M²)Gauss–Jordan with odd-pivot modinv_odd
pade_approximant [M/N]O(N³ + M·N)O(N² + M+N)Gaussian elimination on N×N Toeplitz system
cf_convergentO(n²)O(n)Classical recurrence; degree grows linearly
div_unsignedO(NL·NR)O(NL+NR)Knuth Algorithm D (NR ≥ 2) or divmod_single (NR=1)
reciprocal_newtonO(NR²·log NR)O(NR)Newton iteration; ceil(log₂((NR+1)·W)) iterations
div_newtonO(NR²·log NR + NL·NR)O(NL+NR)Newton reciprocal division with correction step
binom / stirling_2 / stirling_1O(k·log n) / O(n²)O(1) / O(n)GCD-multiplicative / DP recurrence
StirlingCache / PascalCache ctorO(N²) compile-timeO(N²)inline constexpr cached once per (N,W)

All operations are constexpr; runtime performance matches compile-time complexity bounds. The carry-chain poly_mul and mul_unsigned are auto-vectorized with SIMD hints (#pragma GCC ivdep, DYADIC_RESTRICT) and use hardware unsigned __int128 accumulation for uint64_t where available.

Known Limitations

  • Polynomial::degree() renamed to max_degree(): The old name was misleading (it returned N−1, the maximum possible degree, not the actual degree). Added actual_degree() member function.
  • Taylor basis roundtrip: T_k = k! · FF_k wraps when FF_k ≥ 2^W / k!. Use small coefficients for exact roundtrips. FallingFactorial basis has no such limitation.
  • Witt precision window: Recovery r_j = (G_j − S_j) / 2^j requires r_j < 2^{W−j}.
  • Witt exp/log term truncation: Uses valuation-aware dynamic term counting with 2× bit-width budget. Requires v₂(x) ≥ 2 for exp convergence (mathematical limit — v₂(x) = 1 stalls at ≤ log₂(n)+1). Log converges for v₂(y) ≥ 1 (~135 terms at 128-bit).
  • detail::uint128_t is a software 128-bit pair — no unsigned __int128 required. __int128 is used as a hot-path optimization in binom() (dyadic/combinatorial.h), poly_mul() and mul_unsigned() (dyadic/core.h/arith.h, ~2.7× speedup at deg=63 for uint64_t), div_unsigned_knuth() (dyadic/arith.h, hardware trial-division / multiply-subtract for ~3-4× speedup), and the p-adic series in witt_log/witt_exp (dyadic/witt.h, hardware multiply-accumulate). All paths guarded by __SIZEOF_INT128__.

License

This project is made available under the terms of the MIT License.

About

Header-only C++20 library for arithmetic in ℤ₂[[t]]: carry chains, formal derivatives, forward differences, Witt vectors, basis conversions, power series composition/reversion — with compile-time verified proofs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages