mathfirst
mathfirst is a math-native layer for representing and working with mathematics in Python.
The core idea is simple:
Model mathematics according to its mathematical meaning first, and treat symbolic, numerical, visualization, and other libraries as implementation backends.
Instead of building directly around library-specific objects such as SymPy expressions, NumPy arrays, or plotting primitives, mathfirst defines its own mathematical concepts and relationships:
Scalar
Variable
Expression
Set
Function
Polynomial
Point
Root
...
These objects should preserve the structure and semantics of the mathematics they represent, including concepts such as exactness, assumptions, domains, codomains, dimensions, and algebraic relationships.
SymPy is the current symbolic backend. NumPy, SciPy, or mpmath may support future computational needs, while the public mathematical model remains defined by mathfirst.
The guiding principles are:
- Mathematical meaning before implementation details
- Exact mathematics where possible; approximation should be explicit
- Objects represent mathematical concepts
- Operations transform mathematical objects
- Mathematical structure and assumptions should be explicit
- Backend-specific behavior should remain behind deliberate boundaries
- Downstream systems consume the common math model
Current behavior and usage
Variable("x")represents a real variable by default; equal names represent the same variable.Scalarpreserves exact integers, fractions, and decimal strings.Scalar.exact(...)rejects approximate inputs;Scalar.approx(...)explicitly requests approximation. Passing a Python float directly toScalaremits anApproximateScalarWarningwith both alternatives.Scalar.is_exactdescribes the current representation, rather than the provenance of previous arithmetic.- Function domains and codomains are optional, descriptive metadata. Construction validates expression and argument structure, and evaluation checks argument count. Set membership is available explicitly through
Set.contains(); Cartesian products useSet.product(). - General derivatives leave domain and codomain unspecified unless supplied. Polynomial derivatives remain polynomials with real default sets.
- Expression equality is structural.
real_roots()returns exact real roots with multiplicities;limit()defaults to a two-sided approach and accepts an explicit direction. to_sympy()provides a deliberate backend escape hatch, including SymPy lambdas for functions and SymPy polynomials for polynomials.
from fractions import Fraction
from mathfirst import Polynomial, Scalar, Variable, derivative
x = Variable("x")
exact_decimal = Scalar.exact("0.1") # Exact 1/10
exact_fraction = Scalar(Fraction(1, 3))
approximate = Scalar.approx(0.1) # Explicit approximation, without a warning
p = Polynomial(x**2 - exact_decimal, x)
print(p(Scalar(2))) # 39/10
print(derivative(p)) # 2*x
backend_polynomial = p.to_sympy()
Install with uv add mathfirst or pip install mathfirst. Requires Python
3.12–3.14. SymPy is the current required backend.
Package tests live in tests/. See the
development guide for setup, checks, and releases.
Future direction
Other packages can build on this common mathematical foundation for visualization, simulation, or analysis. One possible future visualization layout is:
mathfirst
├── core mathematical objects
├── symbolic / numeric operations
└── viz/
└── visualization of mathematical objects
Visualization is therefore not the purpose of the math layer itself; it is one consumer of it.
The long-term goal is for code using mathfirst to read and behave more like the mathematics being expressed, rather than like the particular libraries used to compute it.
Metadata
Release files for mathfirst 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mathfirst-0.2.0.tar.gz | 12.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mathfirst-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.5 kB
Release files / mathfirst-0.2.0.tar.gz
| Download URL | mathfirst-0.2.0.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35a405eb7220b3bd4983ef7097cddafc1d57b13e899d5803ef5b243285e2350f
|
|
BLAKE2b-256 checksum How to use checksums |
9e282fb4e657ed7c1f7d350144687e975a6785972e647b887f9d85dbcadaae55
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logRelease files / mathfirst-0.2.0-py3-none-any.whl
| Download URL | mathfirst-0.2.0-py3-none-any.whl |
|---|---|
| Size | 14.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
849197f0bdaeab97a5d132147a97d61c2a71e73cf61f0cba82735fc810db4f28
|
|
BLAKE2b-256 checksum How to use checksums |
3bd78333b66d8c84ef91753a6189bcecbc18b2211cc5738d84b6d3729d9b9e6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency log