galois.GF2
- class galois.GF2(array, dtype=None, copy=True, order='K', ndmin=0)
An array over \(\mathrm{GF}(2)\).
This class is a pre-generated
galois.FieldArray
subclass generated withgalois.GF(2)
and is included in the API for convenience. Seegalois.FieldArray
andgalois.FieldClass
for more complete documentation and examples.Examples
This class is equivalent (and, in fact, identical) to the class returned from the Galois field class constructor.
In [1]: print(galois.GF2) <class 'numpy.ndarray over GF(2)'> In [2]: GF2 = galois.GF(2); print(GF2) <class 'numpy.ndarray over GF(2)'> In [3]: GF2 is galois.GF2 Out[3]: True
The Galois field properties can be viewed by class attributes, see
galois.FieldClass
.# View a summary of the field's properties In [4]: 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 [5]: galois.GF2.irreducible_poly Out[5]: Poly(x + 1, GF(2)) In [6]: galois.GF2.is_prime_field Out[6]: True
The class’s constructor mimics the call signature of
numpy.array()
.# Construct a Galois field array from an iterable In [7]: galois.GF2([1,0,1,1,0,0,0,1]) Out[7]: GF([1, 0, 1, 1, 0, 0, 0, 1], order=2) # Or an iterable of iterables In [8]: galois.GF2([[1,0], [1,1]]) Out[8]: GF([[1, 0], [1, 1]], order=2) # Or a single integer In [9]: galois.GF2(1) Out[9]: 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 isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.- Returns
A 1-D Galois field array of all the field’s elements.
- Return type
Examples
In [10]: GF = galois.GF(2**4) In [11]: GF.Elements() Out[11]: 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 [12]: GF.display("poly"); In [13]: GF.Elements() Out[13]: 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 [14]: 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 [15]: GF.display();
- classmethod Identity(size, dtype=None)
Creates an \(n \times n\) Galois field identity matrix.
- Parameters
size (int) – The size \(n\) along one axis of the matrix. The resulting array has shape
(size, size)
.dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field identity matrix of shape
(size, size)
.- Return type
Examples
In [16]: GF = galois.GF(31) In [17]: GF.Identity(4) Out[17]: 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, seenumpy.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 isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of ones.
- Return type
Examples
In [18]: GF = galois.GF(31) In [19]: GF.Ones((2,5)) Out[19]: GF([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], order=31)
- classmethod Random(shape=(), low=0, high=None, seed=None, dtype=None)
Creates a Galois field array with random field elements.
- Parameters
shape (int, tuple) – A numpy-compliant
shape
tuple, seenumpy.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\).seed (int, numpy.random.Generator, optional) – Non-negative integer used to initialize the PRNG. The default is
None
which means that unpredictable entropy will be pulled from the OS to be used as the seed. Anumpy.random.Generator
can also be passed. If so, it is used directly whendtype != np.object_
. Its state is used to seedrandom.seed()
, otherwise.dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of random field elements.
- Return type
Examples
Generate a random matrix with an unpredictable seed.
In [20]: GF = galois.GF(31) In [21]: GF.Random((2,5)) Out[21]: GF([[12, 1, 5, 14, 15], [15, 9, 14, 22, 2]], order=31)
Generate a random array with a specified seed. This produces repeatable outputs.
In [22]: GF.Random(10, seed=123456789) Out[22]: GF([ 7, 29, 20, 27, 18, 5, 2, 0, 24, 24], order=31) In [23]: GF.Random(10, seed=123456789) Out[23]: GF([ 7, 29, 20, 27, 18, 5, 2, 0, 24, 24], order=31)
Generate a group of random arrays with one global seed.
In [24]: rng = np.random.default_rng(123456789) In [25]: GF.Random(10, seed=rng) Out[25]: GF([ 7, 29, 20, 27, 18, 5, 2, 0, 24, 24], order=31) In [26]: GF.Random(10, seed=rng) Out[26]: GF([20, 15, 3, 28, 22, 0, 5, 10, 1, 0], 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 isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A 1-D Galois field array of a range of field elements.
- Return type
Examples
In [27]: GF = galois.GF(31) In [28]: GF.Range(10,20) Out[28]: 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
a (int, galois.FieldArray) – An element of \(\mathrm{GF}(p^m)\).
m (int) – The number of rows in the Vandermonde matrix.
n (int) – The number of columns in the Vandermonde matrix.
dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
The \(m \times n\) Vandermonde matrix.
- Return type
Examples
In [29]: GF = galois.GF(2**3) In [30]: a = GF.primitive_element In [31]: V = GF.Vandermonde(a, 7, 7) In [32]: 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 isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array over \(\mathrm{GF}(p^m)\).
- Return type
Examples
In [33]: GF = galois.GF(2**6) In [34]: vec = galois.GF2.Random((3,6)); vec Out[34]: GF([[1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0]], order=2) In [35]: a = GF.Vector(vec); a Out[35]: GF([62, 17, 36], order=2^6) In [36]: with GF.display("poly"): ....: print(a) ....: GF([α^5 + α^4 + α^3 + α^2 + α, α^4 + 1, α^5 + α^2], order=2^6) In [37]: a.vector() Out[37]: GF([[1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0]], order=2)
- classmethod Zeros(shape, dtype=None)
Creates a Galois field array with all zeros.
- Parameters
shape (int, tuple) – A numpy-compliant
shape
tuple, seenumpy.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 isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of zeros.
- Return type
Examples
In [38]: GF = galois.GF(31) In [39]: GF.Zeros((2,5)) Out[39]: GF([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], order=31)
- __init__(array, dtype=None, copy=True, order='K', ndmin=0)
Creates an array over \(\mathrm{GF}(p^m)\).
- Parameters
array (int, str, tuple, list, numpy.ndarray, galois.FieldArray) –
The input array-like object to be converted to a Galois field array. See the examples section for demonstations of array creation using each input type. See see
galois.FieldClass.display()
andgalois.FieldClass.display_mode
for a description of the “integer” and “polynomial” representation of Galois field elements.int
: A single integer, which is the “integer representation” of a Galois field element, creates a 0-D array.str
: A single string, which is the “polynomial representation” of a Galois field element, creates a 0-D array.tuple
,list
: A list or tuple (or nested lists/tuples) of ints or strings (which can be mix-and-matched) creates an array of
Galois field elements from their integer or polynomial representations. *
numpy.ndarray
,galois.FieldArray
: An array of ints creates a copy of the array over this specific field.dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest unsigned dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.copy (bool, optional) – The
copy
keyword argument fromnumpy.array()
. The default isTrue
which makes a copy of the input array.order (str, optional) – The
order
keyword argument fromnumpy.array()
. Valid values are"K"
(default),"A"
,"C"
, or"F"
.ndmin (int, optional) – The
ndmin
keyword argument fromnumpy.array()
. The minimum number of dimensions of the output. The default is 0.
- Returns
An array over \(\mathrm{GF}(p^m)\).
- Return type