API Documentation

RationalPy module.

This module provides functionality for handling rational numbers.

class rationalpy.RationalArray(numerator: ndarray | list | tuple | int, denominator: ndarray | list | tuple | int = 1, auto_simplify: bool = True, dtype: dtype | None = None, copy: bool = True, order: str = 'K', ndmin: int = 0)

Bases: NDArrayOperatorsMixin

A class to represent arrays of rational numbers.

asnumpy(dtype: dtype | None = None, copy: bool | None = None)
Alias for the __array__ method, which returns the RationalArray as a numpy

array.

Parameters:
  • dtype (np.dtype, optional) – The data type of the output numpy array. If not provided, the data type is inferred from the RationalArray.

  • copy (bool, optional) – Unused parameter. Kept for compatibility with numpy.

Returns:

The RationalArray as a numpy array

Return type:

np.ndarray

asratio() Tuple[ndarray, ndarray]

Return the RationalArray as a tuple of numpy arrays.

Returns:

The RationalArray as a tuple of numpy arrays.

Return type:

Tuple[np.ndarray, np.ndarray]

copy() RationalArray

Return a copy of the RationalArray.

decompose() Tuple[RationalArray, RationalArray]

Decompose the rational array into two parts: n_i / 1 and 1 / d_i.

Returns:

The decomposed RationalArrays.

Return type:

Tuple[RationalArray, RationalArray]

form_common_denominator(inplace: bool = True)

Form a common denominator for the rational array.

Parameters:

inplace (bool, optional) – Whether to perform the operation in-place.

Notes

  • The array is simplified before forming the common denominator.

reciprocal() RationalArray

Return the reciprocal of the RationalArray (1 / self).

simplify(inplace: bool = True)

Element-wise rational array simplification.

Parameters:

inplace (bool, optional) – Whether to perform the operation in-place.

validate(simplify: bool | None = None)

Validate the RationalArray arrays with optional simplification.

Parameters:

simplify (bool, optional) – Whether to simplify the RationalArray after validation. If not provided, the auto_simplify attribute is used.

rationalpy.asnumpy(rarr: RationalArray, dtype: dtype = None) ndarray

Convert a RationalArray to a numpy array.

Parameters:
  • rarr (RationalArray) – The RationalArray to convert. dtype (np.dtype, optional): The data type of the output numpy array.

  • provided (If not)

  • RationalArray. (the data type is inferred from the)

Returns:

A numpy array with the same shape as the RationalArray.

Return type:

np.ndarray

rationalpy.rational_array(numerator: int | List | Tuple | ndarray, denominator: int | List | Tuple | ndarray = 1, auto_simplify: bool = True, dtype: dtype = None, copy: bool = True, order: str = 'K', ndmin: int = 0) RationalArray

Factory function to create a RationalArray.

Parameters:
  • numerator (array_like) – The numerator values. Must be an integer array.

  • denominator (array_like) – The denominator values. If not provided, the denominator is set to 1. Must be an integer array.

  • auto_simplify (bool) – Whether to simplify the RationalArray after it is initialized.

  • dtype (np.dtype, optional) – The data type of the numerator and denominator arrays. If not provided, the data type is inferred from input arrays. Must be an integer data type. Passed to numpy.array.

  • copy (bool) – Whether to copy the input arrays when defining the numerator and denominator attributes. Passed to numpy.array.

  • order (str) – The memory layout of the numerator and denominator attributes. Passed to numpy.array.

  • ndmin (int) – The minimum number of dimensions of the numerator and denominator attributes. Passed to numpy.array.

Returns:

A RationalArray object.

Return type:

RationalArray

Notes

The dtype, copy, order, and ndmin arguments are passed to the

numpy.array function when defining the numerator and denominator attributes. See the numpy.array documentation for more information.