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 denominatorRationalArray.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rationalpy-0.1.0.post2.tar.gz.
File metadata
- Download URL: rationalpy-0.1.0.post2.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c14a7a0b1598b3fe3b8d07bdfde44c694468389e541ee130d272dee9293f666
|
|
| MD5 |
d58bb186b018da533d9daed7139e1946
|
|
| BLAKE2b-256 |
74e518d8465a59ea386a84ef43f0da26f074d8ee1fa5752eb92d42ce8528ad0c
|
File details
Details for the file rationalpy-0.1.0.post2-py2.py3-none-any.whl.
File metadata
- Download URL: rationalpy-0.1.0.post2-py2.py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9441cd3d1fa513eaec20fdb126e5baba861c8f825a44a36f88183b3b3738a338
|
|
| MD5 |
ce6a61b9dbe73416e0e1285b9e00495c
|
|
| BLAKE2b-256 |
d7a11936eb942d4f34ae552417e1c0c36faac4a1340530d542090261c39bc3c7
|