Polynomials, exact arithmetic and number theory

Polynomial calculations, rational matrices and arbitrary-size integers.
import rustymath as rm

Polynomials

rm.polynomial evaluates polynomials, finds roots, constructs coefficients from roots, differentiates and fits data, under the names of numpy.polynomial.polynomial. Coefficients start from the constant term. Evaluation, construction and derivatives take real or complex arrays. polyvalfromroots(x, roots, m=1) evaluates directly from roots without forming coefficients. The roots of x^3 - 6x^2 + 11x - 6 are 1, 2 and 3:

rm.polynomial.polyroots([-6, 11, -6, 1])
Array([(1+0j), (2-0j), (3+0j)], dtype=complex128)

Exact arithmetic

rm.exact computes with integers and fractions.Fraction. It provides rational gcd, lcm, remainder and binomial coefficients, matrix solves, pseudoinverses, determinants, permanents and row reduction, and polynomial construction, evaluation and derivatives. Arguments can be integers, Fractions, nested lists of them, or integer arrays. Results are Fractions, in nested lists for arrays. Matrix calls take one matrix. Ordinary rm.linalg calls stay floating-point.

from fractions import Fraction
rm.exact.solve([[2, 1], [1, 2]], [Fraction(1, 2), 0]), rm.exact.binom(Fraction(3, 2), 2)
([Fraction(1, 3), Fraction(-1, 6)], Fraction(3, 8))

Number theory

rm.number_theory tests primality, factorises, finds the next or previous prime, gives Euler’s totient and exact factorials, and lists and counts primes, on Python integers of any size. Ctrl-C stops a long call:

nt = rm.number_theory
nt.factor(2**64 + 1), nt.prime_count(10**8), nt.next_prime(10**20)
([(274177, 1), (67280421310721, 1)], 5761455, 100000000000000000039)

rm.integer.encode and decode convert mixed-radix integers in int64. A result that does not fit raises OverflowError. rm.polynomial.decode and rm.exact.decode accept field-valued bases and digits.