Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Introduction

What is Algebraeon?

Algebraeon is a computer algebra system written in Rust.

This guide is for the Python module algebraeon which provides a more user-friendly interface to the capabilities of Algebraeon.

Stability

The API is subject to large breaking changes at this time. I hope to stabilize things more in the not too distant future.

Installation

The latest version can be installed with pip

pip install algebraeon

There are no other dependencies. Most platforms should be supported.

Numbers

Naturals, Integers, and Rationals

Algebraeon provides Nat, Int, and Rat types for representing natural numbers, integers, and rational numbers respectively.

Constructing Numbers

The types can all be constructed using the primitive int type.

from algebraeon import *

# Construct a `Nat` from non-negative primitive `int`s.
Nat(0)
Nat(7)

# ValueError because it is possible to construct a 
# `Nat` from a non-negative primitive `int`.
try:
    Nat(-5)
except ValueError:
    pass
else:
    raise Exception()

# Construct an `Int` from primitive `int`s.
Int(-7)
Int(0)
Int(7)

# Construct a `Rat` from primitive `int`s.
Rat(-7)
Rat(0)
Rat(7)
Rat(2, 3) # 2/3

Rational numbers can also be constructed from instaces of Fraction from the fractions module of the standard library.

from algebraeon import *
import fractions

assert(Rat(fractions.Fraction(3, 5)) == Rat(3) / Rat(5))

They can also be implicitly casted to larger sets, but not to smaller ones.

from algebraeon import *

# Creating numbers in a larger set from numbers in a smaller set.
Int(Nat(5))
Rat(Nat(5))
Rat(Int(-5))

# Numbers can be created from the same set too.
Nat(Nat(3))
Int(Int(-3))
Rat(Rat(3))

# TypeError because `Int` is a strictly larger set than 
# `Nat` so implicit conversion is not allowed.
try:
    Nat(Int(5))
except TypeError:
    pass
else:
    raise Exception()

Operations

The usual operations are defined for Algebraeon’s number types.

For operations involving more than one number, the type of the result is the largest of the input types. For example, adding a Nat to an Int produces an Int.

from algebraeon import *

# The type of the result is the largest of the input types.
assert((Nat(2) + Int(3)).set() == Int)

assert(Int(4) + 5 == Rat(9))

assert(Int(-3) ** 3 == -27)

Division exampes:

from algebraeon import *

# Integer division is ok as long as the result is an integer.
assert(Int(6) / Int(2) == 3)
try:
    Int(7) / Int(3)
except ValueError:
    pass
else:
    raise Exception()

# Rational division is ok, as long as we're not dividing by 0.
assert(Rat(6) / Rat(2) == 3)
assert(Rat(7) / Rat(3) == Rat(7, 3))

# Division by 0 raises a `ValueError`.
try:
    Int(2) / Int(0)
except ValueError:
    pass
else:
    raise Exception()

Polynomials

Integer Polynomials

from algebraeon import *

x = Int.polynomials().var()

assert((x + 2) ** 2 == x**2 + 4*x + 4)

assert((x**2 + 3*x + 2) / (x + 1) == x + 2)

# not divisible
try:
    x / (2 * x)
except ValueError:
    pass
else:
    raise Exception()

Rational Polynomials

from algebraeon import *

x = Rat.polynomials().var()

assert((x + 2) ** 2 == x**2 + 4*x + 4)

assert((x**2 + 3*x + 2) / (x + 1) == x + 2)

assert(x / (2 * x) == Rat(1, 2))

Polynomials Over the Natural Numbers

from algebraeon import *

x = Nat.polynomials().var()

assert((x + 2) ** 2 == x**2 + 4*x + 4)

Factoring

Algebraeon implements algorithms for factoring elements belonging to various domains.

Natural Numbers

Factoring natural numbers:

from algebraeon import *

assert(Nat(12).factor().primes()          == [2, 2, 3])
assert(Nat(12).factor().distinct_primes() == [2, 3])
assert(Nat(12).factor().powers()          == {2 : 2, 3 : 1})

