-
Notifications
You must be signed in to change notification settings - Fork 6
Add Chebyshev approximations #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sigilante
wants to merge
8
commits into
main
Choose a base branch
from
claude/add-numeric-types-A3HVY
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prototype implementation demonstrating jet-compatible approach for transcendental functions (exp, sin, cos, tan) using:
Key principle: every FP operation happens in same order with same intermediate values in both Hoon and C jet.