# galois.GF2¶

class galois.GF2(array, dtype=None, copy=True, order='K', ndmin=0)

Creates an array over $$\mathrm{GF}(2)$$.

This class is a pre-generated galois.FieldArray subclass generated with galois.GF(2) and is included in the API for convenience. See galois.FieldArray and galois.FieldClass for more complete documentation and examples.

Parameters
Returns

The copied input array as a Galois field array over $$\mathrm{GF}(2)$$.

Return type

galois.FieldArray

Examples

This class is equivalent (and, in fact, identical) to the class returned from the Galois field class constructor.

In : print(galois.GF2)
<class 'numpy.ndarray over GF(2)'>

In : GF2 = galois.GF(2); print(GF2)
<class 'numpy.ndarray over GF(2)'>

In : GF2 is galois.GF2
Out: True


The Galois field properties can be viewed by class attributes, see galois.FieldClass.

# View a summary of the field's properties
In : print(galois.GF2.properties)
GF(2):
characteristic: 2
degree: 1
order: 2
irreducible_poly: x + 1
is_primitive_poly: True
primitive_element: 1

# Or access each attribute individually
In : galois.GF2.irreducible_poly
Out: Poly(x + 1, GF(2))

In : galois.GF2.is_prime_field
Out: True


The class’s constructor mimics the call signature of numpy.array().

# Construct a Galois field array from an iterable
In : galois.GF2([1,0,1,1,0,0,0,1])
Out: GF([1, 0, 1, 1, 0, 0, 0, 1], order=2)

# Or an iterable of iterables
In : galois.GF2([[1,0], [1,1]])
Out:
GF([[1, 0],
[1, 1]], order=2)

# Or a single integer
In : galois.GF2(1)
Out: GF(1, order=2)

classmethod Elements(dtype=None)

Creates a 1-D Galois field array of the field’s elements $$\{0, \dots, p^m-1\}$$.

Parameters

dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A 1-D Galois field array of all the field’s elements.

Return type

galois.FieldArray

Examples

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

