numpy¶
Documentation of some native numpy functions when called on Galois field arrays.
General¶
|
Returns a copy of a given Galois field array. |
|
Concatenates the input arrays along the given axis. |
|
Inserts values along the given axis. |
Arithmetic¶
|
Adds two Galois field arrays element-wise. |
|
Subtracts two Galois field arrays element-wise. |
|
Multiplies two Galois field arrays element-wise. |
|
Divides two Galois field arrays element-wise. |
|
Returns the element-wise additive inverse of a Galois field array. |
Returns the element-wise multiplicative inverse of a Galois field array. |
|
|
Exponentiates a Galois field array element-wise. |
|
Squares a Galois field array element-wise. |
|
Computes the logarithm (base |
|
Computes the matrix multiplication of two Galois field arrays. |
Advanced Arithmetic¶
|
Convolves the input arrays. |
Linear Algebra¶
|
Returns the sum along the diagonal of a Galois field array. |
|
Computes the matrix multiplication of two Galois field arrays. |
|
Returns the rank of a Galois field matrix. |
|
Raises a square Galois field matrix to an integer power. |
|
Computes the determinant of the matrix. |
|
Computes the inverse of the matrix. |
|
Solves the system of linear equations. |
- np.add(x, y)[source]¶
Adds two Galois field arrays element-wise.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([27, 22, 28, 10, 24, 5, 26, 11, 22, 5], order=31) In [3]: y = GF.Random(10); y Out[3]: GF([ 9, 19, 22, 0, 24, 28, 1, 13, 14, 8], order=31) In [4]: np.add(x, y) Out[4]: GF([ 5, 10, 19, 10, 17, 2, 27, 24, 5, 13], order=31) In [5]: x + y Out[5]: GF([ 5, 10, 19, 10, 17, 2, 27, 24, 5, 13], order=31)
- np.concatenate(arrays, axis=0)[source]¶
Concatenates the input arrays along the given axis.
See: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
Examples
In [1]: GF = galois.GF(2**3) In [2]: A = GF.Random((2,2)); A Out[2]: GF([[4, 5], [2, 0]], order=2^3) In [3]: B = GF.Random((2,2)); B Out[3]: GF([[1, 2], [2, 4]], order=2^3) In [4]: np.concatenate((A,B), axis=0) Out[4]: GF([[4, 5], [2, 0], [1, 2], [2, 4]], order=2^3) In [5]: np.concatenate((A,B), axis=1) Out[5]: GF([[4, 5, 1, 2], [2, 0, 2, 4]], order=2^3)
- np.convolve(a, b)[source]¶
Convolves the input arrays.
See: https://numpy.org/doc/stable/reference/generated/numpy.convolve.html
Examples
In [1]: GF = galois.GF(31) In [2]: a = GF.Random(10) In [3]: b = GF.Random(10) In [4]: np.convolve(a, b) Out[4]: GF([ 6, 23, 7, 28, 7, 23, 3, 1, 28, 2, 8, 21, 14, 14, 13, 1, 13, 7, 22], order=31) # Equivalent implementation with native numpy In [5]: np.convolve(a.view(np.ndarray).astype(int), b.view(np.ndarray).astype(int)) % 31 Out[5]: array([ 6, 23, 7, 28, 7, 23, 3, 1, 28, 2, 8, 21, 14, 14, 13, 1, 13, 7, 22])
In [6]: GF = galois.GF(2**8) In [7]: a = GF.Random(10) In [8]: b = GF.Random(10) In [9]: np.convolve(a, b) Out[9]: GF([ 1, 206, 35, 179, 47, 180, 94, 133, 162, 148, 137, 12, 131, 199, 24, 208, 12, 85, 77], order=2^8)
- np.divide(x, y)[source]¶
Divides two Galois field arrays element-wise.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([27, 9, 16, 12, 5, 2, 5, 9, 12, 30], order=31) In [3]: y = GF.Random(10, low=1); y Out[3]: GF([ 3, 6, 25, 13, 3, 22, 1, 13, 18, 1], order=31) In [4]: z = np.divide(x, y); z Out[4]: GF([ 9, 17, 18, 20, 12, 17, 5, 15, 11, 30], order=31) In [5]: y * z Out[5]: GF([27, 9, 16, 12, 5, 2, 5, 9, 12, 30], order=31)
In [6]: np.true_divide(x, y) Out[6]: GF([ 9, 17, 18, 20, 12, 17, 5, 15, 11, 30], order=31) In [7]: x / y Out[7]: GF([ 9, 17, 18, 20, 12, 17, 5, 15, 11, 30], order=31) In [8]: np.floor_divide(x, y) Out[8]: GF([ 9, 17, 18, 20, 12, 17, 5, 15, 11, 30], order=31) In [9]: x // y Out[9]: GF([ 9, 17, 18, 20, 12, 17, 5, 15, 11, 30], order=31)
- np.insert(array, object, values, axis=None)[source]¶
Inserts values along the given axis.
See: https://numpy.org/doc/stable/reference/generated/numpy.insert.html
Examples
In [1]: GF = galois.GF(2**3) In [2]: x = GF.Random(5); x Out[2]: GF([4, 6, 5, 7, 0], order=2^3) In [3]: np.insert(x, 1, [0,1,2,3]) Out[3]: GF([4, 0, 1, 2, 3, 6, 5, 7, 0], order=2^3)
- np.log(x)[source]¶
Computes the logarithm (base
GF.primitive_element
) of a Galois field array element-wise.Calling
np.log()
implicitly uses basegalois.GFMeta.primitive_element
. Seegalois.GFArray.log()
for logarithm with arbitrary base.References
Examples
In [1]: GF = galois.GF(31) In [2]: alpha = GF.primitive_element; alpha Out[2]: GF(3, order=31) In [3]: x = GF.Random(10, low=1); x Out[3]: GF([22, 18, 8, 3, 25, 16, 12, 6, 13, 12], order=31) In [4]: y = np.log(x); y Out[4]: array([17, 26, 12, 1, 10, 6, 19, 25, 11, 19]) In [5]: alpha ** y Out[5]: GF([22, 18, 8, 3, 25, 16, 12, 6, 13, 12], order=31)
- np.matmul(x1, x2)[source]¶
Computes the matrix multiplication of two Galois field arrays.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x1 = GF.Random((3,4)); x1 Out[2]: GF([[10, 19, 11, 25], [17, 24, 11, 7], [10, 8, 21, 11]], order=31) In [3]: x2 = GF.Random((4,5)); x2 Out[3]: GF([[ 9, 14, 12, 25, 15], [20, 3, 27, 30, 13], [17, 3, 23, 3, 22], [24, 28, 3, 27, 7]], order=31) In [4]: np.matmul(x1, x2) Out[4]: GF([[17, 0, 0, 9, 8], [27, 12, 10, 3, 21], [ 3, 8, 15, 13, 18]], order=31) In [5]: x1 @ x2 Out[5]: GF([[17, 0, 0, 9, 8], [27, 12, 10, 3, 21], [ 3, 8, 15, 13, 18]], order=31)
- np.multiply(x, y)[source]¶
Multiplies two Galois field arrays element-wise.
References
Examples
Multiplying two Galois field arrays results in field multiplication.
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([19, 30, 3, 2, 11, 17, 14, 1, 25, 5], order=31) In [3]: y = GF.Random(10); y Out[3]: GF([11, 25, 8, 28, 7, 4, 8, 21, 0, 26], order=31) In [4]: np.multiply(x, y) Out[4]: GF([23, 6, 24, 25, 15, 6, 19, 21, 0, 6], order=31) In [5]: x * y Out[5]: GF([23, 6, 24, 25, 15, 6, 19, 21, 0, 6], order=31)
Multiplying a Galois field array with an integer results in scalar multiplication.
In [6]: GF = galois.GF(31) In [7]: x = GF.Random(10); x Out[7]: GF([ 4, 19, 2, 4, 3, 15, 10, 22, 3, 15], order=31) In [8]: np.multiply(x, 3) Out[8]: GF([12, 26, 6, 12, 9, 14, 30, 4, 9, 14], order=31) In [9]: x * 3 Out[9]: GF([12, 26, 6, 12, 9, 14, 30, 4, 9, 14], order=31)
In [10]: print(GF.properties) GF(31): structure: Finite Field characteristic: 31 degree: 1 order: 31 irreducible_poly: Poly(x + 28, GF(31)) is_primitive_poly: True primitive_element: GF(3, order=31) # Adding `characteristic` copies of any element always results in zero In [11]: x * GF.characteristic Out[11]: GF([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], order=31)
- np.negative(x)[source]¶
Returns the element-wise additive inverse of a Galois field array.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([19, 1, 20, 4, 25, 10, 28, 20, 18, 13], order=31) In [3]: y = np.negative(x); y Out[3]: GF([12, 30, 11, 27, 6, 21, 3, 11, 13, 18], order=31) In [4]: x + y Out[4]: GF([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], order=31)
In [5]: -x Out[5]: GF([12, 30, 11, 27, 6, 21, 3, 11, 13, 18], order=31) In [6]: -1*x Out[6]: GF([12, 30, 11, 27, 6, 21, 3, 11, 13, 18], order=31)
- np.power(x, y)[source]¶
Exponentiates a Galois field array element-wise.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([ 9, 12, 20, 22, 6, 25, 9, 13, 24, 27], order=31) In [3]: np.power(x, 3) Out[3]: GF([16, 23, 2, 15, 30, 1, 16, 27, 29, 29], order=31) In [4]: x ** 3 Out[4]: GF([16, 23, 2, 15, 30, 1, 16, 27, 29, 29], order=31) In [5]: x * x * x Out[5]: GF([16, 23, 2, 15, 30, 1, 16, 27, 29, 29], order=31)
In [6]: x = GF.Random(10, low=1); x Out[6]: GF([18, 4, 14, 29, 18, 28, 19, 10, 7, 18], order=31) In [7]: y = np.random.randint(-10, 10, 10); y Out[7]: array([-10, 9, 8, 0, 9, 0, -10, 1, 5, -6]) In [8]: np.power(x, y) Out[8]: GF([25, 8, 18, 1, 2, 1, 5, 10, 5, 2], order=31) In [9]: x ** y Out[9]: GF([25, 8, 18, 1, 2, 1, 5, 10, 5, 2], order=31)
- np.reciprocal(x)[source]¶
Returns the element-wise multiplicative inverse of a Galois field array.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(5, low=1); x Out[2]: GF([18, 12, 9, 7, 4], order=31) In [3]: y = np.reciprocal(x); y Out[3]: GF([19, 13, 7, 9, 8], order=31) In [4]: x * y Out[4]: GF([1, 1, 1, 1, 1], order=31)
In [5]: x ** -1 Out[5]: GF([19, 13, 7, 9, 8], order=31) In [6]: GF(1) / x Out[6]: GF([19, 13, 7, 9, 8], order=31) In [7]: GF(1) // x Out[7]: GF([19, 13, 7, 9, 8], order=31)
- np.square(x)[source]¶
Squares a Galois field array element-wise.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([ 6, 15, 17, 7, 2, 30, 13, 8, 6, 7], order=31) In [3]: np.square(x) Out[3]: GF([ 5, 8, 10, 18, 4, 1, 14, 2, 5, 18], order=31) In [4]: x ** 2 Out[4]: GF([ 5, 8, 10, 18, 4, 1, 14, 2, 5, 18], order=31) In [5]: x * x Out[5]: GF([ 5, 8, 10, 18, 4, 1, 14, 2, 5, 18], order=31)
- np.subtract(x, y)[source]¶
Subtracts two Galois field arrays element-wise.
References
Examples
In [1]: GF = galois.GF(31) In [2]: x = GF.Random(10); x Out[2]: GF([ 2, 10, 1, 5, 21, 27, 10, 3, 2, 17], order=31) In [3]: y = GF.Random(10); y Out[3]: GF([26, 11, 12, 15, 29, 26, 0, 15, 11, 20], order=31) In [4]: np.subtract(x, y) Out[4]: GF([ 7, 30, 20, 21, 23, 1, 10, 19, 22, 28], order=31) In [5]: x - y Out[5]: GF([ 7, 30, 20, 21, 23, 1, 10, 19, 22, 28], order=31)