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 subclass of
galois.FieldArray
and instance ofgalois.FieldClass
.- Parameters
array (array_like) – The input array to be converted to a Galois field array. The input array is copied, so the original array is unmodified by changes to the Galois field array. Valid input array types are
numpy.ndarray
,list
ortuple
of int or str,int
, orstr
.dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest valid 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 object is it’s an array.order ({
"K"
,"A"
,"C"
,"F"
}, optional) – Theorder
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.
Examples
This class is equivalent (and, in fact, identical) to the class returned from the Galois field array 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 # 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)
Constructors
Elements
([dtype])Creates a Galois field array of the field’s elements \(\{0, \dots, p^m-1\}\).
Identity
(size[, dtype])Creates an \(n \times n\) Galois field identity matrix.
Ones
(shape[, dtype])Creates a Galois field array with all ones.
Random
([shape, low, high, dtype])Creates a Galois field array with random field elements.
Range
(start, stop[, step, dtype])Creates a Galois field array with a range of field elements.
Vandermonde
(a, m, n[, dtype])Creates a \(m \times n\) Vandermonde matrix of \(a \in \mathrm{GF}(p^m)\).
Vector
(array[, dtype])Creates a Galois field array over \(\mathrm{GF}(p^m)\) from length-\(m\) vectors over the prime subfield \(\mathrm{GF}(p)\).
Zeros
(shape[, dtype])Creates a Galois field array with all zeros.
Methods
lu_decompose
()Decomposes the input array into the product of lower and upper triangular matrices.
lup_decompose
()Decomposes the input array into the product of lower and upper triangular matrices using partial pivoting.
row_reduce
([ncols])Performs Gaussian elimination on the matrix to achieve reduced row echelon form.
vector
([dtype])Converts the Galois field array over \(\mathrm{GF}(p^m)\) to length-\(m\) vectors over the prime subfield \(\mathrm{GF}(p)\).
- classmethod Elements(dtype=None)¶
Creates a 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 valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.- Returns
A Galois field array of all the field’s elements.
- Return type
Examples
In [10]: GF = galois.GF(31) In [11]: GF.Elements() Out[11]: GF([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], order=31)
- 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 valid 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 [12]: GF = galois.GF(31) In [13]: GF.Identity(4) Out[13]: 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 (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-dim array. An n-tuple, e.g.(M,N)
, represents an n-dim 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 valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of ones.
- Return type
Examples
In [14]: GF = galois.GF(31) In [15]: GF.Ones((2,5)) Out[15]: 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 (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-dim array. An n-tuple, e.g.(M,N)
, represents an n-dim array with each element indicating the size in each dimension.low (int, optional) – The lowest value (inclusive) of a random field element. The default is 0.
high (int, optional) – The highest value (exclusive) of a random field element. 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 isNone
which represents the smallest valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of random field elements.
- Return type
Examples
In [16]: GF = galois.GF(31) In [17]: GF.Random((2,5)) Out[17]: GF([[ 4, 15, 29, 24, 17], [16, 23, 13, 7, 3]], order=31)
- classmethod Range(start, stop, step=1, dtype=None)¶
Creates a Galois field array with a range of field elements.
- Parameters
start (int) – The starting value (inclusive).
stop (int) – The stopping value (exclusive).
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 valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of a range of field elements.
- Return type
Examples
In [18]: GF = galois.GF(31) In [19]: GF.Range(10,20) Out[19]: GF([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], order=31)
- classmethod Vandermonde(a, m, n, dtype=None)¶
Creates a \(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 valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
The \(m \times n\) Vandermonde matrix.
- Return type
Examples
In [20]: GF = galois.GF(2**3) In [21]: a = GF.primitive_element In [22]: V = GF.Vandermonde(a, 7, 7) In [23]: 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)\).
- 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)
.dtype (numpy.dtype, optional) – The
numpy.dtype
of the array elements. The default isNone
which represents the smallest valid 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 [24]: GF = galois.GF(2**6) In [25]: vec = galois.GF2.Random((3,6)); vec Out[25]: GF([[1, 1, 1, 0, 1, 0], [1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 0, 1]], order=2) In [26]: a = GF.Vector(vec); a Out[26]: GF([58, 39, 41], order=2^6) In [27]: with GF.display("poly"): ....: print(a) ....: GF([α^5 + α^4 + α^3 + α, α^5 + α^2 + α + 1, α^5 + α^3 + 1], order=2^6) In [28]: a.vector() Out[28]: GF([[1, 1, 1, 0, 1, 0], [1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 0, 1]], order=2)
- classmethod Zeros(shape, dtype=None)¶
Creates a Galois field array with all zeros.
- Parameters
shape (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-dim array. An n-tuple, e.g.(M,N)
, represents an n-dim 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 valid dtype for this class, i.e. the first element ingalois.FieldClass.dtypes
.
- Returns
A Galois field array of zeros.
- Return type
Examples
In [29]: GF = galois.GF(31) In [30]: GF.Zeros((2,5)) Out[30]: GF([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], order=31)