In : GF.Elements()
Out:
GF([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
order=2^4)


As usual, Galois field elements can be displayed in either the “integer” (default), “polynomial”, or “power” representation. This can be changed by calling galois.FieldClass.display().

# Permanently set the display mode to "poly"
In : GF.display("poly");

In : GF.Elements()
Out:
GF([0, 1, α, α + 1, α^2, α^2 + 1, α^2 + α, α^2 + α + 1, α^3, α^3 + 1,
α^3 + α, α^3 + α + 1, α^3 + α^2, α^3 + α^2 + 1, α^3 + α^2 + α,
α^3 + α^2 + α + 1], order=2^4)

# Temporarily set the display mode to "power"
In : with GF.display("power"):
....:     print(GF.Elements())
....:
GF([0, 1, α, α^4, α^2, α^8, α^5, α^10, α^3, α^14, α^9, α^7, α^6, α^13,
α^11, α^12], order=2^4)

# Reset the display mode to "int"
In : GF.display();

classmethod Identity(size, dtype=None)

Creates an $$n \times n$$ Galois field identity matrix.

Parameters
Returns

A Galois field identity matrix of shape (size, size).

Return type

galois.FieldArray

Examples

In : GF = galois.GF(31)

In : GF.Identity(4)
Out:
GF([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], order=31)

classmethod Ones(shape, dtype=None)

Creates a Galois field array with all ones.

Parameters
• shape (int, tuple) – A numpy-compliant shape tuple, see numpy.ndarray.shape. An empty tuple () represents a scalar. A single integer or 1-tuple, e.g. N or (N,), represents the size of a 1-D array. A 2-tuple, e.g. (M,N), represents a 2-D array with each element indicating the size in each dimension.

• dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A Galois field array of ones.

Return type

galois.FieldArray

Examples

In : GF = galois.GF(31)

In : GF.Ones((2,5))
Out:
GF([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], order=31)

classmethod Random(shape=(), low=0, high=None, dtype=None)

Creates a Galois field array with random field elements.

Parameters
• shape (int, tuple) – A numpy-compliant shape tuple, see numpy.ndarray.shape. An empty tuple () represents a scalar. A single integer or 1-tuple, e.g. N or (N,), represents the size of a 1-D array. A 2-tuple, e.g. (M,N), represents a 2-D array with each element indicating the size in each dimension.

• low (int, optional) – The lowest value (inclusive) of a random field element in its integer representation. The default is 0.

• high (int, optional) – The highest value (exclusive) of a random field element in its integer representation. The default is None which represents the field’s order $$p^m$$.

• dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A Galois field array of random field elements.

Return type

galois.FieldArray

Examples

In : GF = galois.GF(31)

In : GF.Random((2,5))
Out:
GF([[18, 10,  9,  4, 20],
[12, 25,  6, 20, 12]], order=31)

classmethod Range(start, stop, step=1, dtype=None)

Creates a 1-D Galois field array with a range of field elements.

Parameters
• start (int) – The starting Galois field value (inclusive) in its integer representation.

• stop (int) – The stopping Galois field value (exclusive) in its integer representation.

• step (int, optional) – The space between values. The default is 1.

• dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A 1-D Galois field array of a range of field elements.

Return type

galois.FieldArray

Examples

In : GF = galois.GF(31)

In : GF.Range(10,20)
Out: GF([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], order=31)

classmethod Vandermonde(a, m, n, dtype=None)

Creates an $$m \times n$$ Vandermonde matrix of $$a \in \mathrm{GF}(p^m)$$.

Parameters
Returns

The $$m \times n$$ Vandermonde matrix.

Return type

galois.FieldArray

Examples

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

In : a = GF.primitive_element

In : V = GF.Vandermonde(a, 7, 7)

In : with GF.display("power"):
....:     print(V)
....:
GF([[  1,   1,   1,   1,   1,   1,   1],
[  1,   α, α^2, α^3, α^4, α^5, α^6],
[  1, α^2, α^4, α^6,   α, α^3, α^5],
[  1, α^3, α^6, α^2, α^5,   α, α^4],
[  1, α^4,   α, α^5, α^2, α^6, α^3],
[  1, α^5, α^3,   α, α^6, α^4, α^2],
[  1, α^6, α^5, α^4, α^3, α^2,   α]], order=2^3)

classmethod Vector(array, dtype=None)

Creates a Galois field array over $$\mathrm{GF}(p^m)$$ from length-$$m$$ vectors over the prime subfield $$\mathrm{GF}(p)$$.

This function is the inverse operation of the vector() method.

Parameters
• array (array_like) – The input array with field elements in $$\mathrm{GF}(p)$$ to be converted to a Galois field array in $$\mathrm{GF}(p^m)$$. The last dimension of the input array must be $$m$$. An input array with shape (n1, n2, m) has output shape (n1, n2). By convention, the vectors are ordered from highest degree to 0-th degree.

• dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A Galois field array over $$\mathrm{GF}(p^m)$$.

Return type

galois.FieldArray

Examples

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

In : vec = galois.GF2.Random((3,6)); vec
Out:
GF([[0, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 1, 1]], order=2)

In : a = GF.Vector(vec); a
Out: GF([25,  7, 11], order=2^6)

In : with GF.display("poly"):
....:     print(a)
....:
GF([α^4 + α^3 + 1, α^2 + α + 1, α^3 + α + 1], order=2^6)

In : a.vector()
Out:
GF([[0, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 1, 1]], order=2)

classmethod Zeros(shape, dtype=None)

Creates a Galois field array with all zeros.

Parameters
• shape (int, tuple) – A numpy-compliant shape tuple, see numpy.ndarray.shape. An empty tuple () represents a scalar. A single integer or 1-tuple, e.g. N or (N,), represents the size of a 1-D array. A 2-tuple, e.g. (M,N), represents a 2-D array with each element indicating the size in each dimension.

• dtype (numpy.dtype, optional) – The numpy.dtype of the array elements. The default is None which represents the smallest unsigned dtype for this class, i.e. the first element in galois.FieldClass.dtypes.

Returns

A Galois field array of zeros.

Return type

galois.FieldArray

Examples

In : GF = galois.GF(31)

In : GF.Zeros((2,5))
Out:
GF([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]], order=31)