Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

algebraic numbers library

Project description

Algebraic Numbers Library

Use when you need exact arithmetic, speed is not critical, and rational numbers aren't good enough.


use algebraics::prelude::*;
use algebraics::RealAlgebraicNumber as Number;

let two = Number::from(2);

// 2 is a rational number

// 1/2 is the reciprocal of 2
let one_half = two.recip();

// 1/2 is also a rational number

// 2^(1/4)
let root = (&two).pow((1, 4));

// we can use all the standard comparison operators
assert!(root != Number::from(3));
assert!(root < Number::from(2));
assert!(root > Number::from(1));

// we can use all of add, subtract, multiply, divide, and remainder
let sum = &root + &root;
let difference = &root - Number::from(47);
let product = &root * &one_half;
let quotient = &one_half / &root;
let remainder = &root % &one_half;

// root is not a rational number

// the calculations are always exact
assert_eq!((&root).pow(4), two);

// lets compute 30 decimal places of root
let scale = Number::from(10).pow(30);
let scaled = &root * scale;
let digits = scaled.into_integer_trunc();

// get the minimal polynomial
let other_number = root + two.pow((1, 2));
    "2 + -8*X + -4*X^2 + 0*X^3 + 1*X^4"

// works with really big numbers
let really_big = Number::from(1_00000_00000i64).pow(20) + Number::from(23);

Python support

Using algebraics from Python:

python3 -m pip install algebraics
from algebraics import RealAlgebraicNumber
sqrt_2 = 2 ** (RealAlgebraicNumber(1) / 2)
assert sqrt_2 * sqrt_2 == 2

Using algebraics in your own Rust project:

version = "0.2"

Developing algebraics:

cargo install maturin
maturin develop --cargo-extra-args="--features python-extension"

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for algebraics, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size algebraics-0.2.0-cp36-cp36m-manylinux1_x86_64.whl (428.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size algebraics-0.2.0.tar.gz (131.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page