Skip to main content

A transformer-based math library

Project description

MathFormer

MathFormer is a Python library that leverages Transformer-based language models to perform mathematical operations. Unlike standard arithmetic libraries, MathFormer uses Llama-architecture models to "predict" the results of arithmetic operations, token by token, demonstrating the capability of small language models to learn arithmetic rules.

It supports basic arithmetic operations: Addition, Subtraction, Multiplication, and Division.

Features

  • Transformer-Powered Arithmetic: Uses specialized Llama-based models for each arithmetic operation.
  • Large Number Support: Implements recursive logic to handle multi-digit arithmetic using digit-by-digit prediction (similar to manual calculation).
  • Unified API: Easy-to-use functions for add, sub, mul, and div.
  • Resource Management: Supports lazy loading of models to save memory, and manual unloading.
  • Custom Tokenizer: Built-in minimalist tokenizer optimized for mathematical expressions.

Installation

You can install MathFormer via pip:

pip install mathformer

Quick Start

The simplest way to use MathFormer is through the top-level convenience functions. These functions automatically handle model loading when needed.

import mathformer

# Addition
result = mathformer.add(123, 456)
print(f"123 + 456 = {result}")  # Output: 579

# Subtraction
result = mathformer.sub(1000, 250)
print(f"1000 - 250 = {result}") # Output: 750

# Multiplication
result = mathformer.mul(12, 12)
print(f"12 * 12 = {result}")    # Output: 144

# Division (returns decimal for non-exact results)
result = mathformer.div(100, 3)
print(f"100 / 3 = {result}")    # Output: 0.3333333333

result = mathformer.div(100, 4)
print(f"100 / 4 = {result}")    # Output: 25 (exact division)

You can also pass string expressions:

print(mathformer.add("100 + 200"))
print(mathformer.calculate("mul", 50, 4))

Decimal Support (v1.1.0+)

MathFormer now supports decimal (floating-point) arithmetic while maintaining its algorithm-based approach:

import mathformer

# Decimal addition
result = mathformer.add(1.5, 2.3)
print(f"1.5 + 2.3 = {result}")  # Output: 3.8

# Decimal subtraction (with negative results)
result = mathformer.sub(2.3, 5.5)
print(f"2.3 - 5.5 = {result}")  # Output: -3.2

# Decimal multiplication
result = mathformer.mul(1.5, 2.5)
print(f"1.5 * 2.5 = {result}")  # Output: 3.75

# Division with custom precision
result = mathformer.div(1, 7, precision=5)
print(f"1 / 7 = {result}")      # Output: 0.14285

# Mixed decimal and integer operations
result = mathformer.div(7.5, 2.5)
print(f"7.5 / 2.5 = {result}")  # Output: 3

Division Behavior

  • Exact division: Returns an integer (e.g., 10 / 2 = 5)
  • Non-exact division: Returns a decimal with up to 10 decimal places (configurable via precision parameter)
  • No more Q/R format: Division now outputs decimals instead of Q{quotient}R{remainder} format

Advanced Usage

For more control over resource usage, you can use the MathFormerAPI class directly.

Managing Resources (Load/Unload)

By default, models are lazy-loaded (loaded only when first requested). You can manually load all models at startup or unload them to free GPU/CPU memory.

from mathformer import MathFormerAPI

# Initialize API (lazy_load=False to load everything immediately)
api = MathFormerAPI(lazy_load=True)

# Perform operations
print(api.add(50, 50))

# Unload all models to free memory
api.unload_all()

Context Manager

You can use MathFormerAPI as a context manager to ensure models are cleaned up after use:

from mathformer import MathFormerAPI

with MathFormerAPI() as api:
    print(api.mul(99, 9))
# Models are automatically unloaded here

How It Works

MathFormer isn't just calling Python's + or - operators. It actually uses a neural network to predict the result!

  1. Single-Step Prediction: For small single-digit operations (e.g., 5+7), it queries a Transformer model customized for that operation.
  2. Multi-Digit Logic: For larger numbers (e.g., 123+456), the library implements the standard grade-school algorithms (carrying, borrowing, partial products) but delegates the fundamental single-digit arithmetic steps to the Transformer model.

Training Repositories

The training code and datasets for the models used in this library can be found in the following repositories:

Requirements

  • Python >= 3.8
  • PyTorch >= 2.0.0
  • Transformers >= 4.30.0

License

This project is licensed under the MIT License.

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

mathformer-1.2.0.tar.gz (7.9 MB view details)

Uploaded Source

Built Distribution

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

mathformer-1.2.0-py3-none-any.whl (7.9 MB view details)

Uploaded Python 3

File details

Details for the file mathformer-1.2.0.tar.gz.

File metadata

  • Download URL: mathformer-1.2.0.tar.gz
  • Upload date:
  • Size: 7.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mathformer-1.2.0.tar.gz
Algorithm Hash digest
SHA256 be3eeede52a9edfb89f55843f12a11f185d07d99c0e7751a1e9233b4550d2ec8
MD5 f31a036083811eed4341e9f5550c1d3b
BLAKE2b-256 de6e1de7d38b09c3f4a9aff05459248e61a640da8c852639a2d6997da1376f38

See more details on using hashes here.

File details

Details for the file mathformer-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: mathformer-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mathformer-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dda27566f4b4f9399cd66ea5e026ad7f4a83e4102125cfdcc448936d32b269a9
MD5 becac3528c0c0e555667013c5519773c
BLAKE2b-256 c32daa679b1dd0456864dbcf43c47771eda5bcf691936916b47c4b459a3440d0

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