Find closed-form expressions for numerical values
Project description
find-closed-form
Given a numerical value, find_closed_form searches for closed-form
mathematical expressions that match it — testing candidates directly by
comparing digits and filtering by a formula-complexity heuristic.
This is a Python translation of the Wolfram Language ResourceFunction FindClosedForm, contributed by the same author and vetted by the Wolfram reviewers.
Quick start
from find_closed_form import find_closed_form
# Identify sqrt(2)/2
find_closed_form(0.7071067811865476)
# [sqrt(2)/2]
# Identify the Euler–Mascheroni constant
find_closed_form(0.5772156649015329)
# [EulerGamma]
# bad: less digits do not work automatically
# bad: single output should not be a list
How it works
-
Argument-range generation — For each search round the algorithm expands a Farey-based range of rational arguments (built-in
farey_rangeimplementation). -
Function evaluation — A library of ~27 common mathematical functions (trig, exponential, logarithmic, special functions, and mathematical constants) is evaluated over the argument range.
-
Digit matching — Candidate expressions are accepted when
|1 − candidate/target| ≤ 10^(−digits+1), where digits is auto-detected from the input or set explicitly. -
Algebraic combinations — Beyond direct
f(arg)matches, the algorithm also searches for:- Multiplicative:
a · f(b) ≈ target, where a is a simple algebraic number (integer, rational, or root). - Additive:
a + f(b) ≈ target.
- Multiplicative:
-
Complexity filtering — Every candidate is scored using the built-in
formula_complexityheuristic and discarded if it exceeds the threshold.
API
find_closed_form(
y, # target number (int, float, Fraction, sympy)
functions=None, # callable, list of callables, or list of (name, fn, filter) tuples
max_results=1, # how many results to return
*,
significant_digits=None, # precision target (auto-detected if None)
formula_complexity_threshold=None, # max allowed complexity (auto-scaled if None)
algebraic_factor=True, # enable a·f(b,...) search
algebraic_add=True, # enable a+f(b,...) search
max_search_rounds=50, # max argument-range expansion rounds
)
Examples
1. Recognise a trigonometric value
import math
find_closed_form(math.cos(math.pi / 5))
# [GoldenRatio/2]
2. Identify a logarithmic expression
find_closed_form(0.6931471805599453)
# [log(2)]
3. Pass a pure function to search over
from sympy import sin, pi
find_closed_form(0.8660254037844386, functions=lambda x: sin(pi * x))
# [sin(pi/3)]
# bad actual result is 0
4. Search with a named function and domain filter
from sympy import asin
find_closed_form(
1.0471975511965976,
functions=[("asin(#)", lambda x: asin(x), lambda x: 0 <= x <= 1)],
)
# [asin(sqrt(3)/2)]
5. Combine multiple functional forms
from sympy import cos, exp, pi
find_closed_form(
0.5,
functions=[lambda x: sin(pi * x), lambda x: cos(pi * x)],
max_results=3,
)
# [sin(pi/6), cos(pi/3), ...]
6. Two-argument function — multiplicative
from sympy import log
# 2 * log(3) ≈ 2.1972...
find_closed_form(2.1972245773362196, functions=lambda x, y: x * log(y))
# [2*log(3)]
7. Two-argument function with domain filter
find_closed_form(
1.0,
functions=[(
"x*sin(pi*y)",
lambda x, y: x * sin(pi * y),
lambda x, y: x > 0 and 0 < y < 1,
)],
)
# [2*sin(pi/6)]
8. Three-argument function
# Search for x + y*log(z)
find_closed_form(
2.3862943611198906,
functions=lambda x, y, z: x + y * log(z),
max_search_rounds=5,
)
# [1 + 2*log(2)] or equivalent
9. Use a regular Python function (not just lambdas)
from sympy import exp, sqrt
def my_form(x):
return exp(x) / sqrt(2)
find_closed_form(1.9221276790498548, functions=my_form)
# [exp(1)/sqrt(2)] or equivalent
10. Fewer significant digits — find approximate matches
find_closed_form(1.414, significant_digits=4, max_results=5)
# [sqrt(2), ...] and other expressions close to 1.414
11. Stricter matching with more significant digits
find_closed_form(1.4142135623730951, significant_digits=15)
# [sqrt(2)]
12. Controlling complexity to prefer simpler expressions
find_closed_form(0.7071067811865476, formula_complexity_threshold=10)
# [sqrt(2)/2]
Formula complexity
The complexity of a candidate expression is computed as the sum of per-integer complexities for every integer appearing in the expression:
int_complexity(n) = 0.5 × mean(digit_sum, 5×len, #prime_factors, √n)
This is the same definition as the WL AlgebraicRange resource function.
Dependencies
- sympy — symbolic mathematics (only runtime dependency)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file find_closed_form-0.0.2.tar.gz.
File metadata
- Download URL: find_closed_form-0.0.2.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5898fac38f6cc0af970cfb3d04e33b849fbc6a101eb788806199b629d5e3101
|
|
| MD5 |
30b87ad0573773675c97707bded8a1a2
|
|
| BLAKE2b-256 |
90d815c364e247486fcb15c4d26bab2a2e2c8db1b428b1a60bea8cb219c6993c
|
File details
Details for the file find_closed_form-0.0.2-py3-none-any.whl.
File metadata
- Download URL: find_closed_form-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a09959f9087f3b218d08e3e02fb2e54e9ea926104286a8e55b387915a8b1e11
|
|
| MD5 |
5c32f3a18326bbc0c67f29135ccfad10
|
|
| BLAKE2b-256 |
722bf40130e168744c8d2b736d5464fccd881be5104c6250330c2582fe37a62e
|