galois.Poly

class galois.Poly(coeffs, field=None, order='desc')[source]

Bases: object

A polynomial class with coefficients in any Galois field.

Parameters
  • coeffs (array_like) – List of polynomial coefficients of type Galois field array, np.ndarray, list, or tuple. The first element is the highest-degree element if order="desc" or the first element is the 0-th degree element if order="asc".

  • field (galois.GF, optional) – Optionally specify the field to which the coefficients belong. The default field is galois.GF2. If coeffs is a Galois field array, then that field is used and the field parameter is ignored.

  • order (str, optional) – The interpretation of the coefficient degrees, either "desc" (default) or "asc". For "desc", the first element of coeffs is the highest degree coefficient (x^(N-1)) and the last element is the 0-th degree element (x^0).

Examples

Create polynomials over GF(2)

# Construct a polynominal over GF(2)
In [6]: a = galois.Poly([1,0,1,1]); a
Out[6]: Poly(x^3 + x + 1, GF2)

# Construct the same polynomial by only specifying its non-zero coefficients
In [7]: b = galois.Poly.NonZero([1,1,1], [3,1,0]); b
Out[7]: Poly(x^3 + x + 1, GF2)

Create polynomials over GF(7)

# Construct the GF(7) field
In [8]: GF = galois.GF_factory(7, 1)

# Construct a polynominal over GF(7)
In [9]: galois.Poly([4,0,3,0,0,2], field=GF)
Out[9]: Poly(4x^5 + 3x^3 + 2, GF7)

# Construct the same polynomial by only specifying its non-zero coefficients
In [10]: galois.Poly.NonZero([4,3,2], [5,3,0], field=GF)
Out[10]: Poly(4x^5 + 3x^3 + 2, GF7)

Polynomial arithmetic

In [11]: a = galois.Poly([1,0,6,3], field=GF); a
Out[11]: Poly(x^3 + 6x + 3, GF7)

In [12]: b = galois.Poly([2,0,2], field=GF); b
Out[12]: Poly(2x^2 + 2, GF7)

In [13]: a + b
Out[13]: Poly(x^3 + 2x^2 + 6x + 5, GF7)

In [14]: a - b
Out[14]: Poly(x^3 + 5x^2 + 6x + 1, GF7)

# Compute the quotient of the polynomial division
In [15]: a / b
Out[15]: Poly(4x, GF7)

# True division and floor division are equivalent
In [16]: a / b == a // b
Out[16]: True

# Compute the remainder of the polynomial division
In [17]: a % b
Out[17]: Poly(5x + 3, GF7)
classmethod Decimal(decimal, field=<class 'galois.gf2.GF2'>, order='desc')[source]
classmethod NonZero(coeffs, degrees, field=<class 'galois.gf2.GF2'>)[source]

Examples

# Construct a polynomial over GF2 only specifying the non-zero terms
In [18]: a = galois.Poly.NonZero([1,1,1], [3,1,0]); a
Out[18]: Poly(x^3 + x + 1, GF2)
static divmod(dividend, divisor)[source]
property coeffs

The polynomial coefficients as a Galois field array. Coefficients are \([a_{N-1}, \dots, a_1, a_0]\) if order="desc" or \([a_0, a_1, \dots, a_{N-1}]\) if order="asc", where \(p(x) = a_{N-1}x^{N-1} + \dots + a_1x + a_0\).

Type

galois.GF2, galois.GF2m, galois.GFp, galois.GFpm

property coeffs_asc

The polynomial coefficients \([a_0, a_1, \dots, a_{N-1}]\) as a Galois field array in exponent-ascending order, where \(p(x) = a_{N-1}x^{N-1} + \dots + a_1x + a_0\).

Type

galois.GF2, galois.GF2m, galois.GFp, galois.GFpm

property coeffs_desc

The polynomial coefficients \([a_{N-1}, \dots, a_1, a_0]\) as a Galois field array in exponent-ascending order, where \(p(x) = a_{N-1}x^{N-1} + \dots + a_1x + a_0\).

Type

galois.GF2, galois.GF2m, galois.GFp, galois.GFpm

property decimal

The integer representation of the polynomial. For \(p(x) = a_{N-1}x^{N-1} + \dots + a_1x + a_0\) with elements in \(\mathrm{GF}(q)\), the decimal representation is \(d = a_{N-1} q^{N-1} + \dots + a_1 q + a_0\) (using integer arithmetic, not field arithmetic) where \(q\) is the field order.

Type

int

property degree

The degree of the polynomial, i.e. the highest degree with non-zero coefficient.

Type

int

property field

The finite field to which the coefficients belong.

Type

galois.GF2, galois.GF2m, galois.GFp, galois.GFpm

property order

The interpretation of the ordering of the polynomial coefficients. coeffs are in exponent-descending order if order="desc" and in exponent-ascending order if order="asc".

Type

str