Skip to main content

A Python library for tropical geometry computation, exploring the connection between ReLU neural networks and tropical rational maps.

Project description

tropix

tropix is a Python library for tropical geometry, implementing tropical polynomial arithmetic, Newton polytopes, regular subdivisions, and tropical hypersurfaces, motivated in part by their connection to ReLU neural networks through tropical rational maps (Zhang et al., 2018).

Status

Current version: v0.1.0

  • Tropical arithmetic
  • Tropical polynomials
  • Newton polytopes
  • Regular subdivisions
  • Analytical duals
  • Visualisation
  • 3D support, ReLU network interface (planned for v0.2)

Mathematical background

Tropical geometry replaces ordinary polynomial arithmetic with the min-plus semiring (R ∪ {∞}, +, min), where addition becomes "take the minimum" and multiplication becomes ordinary addition. A tropical polynomial is a piecewise-linear function; the set of points where its minimum is achieved by two or more monomials simultaneously is its tropical hypersurface. This hypersurface is combinatorially dual to a regular subdivision of the polynomial's Newton polytope, obtained by lifting each exponent vector by its coefficient and taking the lower convex hull of the resulting point cloud.

This library implements each stage of this pipeline:

arithmetic
    |
polynomials
    |
Newton polytope
    |
subdivision
    |
analytical dual
    |
visualisation

Installation

pip install -e .

Requires Python 3.9+, NumPy, SciPy, and Matplotlib.

Quick start

from tropix import TropicalPolynomial, tropical_edges, plot_tropical_curve_dual

# the standard tropical line: min(x, y, 0)
f = TropicalPolynomial({(1, 0): 0.0, (0, 1): 0.0, (0, 0): 0.0}, nvars=2)

vertices, edges, boundary_edges, cells, index_map = tropical_edges(f)
print(f"{len(vertices)} vertex, {len(boundary_edges)} rays")
# 1 vertex, 3 rays

plot_tropical_curve_dual(f)

Running this example constructs and plots the analytical dual of the standard tropical line.

The tropical line

The tropical line has one vertex at the origin and three unbounded rays:

tropical line dual

A non-simplicial tropical conic

This tropical conic contains a non-simplicial (quadrilateral) cell in its regular subdivision, producing three vertices, two bounded edges, and six rays instead of the four-vertex combinatorics of a fully triangulated smooth conic:

f = TropicalPolynomial({
    (2, 0): 0.0, (0, 2): 0.0, (0, 0): 0.0,
    (1, 1): -1.0, (1, 0): -0.5, (0, 1): -0.5,
}, nvars=2)

tropical conic dual

This is the analytical dual, exact rather than grid-approximated. For comparison, visualisation.py's brute-force grid approximation of the same curve's hypersurface, alongside its Newton polytope:

tropical conic grid approximation

Together these figures illustrate the complete computational pipeline implemented by tropix, from a tropical polynomial through its Newton polytope and regular subdivision to the corresponding tropical hypersurface and its analytical dual.

Polynomial
    |
    +----------> Grid approximation of T(f)  (independent, numerical)
    |
    v
Newton polytope
    |
    v
Regular subdivision
    |
    v
Analytical dual

What's implemented (v0.1)

  • arithmetic.py: TropicalNumber, the min-plus semiring. Validated domain, correct identity elements, consistent equality/hashing.
  • polynomial.py: TropicalPolynomial, exponent-to-coefficient maps with full input validation and tropical arithmetic.
  • newton.py: Newton polytope vertices and Minkowski sums, correctly handling the 1D case and degenerate inputs.
  • subdivision.py: the regular subdivision induced by a polynomial's coefficients. Explicitly detects the degenerate (affine) case via an SVD-based rank test.
  • dual.py: the analytical dual, tropical vertices, edges, and rays, computed from each cell's true geometric boundary.
  • visualisation.py: brute-force grid approximation of a tropical hypersurface, and Newton polytope plotting.

All five mathematical modules have a corresponding test file; 49 tests in total, covering the semiring axioms, evaluation correctness, the 1D Newton polytope fix, the flat-subdivision fix, and the exact combinatorics of both the tropical line and a genuine tropical conic, verified against known tropical geometry results.

pip install -e ".[dev]"
pytest

Connection to neural networks

Zhang, Naitzat, and Lim (2018) show that a ReLU network computes a tropical rational map: a ratio of tropical polynomials. Under this correspondence, the network's linear regions (the pieces of input space on which it reduces to a single affine map) correspond to cells of a polyhedral complex dual to a Newton polytope subdivision, exactly the structure this library computes directly.

A companion project, nn-from-first-principles, implements a feedforward network from scratch in NumPy and empirically visualises its linear regions. Extracting the actual tropical polynomial a trained network computes, connecting that project's empirical picture to this library's exact machinery, is the natural next step (planned for v0.2).

Notes and limitations (v0.1)

  • 2D only: hypersurface visualisation and the analytical dual construction currently require exactly 2 variables.
  • The grid-based hypersurface plot (visualisation.py) is a brute-force approximation; the analytical dual (dual.py) is exact.
  • No 3D support, no Gröbner bases, no tropical linear algebra yet; these are planned for later versions.

Current roadmap

v0.1 (current): 2D core, arithmetic, polynomials, Newton polytopes, regular subdivisions, the analytical dual, and grid-based visualisation.

v0.2: 3D hypersurfaces, a ReLU network interface for extracting the tropical rational map represented by a trained network, and explicit balancing-condition verification.

Further plans (Gröbner bases, tropical linear algebra, and beyond) are tracked in the repository's issues rather than here.

References

Zhang, L., Naitzat, G., & Lim, L.-H. (2018).
Tropical Geometry of Deep Neural Networks.
Proceedings of the 35th International Conference on Machine Learning (ICML).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tropix-0.1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tropix-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file tropix-0.1.0.tar.gz.

File metadata

  • Download URL: tropix-0.1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for tropix-0.1.0.tar.gz
Algorithm Hash digest
SHA256 80914ec9b4be4b36f5ae14ec1c6ce956d0b26d5fe5970943f777750e60987907
MD5 7ce013f03e5ed787d3e9c314209ac7c8
BLAKE2b-256 78491a7e5d54f066673d04d1b36473260a5d35db7a6cd970371c9bc75df70629

See more details on using hashes here.

File details

Details for the file tropix-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tropix-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for tropix-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 071ebf65355aadaafa8793c4a02ad9075f9f007a0d2fa6283d9ff2b45bdce5dd
MD5 e3bc7bf59172af1f2115ac38ac12054a
BLAKE2b-256 09458972bdb41a63c233d294050927ec2236d63067fd0169e72fd1942719fc7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page