assert(Nat(0).factor().primes()          is None)
assert(Nat(0).factor().distinct_primes() is None)
assert(Nat(0).factor().powers()          is None)

# We can factor numbers much bigger than a naive algorithm is capable of
assert(
    Nat(706000565581575429997696139445280900).factor().powers() 
    == {2: 2, 5: 2, 6988699669998001: 1, 1010203040506070809: 1}
)

Checking if a natural number is prime

from algebraeon import *

assert(not Nat(0).is_prime())
assert(not Nat(1).is_prime())
assert(Nat(2).is_prime())
assert(Nat(3).is_prime())
assert(not Nat(4).is_prime())

# The numbers from the factoring example above
assert(Nat(6988699669998001).is_prime())
assert(Nat(1010203040506070809).is_prime())
assert(not Nat(706000565581575429997696139445280900).is_prime())

Integers

Factoring integers:

from algebraeon import *

assert(Int(12).factor().sign()            == 1)
assert(Int(12).factor().primes()          == [2, 2, 3])
assert(Int(12).factor().distinct_primes() == [2, 3])
assert(Int(12).factor().powers()          == {2 : 2, 3 : 1})

assert(Int(-12).factor().sign()            == -1)
assert(Int(-12).factor().primes()          == [2, 2, 3])
assert(Int(-12).factor().distinct_primes() == [2, 3])
assert(Int(-12).factor().powers()          == {2 : 2, 3 : 1})

assert(Int(0).factor().sign()            == 0)
assert(Int(0).factor().primes()          is None)
assert(Int(0).factor().distinct_primes() is None)
assert(Int(0).factor().powers()          is None)

Checking if an integer is prime

from algebraeon import *

assert(not Int(-4).is_prime())
assert(Int(-3).is_prime())
assert(Int(-2).is_prime())
assert(not Int(-1).is_prime())
assert(not Int(0).is_prime())
assert(not Int(1).is_prime())
assert(Int(2).is_prime())
assert(Int(3).is_prime())
assert(not Int(4).is_prime())

Integer Polynomials

from algebraeon import *

x = Int.polynomials().var()

poly = -12 * x**2 + 60*x - 72
poly_factored = poly.factor()

print(f"poly                     =", poly)
print(f"poly_factored            =", poly_factored)
# A list of the irreducible factors with their multiplicity
print(f".powers()                =", poly_factored.powers())
# A list of the irreducible factors with repetitions
print(f".irreducibles()          =", poly_factored.irreducibles())
# A list of the irreducible factors without repetitions
print(f".distinct_irreducibles() =", poly_factored.distinct_irreducibles())
# The integer part of the factorisation
print(f".content()               =", poly_factored.content())
# The primitive polynomial part of the factorisation
print(f".primitive()             =", poly_factored.primitive())

"""
Output:
    poly                     = -12λ^2+60λ-72
    poly_factored            = -1 * (2)^2 * (3) * (λ-2) * (λ-3)
    .powers()                = [(Polynomial(2, Int), 2), (Polynomial(3, Int), 1), (Polynomial(λ-2, Int), 1), (Polynomial(λ-3, Int), 1)]
    .irreducibles()          = [Polynomial(2, Int), Polynomial(2, Int), Polynomial(3, Int), Polynomial(λ-2, Int), Polynomial(λ-3, Int)]
    .distinct_irreducibles() = [Polynomial(2, Int), Polynomial(3, Int), Polynomial(λ-2, Int), Polynomial(λ-3, Int)]
    .content()               = - 2^2 × 3
    .primitive()             = 1 * (λ-2) * (λ-3)
"""

Meta

Checking Versions

To check which version of the python library algebraeon is installed:

import algebraeon
assert(algebraeon.algebraeon_python_library_version() == "0.0.2")

To check which version of the Rust library for algebraeon is being used behind the scenes:

import algebraeon
assert(algebraeon.algebraeon_rust_library_version() == "0.0.16")