Skip to content

Conversation

@sigilante
Copy link
Collaborator

Prototype implementation demonstrating jet-compatible approach for transcendental functions (exp, sin, cos, tan) using:

  • Fixed polynomial coefficients from musl libc
  • Deterministic argument reduction
  • Horner's method evaluation

Key principle: every FP operation happens in same order with same intermediate values in both Hoon and C jet.

Prototype implementation demonstrating jet-compatible approach for
transcendental functions (exp, sin, cos, tan) using:
- Fixed polynomial coefficients from musl libc
- Deterministic argument reduction
- Horner's method evaluation

Key principle: every FP operation happens in same order with same
intermediate values in both Hoon and C jet.
Complete fixed-polynomial math library with:
- Half precision (@rh): degree 5 sin, degree 4 cos, degree 3 exp
- Single precision (@rs): degree 9 sin, degree 10 cos, degree 4 exp
- Double precision (@rd): degree 13 sin, degree 14 cos, degree 5 exp

All coefficients from musl libc expressed as hex literals for
bit-exact jet compatibility. Each precision level includes:
- Polynomial coefficients and reduction constants
- Chebyshev polynomial generator (+cheb)
- Kernel functions (sindf, cosdf)
- Public API (sin, cos, tan, exp)
- Helper functions (scalbn, floor-int, rem-pio2)
Complete binary128 (128-bit) implementation with:
- Degree 17 sin polynomial (S1-S8)
- Degree 16 cos polynomial (C1-C8)
- Degree 7 exp polynomial (P1-P7)
- Chebyshev polynomial generator (+cheb)
- Helper functions (scalbn, floor-int, rem-pio2)

Coefficients derived from FreeBSD ld128/k_sinl.c, k_cosl.c.
Uses .~~~N syntax for quad precision literals.
Extended math-chebyshev.hoon with:
- log: Fixed polynomial (Lg1-Lg4 for @rs, Lg1-Lg7 for @rd)
- sqrt: Newton-Raphson with fixed iterations (4 for @rs, 6 for @rd)
- atan: Fixed polynomial with range reduction
- asin, acos, atan2: Derived from atan
- pow-n: Binary exponentiation (fixed 32/64 iterations)
- pow: exp(n*log(x)) for non-integer exponents
- log-2, log-10: Derived from log

Added test suite with:
- Individual function tests for @rs and @rd
- Identity tests (sin^2+cos^2=1, exp(log(x))=x, etc.)
- Chebyshev polynomial verification
Half precision (@rh):
- log: Simplified polynomial (single Lg coefficient)
- sqrt: Newton-Raphson with 3 iterations
- atan: Reduced polynomial with large-argument reduction
- asin, acos, atan2, pow-n, pow

Quad precision (@rq):
- log: 8-coefficient polynomial (Lg1-Lg8) for 113-bit accuracy
- sqrt: Newton-Raphson with 8 iterations
- atan: 6-coefficient polynomial with range reduction
- asin, acos, atan2, pow-n, pow

All functions use hex-exact coefficients for jet compatibility.
Adds comprehensive tests for half precision (@rh) and quad precision (@rq):

- Helper functions: expect-near-rh, expect-near-rq
- Function tests: sin, cos, exp, log, sqrt, atan, pow-n, cheb
- Identity tests: sin²+cos²=1, exp(log(x))=x, sqrt(x)²=x

Half precision tests use hex literals (0x3c00 = 1.0, etc.)
Quad precision tests use .~~~X literal format
Hyperbolic functions added to all precision levels (@rh, @rs, @rd, @rq):
- sinh: Hyperbolic sine via (exp(x) - exp(-x)) / 2
- cosh: Hyperbolic cosine via (exp(x) + exp(-x)) / 2
- tanh: Hyperbolic tangent via (e^2x - 1) / (e^2x + 1)
- asinh: Inverse hyperbolic sine via ln(x + sqrt(x^2 + 1))
- acosh: Inverse hyperbolic cosine via ln(x + sqrt(x^2 - 1))
- atanh: Inverse hyperbolic tangent via 0.5 * ln((1+x)/(1-x))

Special function:
- erf: Error function using Abramowitz & Stegun approximation 7.1.26

Tests include:
- Individual function tests for sinh, cosh, tanh, asinh, acosh, atanh
- Hyperbolic identity: cosh^2(x) - sinh^2(x) = 1
- Inverse identities: asinh(sinh(x))=x, acosh(cosh(x))=x, atanh(tanh(x))=x
- erf function tests and symmetry: erf(-x) = -erf(x)
Gamma function:
- Uses Lanczos approximation (g=7, n=9 coefficients)
- lgamma: Log-gamma function via log(gamma(x))
- Handles special cases: Γ(1)=1, Γ(2)=1

Bessel functions of first kind:
- j0: J₀(x) using polynomial for |x|<8, asymptotic otherwise
- j1: J₁(x) with proper sign handling

Bessel functions of second kind:
- y0: Y₀(x) using log-based formula for small x
- y1: Y₁(x) with asymptotic expansion for large x

Tests include:
- Gamma at integer values (factorial property)
- Γ(1.5) = √π/2 ~ 1.7724538
- Γ(n+1) = n*Γ(n) identity
- J0, J1 at standard test points
- Numerical derivative identity: J1(x) ≈ -J0'(x)
- Y0, Y1 at standard test points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants