Skip to content
U-C4NPublic

About

Exact symbolic math library in pure Python with zero dependencies. A lightweight computer algebra system (CAS): solve equations & inequalities, find real polynomial roots, exact linear algebra & calculus, with independently verified results. Built for apps & AI agents.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MathForge

Exact mathematics. Built from first principles.
By Umutcan EDIZASLAN

Python 3.11+  ·  Dependency-free core  ·  v0.7.0 alpha

Releases   /   Changes   /   Example   /   MIT license


New in v0.7

  • Real elementary functions: explicit domain-preserving functions, exact constants, repeated and multivariate derivatives, Taylor polynomials and certified limit rules.
  • Exact integration: Hermite reduction and Lazard–Rioboo–Trager primitives for rational functions, a bounded elementary catalogue and proper finite integrals with whole-interval domain checks.
  • Complex algebra: exact algebraic real/imaginary components, arithmetic, principal square roots and all rational quadratic roots.
  • Discrete mathematics: certified integer arithmetic, generalized CRT, factorization, primality certificates, combinatorial counts and sequences.
  • Probability and linear algebra: finite rational distributions, exact descriptive statistics, LU, subspaces and the complete least-squares minimizer family.
  • Connected evidence: independent checks, mathematical JSON v5 and thirty local MCP tools. Unanswered integrals and limits carry explicit deferred requests.
import mathforge as mf
from mathforge import functions as fn

x = mf.symbol("x")
f = mf.real_function(fn.sin(x)/fn.as_real_expr(x), (x,))
result = mf.operations.function_limit(f, x, 0)
assert result.unwrap().value == fn.as_real_expr(1)
assert mf.verify(mf.loads(mf.dumps(result))).verified
assert mf.expectation(mf.discrete_uniform((1, 2, 3, 4, 5, 6))) == mf.Rational(7, 2)
assert mf.complex_pow(mf.ComplexAlgebraic(0, 1), 2) == mf.ComplexAlgebraic(-1, 0)

See the v0.7 mathematics guide for domains, supported families and API examples. A formal RationalPrimitive certifies an antiderivative identity; general LogRootSum point evaluation is outside scope. Improper integrals, general complex root isolation, symbolic matrices and approximate numerical computation remain future work.

Added in v0.6

  • Exact polynomial algebra: complete irreducible factorization over the rationals, resultants, discriminants and partial fractions that preserve excluded points.
  • Reusable algebraic numbers: explicit normalization, division, integer powers, minimal polynomials and evaluation using real algebraic roots.
  • Connected calculus: rational-function limits at algebraic points, polynomial integrals with algebraic endpoints and exact extrema coordinates.
  • Spectral algebra: rational characteristic polynomials and every real eigenvalue with algebraic multiplicity.
  • Checked end to end: independent certificate checks, mathematical JSON v4, historical byte/hash compatibility and twenty-eight optional local MCP tools.
import mathforge as mf

x = mf.symbol("x")
factors = mf.factor(mf.Polynomial.from_expression(x**4 + 4, x))
assert len(factors.factors) == 2
alpha = mf.RealAlgebraicRoot((-2, 0, 0, 1), 0)
assert mf.algebraic(alpha**3) == mf.Rational(2)
assert mf.evaluate_algebraic(x**3, {x: alpha}) == mf.Rational(2)
assert mf.integrate(x, x, 0, mf.sqrt(2)) == mf.Rational(1)
assert mf.verify(mf.operations.charpoly(mf.Matrix(((0, 2), (1, 0))), x)).verified

Read the exact algebra guide for polynomial domains, certificates and explicit real algebraic arithmetic.

Added in v0.5

  • Multivariate calculus: sparse rational-polynomial expansion, partial derivatives, gradients, Jacobians and Hessians with explicit variable order.
  • Less repeated arithmetic: Sturm isolation carries endpoint signs between subdivisions; RREF skips known zero prefixes while retaining replayable row evidence.
  • Checked results: independent coefficient checks, exact text/LaTeX and sixteen optional local MCP tools.
  • Compatible values: new derivatives use existing expressions, immutable tuples and mathematical JSON v2 requests; no new wire types.

See the multivariate guide and measured performance. Local three-run medians showed 2.37x faster degree-20 roots and 1.50x faster 32-by-32 RREF; timings depend on the machine and input, and are not performance guarantees.

