galois.FieldArray¶
- class galois.FieldArray(array, dtype=None, copy=True, order='K', ndmin=0)¶
Creates an array over \(\mathrm{GF}(p^m)\).
The
galois.FieldArray
class is a parent class for all Galois field array classes. Any Galois field \(\mathrm{GF}(p^m)\) with prime characteristic \(p\) and positive integer \(m\), can be constructed by calling the class factorygalois.GF(p**m)
.Warning
This is an abstract base class for all Galois field array classes.
galois.FieldArray
cannot be instantiated directly. Instead, Galois field array classes are created usinggalois.GF()
.For example, one can create the \(\mathrm{GF}(7)\) field array class as follows:
In [1]: GF7 = galois.GF(7) In [2]: print(GF7) <class 'numpy.ndarray over GF(7)'>
This subclass can then be used to instantiate arrays over \(\mathrm{GF}(7)\).
In [3]: GF7([3,5,0,2,1]) Out[3]: GF([3, 5, 0, 2, 1], order=7) In [4]: GF7.Random((2,5)) Out[4]: GF([[4, 5, 3, 2, 3], [5, 3, 1, 6, 5]], order=7)
galois.FieldArray
is a subclass ofnumpy.ndarray
. Thegalois.FieldArray
constructor has the same syntax asnumpy.array()
. The returnedgalois.FieldArray
object is an array that can be acted upon like any other numpy array.- 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.
- Returns
The copied input array as a \(\mathrm{GF}(p^m)\) field array.
- Return type
Examples
Construct various kinds of Galois fields using
galois.GF
.# Construct a GF(2^m) class In [5]: GF256 = galois.GF(2**8); print(GF256) <class 'numpy.ndarray over GF(2^8)'> # Construct a GF(p) class In [6]: GF571 = galois.GF(571); print(GF571) <class 'numpy.ndarray over GF(571)'> # Construct a very large GF(2^m) class In [7]: GF2m = galois.GF(2**100); print(GF2m) <class 'numpy.ndarray over GF(2^100)'> # Construct a very large GF(p) class In [8]: GFp = galois.GF(36893488147419103183); print(GFp) <class 'numpy.ndarray over GF(36893488147419103183)'>
Depending on the field’s order (size), only certain
dtype
values will be supported.In [9]: GF256.dtypes Out[9]: [numpy.uint8, numpy.uint16, numpy.uint32, numpy.int16, numpy.int32, numpy.int64] In [10]: GF571.dtypes Out[10]: [numpy.uint16, numpy.uint32, numpy.int16, numpy.int32, numpy.int64]
Very large fields, which can’t be represented using
np.int64
, can only be represented asdtype=np.object_
.In [11]: GF2m.dtypes Out[11]: [numpy.object_] In [12]: GFp.dtypes Out[12]: [numpy.object_]
Newly-created arrays will use the smallest, valid dtype.
In [13]: a = GF256.Random(10); a Out[13]: GF([ 68, 128, 248, 216, 156, 148, 241, 119, 101, 211], order=2^8) In [14]: a.dtype Out[14]: dtype('uint8')
This can be explicitly set by specifying the
dtype
keyword argument.In [15]: a = GF256.Random(10, dtype=np.uint32); a Out[15]: GF([160, 205, 161, 47, 89, 71, 77, 196, 131, 103], order=2^8) In [16]: a.dtype Out[16]: dtype('uint32')
Arrays can also be created explicitly by converting an “array-like” object.
# Construct a Galois field array from a list In [17]: l = [142, 27, 92, 253, 103]; l Out[17]: [142, 27, 92, 253, 103] In [18]: GF256(l) Out[18]: GF([142, 27, 92, 253, 103], order=2^8) # Construct a Galois field array from an existing numpy array In [19]: x_np = np.array(l, dtype=np.int64); x_np Out[19]: array([142, 27, 92, 253, 103]) In [20]: GF256(l) Out[20]: GF([142, 27, 92, 253, 103], order=2^8)
Arrays can also be created by “view casting” from an existing numpy array. This avoids a copy operation, which is especially useful for large data already brought into memory.
In [21]: a = x_np.view(GF256); a Out[21]: GF([142, 27, 92, 253, 103], order=2^8) # Changing `x_np` will change `a` In [22]: x_np[0] = 0; x_np Out[22]: array([ 0, 27, 92, 253, 103]) In [23]: a Out[23]: GF([ 0, 27, 92, 253, 103], order=2^8)
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
arithmetic_table
(operation[, mode])Generates the specified arithmetic table for the Galois field.
compile
(mode)Recompile the just-in-time compiled numba ufuncs for a new calculation mode.
display
([mode])Sets the display mode for all Galois field arrays of this type.
repr_table
([primitive_element])Generates an element representation table comparing the power, polynomial, vector, and integer representations.
Attributes
characteristic
degree
display_mode
is_prime_field
is_primitive_poly
name
order
prime_subfield
ufunc_mode
- classmethod Elements(dtype=None)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Elements() Out[2]: 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)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Identity(4) Out[2]: GF([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], order=31)
- classmethod Ones(shape, dtype=None)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Ones((2,5)) Out[2]: GF([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], order=31)
- classmethod Random(shape=(), low=0, high=None, dtype=None)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Random((2,5)) Out[2]: GF([[23, 30, 19, 0, 9], [28, 19, 12, 9, 24]], order=31)
- classmethod Range(start, stop, step=1, dtype=None)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Range(10,20) Out[2]: GF([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], order=31)
- classmethod Vandermonde(a, m, n, dtype=None)[source]¶
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 [1]: GF = galois.GF(2**3) In [2]: a = GF.primitive_element In [3]: V = GF.Vandermonde(a, 7, 7) In [4]: 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)[source]¶
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 [1]: GF = galois.GF(2**6) In [2]: vec = galois.GF2.Random((3,6)); vec Out[2]: GF([[0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0]], order=2) In [3]: a = GF.Vector(vec); a Out[3]: GF([ 2, 16, 16], order=2^6) In [4]: with GF.display("poly"): ...: print(a) ...: GF([α, α^4, α^4], order=2^6) In [5]: a.vector() Out[5]: GF([[0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0]], order=2)
- classmethod Zeros(shape, dtype=None)[source]¶
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 [1]: GF = galois.GF(31) In [2]: GF.Zeros((2,5)) Out[2]: GF([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], order=31)