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.2.post1.tar.gz (13.9 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.2.post1-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rationalpy-0.1.2.post1.tar.gz.

File metadata

  • Download URL: rationalpy-0.1.2.post1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for rationalpy-0.1.2.post1.tar.gz
Algorithm Hash digest
SHA256 3f6a194cad4de5121dc47b38e44d9b4f77bad9891f8dd0f452af22afe31dd27b
MD5 b289e32191502980d0f2b55852965926
BLAKE2b-256 bc3ca3923fadbac28cd4735c78bc18df21a5ebfc33e72a3e48e367f6c05ee902

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for rationalpy-0.1.2.post1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1bd7b59d59f4373a5792458af106b774079b0af330a82afb99e130d327be24b9
MD5 6787b31baf40e958773a66e2cccc0443
BLAKE2b-256 eda59b78ff73ffbaff0798be6c5c55d85773fb5fe171ddcefc562f074230257c

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