mathcompile 0.1.2

High-performance symbolic mathematics with final tagless design, egglog optimization, and Rust hot-loading compilation
Documentation
#!/usr/bin/env julia

# Simple example demonstrating optimal rational function approximation
using MathCompile

println("๐Ÿ”ฌ MathCompile: Optimal Rational Function Approximation")
println("=" ^ 55)
println()

# Example: Find the fastest rational approximation for exp(x) on [0,1]
println("๐Ÿ“Š Finding optimal rational approximation for exp(x) on [0,1]")
println("   Target tolerance: 1e-6")
println()

f(x) = exp(x)
result = find_optimal_rational(f, (0.0, 1.0), 1e-6)

println("โœ… Results:")
println("   โ€ข Optimal degrees: numerator=$(result.degree_n), denominator=$(result.degree_d)")
println("   โ€ข Total degree: $(result.total_degree)")
println("   โ€ข Achieved error: $(result.error)")
println()

# Test the approximation at a few points
println("๐Ÿงช Testing approximation accuracy:")
test_points = [0.0, 0.25, 0.5, 0.75, 1.0]
for x in test_points
    actual = f(x)
    approx = evaluate_rational(x, result.N, result.D)
    error = abs(actual - approx)
    println("   โ€ข f($x) = $(round(actual, digits=6)), approx = $(round(approx, digits=6)), error = $(error)")
end
println()

# Compare with different tolerance levels
println("๐Ÿ“ˆ Tolerance vs. Degree comparison:")
tolerances = [1e-3, 1e-6, 1e-9]
for tol in tolerances
    try
        res = find_optimal_rational(f, (0.0, 1.0), tol)
        println("   โ€ข Tolerance $tol โ†’ Total degree: $(res.total_degree)")
    catch e
        println("   โ€ข Tolerance $tol โ†’ Failed: $e")
    end
end
println()

println("๐ŸŽฏ Key insight: Lower degree rational functions are faster to evaluate!")
println("   This package automatically finds the minimal degree needed for your tolerance.")