# Polynomials¶

Univariate polynomials over finite fields are supported with the Poly class.

## Create a polynomial¶

Create a polynomial by specifying its coefficients in degree-descending order and the finite field its over.

In : GF = galois.GF(2**8)

In : galois.Poly([1, 0, 0, 55, 23], field=GF)
Out: Poly(x^4 + 55x + 23, GF(2^8))


Or pass a FieldArray of coefficients without explicitly specifying the finite field.

In : coeffs = GF([1, 0, 0, 55, 23]); coeffs
Out: GF([ 1,  0,  0, 55, 23], order=2^8)

In : galois.Poly(coeffs)
Out: Poly(x^4 + 55x + 23, GF(2^8))

Use set_printoptions() to display the polynomial coefficients in degree-ascending order.
In : galois.set_printoptions(coeffs="asc")

In : galois.Poly(coeffs)
Out: Poly(23 + 55x + x^4, GF(2^8))


## Element representation¶

As with FieldArray instances, the finite field element representation of the polynomial coefficients may be changed by setting the repr keyword argument of GF() or using the repr() classmethod.

In : GF = galois.GF(3**5)

# Display f(x) using the default integer representation
In : f = galois.Poly([13, 0, 4, 2], field=GF); print(f)
13x^3 + 4x + 2

# Display f(x) using the polynomial representation
In : GF.repr("poly"); print(f)
(α^2 + α + 1)x^3 + (α + 1)x + 2

# Display f(x) using the power representation
In : GF.repr("power"); print(f)
(α^10)x^3 + (α^69)x + α^121

In : GF.repr("int");


See Element Representation for more details.

## Alternate constructors¶

There are several additional ways to create a polynomial. These alternate constructors are included as classmethods in Poly. By convention, alternate constructors use PascalCase while other classmethods use snake_case.

Create a polynomial by specifying its non-zero degrees and coefficients using Degrees().

In : galois.Poly.Degrees([1000, 1], coeffs=[1, 179], field=GF)
Out: Poly(x^1000 + 179x, GF(3^5))


Create a polynomial from its integer representation using Int(). Additionally, one may create a polynomial from a binary, octal, or hexadecimal string of its integer representation.

In : galois.Poly.Int(268, field=GF)
Out: Poly(x + 25, GF(3^5))

In : galois.Poly.Int(int("0b1011", 2))
Out: Poly(x^3 + x + 1, GF(2))

In : galois.Poly.Int(int("0o5034", 8), field=galois.GF(2**3))
Out: Poly(5x^3 + 3x + 4, GF(2^3))

In : galois.Poly.Int(int("0xf700a275", 16), field=galois.GF(2**8))
Out: Poly(247x^3 + 162x + 117, GF(2^8))


Create a polynomial from its string representation using Str().

In : galois.Poly.Str("x^5 + 143", field=GF)
Out: Poly(x^5 + 143, GF(3^5))


Create a polynomial from its roots using Roots().

In : f = galois.Poly.Roots([137, 22, 51], field=GF); f
Out: Poly(x^3 + 180x^2 + 19x + 58, GF(3^5))

In : f.roots()
Out: GF([ 22,  51, 137], order=3^5)


The Zero(), One(), and Identity() classmethods create common, simple polynomials. They are included for convenience.

In : galois.Poly.Zero(GF)
Out: Poly(0, GF(3^5))

In : galois.Poly.One(GF)
Out: Poly(1, GF(3^5))

In : galois.Poly.Identity(GF)
Out: Poly(x, GF(3^5))


Random polynomials of a given degree are easily created with Random().

In : galois.Poly.Random(4, field=GF)
Out: Poly(209x^4 + 186x^3 + 144x^2 + 56x + 165, GF(3^5))


## Methods¶

Polynomial objects have several methods that modify or perform operations on the polynomial. Below are some examples.

Compute the derivative of a polynomial using derivative().

In : GF = galois.GF(7)

In : f = galois.Poly([1, 0, 5, 2, 3], field=GF); f
Out: Poly(x^4 + 5x^2 + 2x + 3, GF(7))

In : f.derivative()
Out: Poly(4x^3 + 3x + 2, GF(7))


Compute the roots of a polynomial using roots().

In : f.roots()
Out: GF([5, 6], order=7)


## Properties¶

Polynomial objects have several instance properties. Below are some examples.

Find the non-zero degrees and coefficients of the polynomial using nonzero_degrees and nonzero_coeffs.

In : GF = galois.GF(7)

In : f = galois.Poly([1, 0, 3], field=GF); f
Out: Poly(x^2 + 3, GF(7))

In : f.nonzero_degrees
Out: array([2, 0])

In : f.nonzero_coeffs
Out: GF([1, 3], order=7)


Find the integer equivalent of the polynomial using int(), see __int__(). Additionally, one may convert a polynomial into the binary, octal, or hexadecimal string of its integer representation.

In : int(f)
Out: 52

In : g = galois.Poly([1, 0, 1, 1]); g
Out: Poly(x^3 + x + 1, GF(2))

In : bin(g)
Out: '0b1011'

In : g = galois.Poly([5, 0, 3, 4], field=galois.GF(2**3)); g
Out: Poly(5x^3 + 3x + 4, GF(2^3))

In : oct(g)
Out: '0o5034'

In : g = galois.Poly([0xf7, 0x00, 0xa2, 0x75], field=galois.GF(2**8)); g
Out: Poly(247x^3 + 162x + 117, GF(2^8))

In : hex(g)
Out: '0xf700a275'


Get the string representation of the polynomial using str().

In : str(f)
Out: 'x^2 + 3'


## Special polynomials¶

The galois library also includes several functions to find certain special polynomials. Below are some examples.

Find one or all irreducible polynomials with irreducible_poly() and irreducible_polys().

In : galois.irreducible_poly(3, 3)
Out: Poly(x^3 + 2x + 1, GF(3))

In : list(galois.irreducible_polys(3, 3))
Out:
[Poly(x^3 + 2x + 1, GF(3)),
Poly(x^3 + 2x + 2, GF(3)),
Poly(x^3 + x^2 + 2, GF(3)),
Poly(x^3 + x^2 + x + 2, GF(3)),
Poly(x^3 + x^2 + 2x + 1, GF(3)),
Poly(x^3 + 2x^2 + 1, GF(3)),
Poly(x^3 + 2x^2 + x + 1, GF(3)),
Poly(x^3 + 2x^2 + 2x + 2, GF(3))]


Find one or all primitive polynomials with primitive_poly() and primitive_polys().

In : galois.primitive_poly(3, 3)
Out: Poly(x^3 + 2x + 1, GF(3))

In : list(galois.primitive_polys(3, 3))
Out:
[Poly(x^3 + 2x + 1, GF(3)),
Poly(x^3 + x^2 + 2x + 1, GF(3)),
Poly(x^3 + 2x^2 + 1, GF(3)),
Poly(x^3 + 2x^2 + x + 1, GF(3))]


Find the Conway polynomial using conway_poly().

In : galois.conway_poly(3, 3)
Out: Poly(x^3 + 2x + 1, GF(3))