Skip to main content

NumPy extension enabling arrays of rational numbers (fractions).

Project description

RationalPy

rationalpy is a Python library and extension of NumPy designed for creating and manipulating arrays of rational numbers, represented as exact fractions. It enables users to perform arithmetic operations on arrays while maintaining an exact rational representation.

Table of Contents


Installation

To install rationalpy, use pip:

pip install rationalpy

For optional development dependencies (e.g., testing and documentation), install with:

pip install .[test,docs]

Documentation

For complete documentation, visit the documentation website.

Usage

import rationalpy as rp

# Create a 1D RationalArray object
rarr = rp.rational_array([1, 2], [2, 3])
print(rarr)
# Output: [1/2 2/3]

# Create a 2D array
rarr_2d = rp.rational_array([[1, 2], [3, 4]], [[4, 5], [6, 7]])
print(rarr_2d)
# Output: [[1/4 2/5]
#          [1/2 4/7]]

Note that the arrays are automatically simplified upon instantiation. This behavior can be disabled by setting auto_simplify=False.

Features

  • Supports exact arithmetic with fractions.
  • Provides numpy-style broadcasting for arrays of rational numbers.
  • Supports common arithmetic operations (addition, subtraction, multiplication, and division) as well as some numpy array functions (np.sum, np.concatenate, and more).
  • Includes methods for fraction simplification RationalArray.simplify() and finding the least common denominator RationalArray.form_common_denominator().
  • Customizable __repr__ for displaying rational arrays in a clean, aligned format.

Examples

Simple arithmetic:

import rationalpy as rp

rarr1 = rp.rational_array([0, 1, 2, 3, 4], [1, 2, 3, 4, 5])
print(rarr1)
# Output: [0/1 1/2 2/3 3/4 4/5]

rarr2 = rp.rational_array(1, [1, 2, 3, 4, 5])
print(rarr2)
# Output: [1/1 1/2 1/3 1/4 1/5]

print(rarr1 + rarr2)
# Output: [1/1 1/1 1/1 1/1 1/1]

print(rarr1 - rarr2)
# Output: [-1/1 0/1 1/3 1/2 3/5]

print(rarr1 * rarr2)
# Output: [0/1 1/4 2/9 3/16 4/25]

print(rarr1 / rarr2)
# Output: [0/1 1/1 2/1 3/1 4/1]

Call basic numpy functions:

import numpy as np

for N in range(1, 11):
    rarr = rp.rational_array(1, np.arange(1, N + 1))
    print(f"S_{N}={np.sum(rarr)}")
# Output: S_1=1/1
#         S_2=3/2
#         S_3=11/6
#         S_4=25/12
#         S_5=137/60
#         S_6=49/20
#         S_7=363/140
#         S_8=761/280
#         S_9=7129/2520
#         S_10=7381/2520

Convert back to numpy arrays:

rarr = rp.rational_array([1, 2], [2, 3])
print(rp.asnumpy(rarr))
# Output: [0.5       0.66666667]

License

This project is licensed under the MIT License. See LICENSE for more information.

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

rationalpy-0.1.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

rationalpy-0.1.1-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rationalpy-0.1.1.tar.gz.

File metadata

  • Download URL: rationalpy-0.1.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for rationalpy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 58d6361ba983b525c89e1ec32f1edaa90255342290b26eb4a7fdcae696da0e1e
MD5 01d9dae2d0966fcf1987b90285147480
BLAKE2b-256 b7ac534a34e8b0e5f9a41e6d8c1d32ab08b3e24ced66b6bbf99c8b904e5eaf58

See more details on using hashes here.

File details

Details for the file rationalpy-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: rationalpy-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for rationalpy-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fad792336c6ec6765ef79b710420415be0a99faf6b95f27288f75da1f92bb7f3
MD5 17be5ecbc065bb51369cd8075065f106
BLAKE2b-256 e551a82a9fac2a554055e491a676f9f6fefe69688ccd08caf34746708531004c

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