Added in v0.4

  • Rational limits: exact finite, one-sided and infinite limits; a nonexistent limit is a completed mathematical result.
  • Function analysis: domain, holes and poles, signs, stationary points, maximal monotonic intervals and strict local extrema locations.
  • Checked results: independent algorithmic checks and exact text/LaTeX.
  • Additive transport: new analysis records use mathematical JSON v3; existing v1/v2 objects keep their bytes and content references.

MathForge implements its own exact arithmetic with finite computation budgets. The core includes real polynomial roots, rational equations and inequalities, exact linear algebra, multivariate polynomial calculus, and rational-function limits and analysis. Verification is algorithmic, not formal proof. General complex polynomial roots, unrestricted transcendental calculus, approximate output and remote HTTP serving are outside this alpha.

Quick start

From a local checkout:

python -m pip install -e .
import mathforge as mf

x = mf.symbol("x", domain=mf.Reals)
problem = mf.parse_problem("x^2 - 2 = 0", symbols={"x": x})
result = mf.solve(problem, for_=x)

print(mf.render_text(result.solution_set))
print(mf.to_latex(result.solution_set))
assert mf.verify(result).verified
assert mf.parse_expression("0.1", symbols={}) == mf.Rational(1, 10)

Exact mathematics, including excluded points

roots = mf.real_roots(x**3 - x, x)  # -1, 0, 1 with multiplicities
rf = mf.rational_function(x**2 - 1, x - 1, variable=x)
assert mf.solve(mf.Eq(rf, 2), for_=x).outcome is mf.Outcome.NO_SOLUTION
assert mf.integrate(x**2, x, 0, 1) == mf.Rational(1, 3)
assert mf.det(mf.Matrix(((1, 2), (3, 4)))) == mf.Rational(-2)

Full workflow · Linear systems · Rational inequalities

Differentiate in several variables

y = mf.symbol("y")
f = x**2*y + y**3
assert mf.gradient(f, (x, y)) == (2*x*y, x**2 + 3*y**2)
assert mf.hessian(f, (x, y)) == ((2*y, 2*x), (2*x, 6*y))
assert mf.verify(mf.operations.jacobian((f, x*y), (x, y))).verified

Jacobian and Hessian values are immutable tuples of expression rows. The existing Matrix type remains rational-valued.

Analyze a function with a hole and a pole

f = mf.parse_expression("((x-1)*(x^2+2))/(x*(x-1))", symbols={"x": x})
analysis_result = mf.operations.analyze_function(f, x)
analysis = analysis_result.unwrap()
assert not analysis.domain.contains(0) and not analysis.domain.contains(1)
assert [(p.point, p.kind) for p in analysis.excluded_points] == [
    (mf.Rational(0), "pole"), (mf.Rational(1), "hole")]
assert mf.limit(f, x, 1) == mf.LimitValue("finite", mf.Rational(3))
assert mf.limit(f, x, 0).kind == "does_not_exist"
assert mf.verify(analysis_result).verified
print(mf.render_text(analysis))

The hole at x=1 remains excluded even though its limit is 3. The two-sided limit at x=0 does not exist because its one-sided limits have opposite signs. See the function analysis guide and the complete executable example.

Connect an agent locally

Install the optional adapter into the environment used by your MCP client:

python -m pip install -e ".[mcp]"
mathforge-mcp

The process speaks MCP on stdin/stdout, not an interactive terminal prompt. The base install has no runtime dependencies; the mcp extra installs the official MCP SDK and its dependencies. There is no API key or HTTP server.

See client configuration and tool inputs, the user guide, and API reference.

Development

python -m unittest discover -s tests -v
python tools/check_docs.py
python -m pip install -r requirements-dev.txt
python -m build
python tools/check_distribution.py

Core tests use Python's standard library. Protocol tests use the optional MCP SDK. Build, schema, typing and the seeded SymPy comparison suite are development dependencies. Distribution checks validate both base and MCP-extra installations. Public documentation/ is tracked and included in the sdist; docs/ remains local working material and is excluded from Git and release packages.

About

Exact symbolic math library in pure Python with zero dependencies. A lightweight computer algebra system (CAS): solve equations & inequalities, find real polynomial roots, exact linear algebra & calculus, with independently verified results. Built for apps & AI agents.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages