math.calculus
solve f(x) = 0, find slopes, limits and areas under curves, numerically
Solving equations, finding slopes, and measuring areas under curves, for any function of one number. The function is given as a lambda, like |x| x ** 2 - 2.
good to know
Section titled “good to know”These work numerically, not by algebra: they try values until the answer is accurate to many digits, so their answers are floats that are very close but not always exact.
Tags: algebra
rootan x where f(x) = 0derivthe slope of f at x (its derivative), estimated from f just either side of x, or with n its nth derivativeminimizethe x between lo and hi where f is smallestmaximizethe x between lo and hi where f is largestsolve_systemthe point where every function in fs is 0 at once, found from a starting guess (Newton’s method)limitwhat f(x) approaches as x approaches a, which can be inf or -infintegratethe area under f between a and b (its integral), where a and b can be inf or -inf
Functions
Section titled “Functions”root(f: fn, guess: num) -> float / root(f: fn, lo: num, hi: num) -> float
an x where f(x) = 0
Given a starting guess, finds the nearest one it can (Newton’s method). Given lo and hi where f is negative at one and positive at the other, always finds one in between (bisection)
1root(|x| x ** 2 - 2, 1)1.414212root(|x| x ** 3 - x - 1, 1, 2)1.32472See also deriv
deriv(f: fn, x: num, n?: int) -> float
the slope of f at x (its derivative), estimated from f just either side of x, or with n its nth derivative
Orders above 1 are less accurate, and go up to 6
1deriv(|x| x ** 2, 3)62deriv(sin, 0)13deriv(|x| x ** 4, 1, 2)12minimize
Section titled “minimize”minimize(f: fn, lo: num, hi: num) -> float
the x between lo and hi where f is smallest
Squeezes the range around the lowest point (golden-section search), so f should dip once between lo and hi. Accurate to about 8 digits
1minimize(|x| (x - 2) ** 2 + 1, 0, 5)22minimize(cos, 2, 5)3.14159maximize
Section titled “maximize”maximize(f: fn, lo: num, hi: num) -> float
the x between lo and hi where f is largest
Same search as minimize, so f should have one peak between lo and hi
1maximize(sin, 0, 3)1.57082maximize(|x| x * (10 - x), 0, 10)5solve_system
Section titled “solve_system”solve_system(fs: list, guess: list) -> list
the point where every function in fs is 0 at once, found from a starting guess (Newton’s method)
Each function takes one number per unknown and the guess is a list with one number each, like solve_system([|x, y| x + y - 3, |x, y| x - y - 1], [0, 0])
1solve_system([|x, y| x + y - 3, |x, y| x - y - 1], [0, 0])[2, 1]2solve_system([|x, y| x ** 2 + y ** 2 - 4, |x, y| x - y], [1, 1])[1.41421, 1.41421]See also root
limit(f: fn, a: num, side: str = “both”) -> float
what f(x) approaches as x approaches a, which can be inf or -inf
Samples f closer and closer to a and extrapolates (Richardson). side is “both”, “left” (from below) or “right” (from above), and when the two sides differ, that is an error
1limit(|x| sin(x) / x, 0)12limit(|x| (1 + 1 / x) ** x, inf)2.718283limit(|x| abs(x) / x, 0, "right")14limit(|x| 1 / x ** 2, 0)∞See also deriv
integrate
Section titled “integrate”integrate(f: fn, a: num|quantity, b: num|quantity) -> float|quantity
the area under f between a and b (its integral), where a and b can be inf or -inf
With plain numbers it splits the range finer where f bends most (adaptive Simpson’s rule), and with quantities it adds up 1000 even slices, so units carry through and speed over time gives distance
1integrate(|x| x ** 2, 0, 3)92integrate(sin, 0, pi)23integrate(|x| exp(-x ** 2), -inf, inf)1.77245See also deriv
More examples
Section titled “More examples”calculus
Section titled “calculus”1# √2 as the x where x² - 2 = 02root(|x| x ** 2 - 2, 1)1.414213# where cos(x) = x, searching between 0 and 14root(|x| cos(x) - x, 0, 1)0.7390855# slope of x³ at 26deriv(|x| x ** 3, 2)127# sin(x)/x near 0, where it can't be computed directly8limit(|x| sin(x) / x, 0)19# the area under a bell curve, squared, is pi10integrate(|x| exp(-x ** 2), -10, 10) ** 23.1415911# distance from speed: units carry through12integrate(|t| 9.8 m/s^2 * t, 0 s, 3 s)44.1 mExample results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.