numpy

Documentation of some native numpy functions when called on Galois field arrays.

General

np.copy(a)

Returns a copy of a given Galois field array.

np.concatenate(arrays[, axis])

Concatenates the input arrays along the given axis.

np.insert(array, object, values[, axis])

Inserts values along the given axis.

Arithmetic

np.add(x, y)

Adds two Galois field arrays element-wise.

np.subtract(x, y)

Subtracts two Galois field arrays element-wise.

np.multiply(x, y)

Multiplies two Galois field arrays element-wise.

np.divide(x, y)

Divides two Galois field arrays element-wise.

np.negative(x)

Returns the element-wise additive inverse of a Galois field array.

np.reciprocal(x)

Returns the element-wise multiplicative inverse of a Galois field array.

np.power(x, y)

Exponentiates a Galois field array element-wise.

np.square(x)

Squares a Galois field array element-wise.

np.log(x)

Computes the logarithm (base GF.primitive_element) of a Galois field array element-wise.

np.matmul(a, b)

Returns the matrix multiplication of two Galois field arrays.

Advanced Arithmetic

np.convolve(a, b)

Convolves the input arrays.

Linear Algebra

np.dot(a, b)

Returns the dot product of two Galois field arrays.

np.vdot(a, b)

Returns the dot product of two Galois field vectors.

np.inner(a, b)

Returns the inner product of two Galois field arrays.

np.outer(a, b)

Returns the outer product of two Galois field arrays.

np.matmul(a, b)

Returns the matrix multiplication of two Galois field arrays.

np.linalg.matrix_power(x)

Raises a square Galois field matrix to an integer power.

np.linalg.det(A)

Computes the determinant of the matrix.

np.linalg.matrix_rank(x)

Returns the rank of a Galois field matrix.

np.trace(x)

Returns the sum along the diagonal of a Galois field array.

np.linalg.solve(x)

Solves the system of linear equations.

np.linalg.inv(A)

Computes the inverse of the matrix.

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([ 6, 29,  0,  2,  3, 29, 24, 24, 11, 30], order=31)

In [3]: y = GF.Random(10); y
Out[3]: GF([28, 14, 27, 16,  3, 23, 21, 24, 25, 26], order=31)

In [4]: np.add(x, y)
Out[4]: GF([ 3, 12, 27, 18,  6, 21, 14, 17,  5, 25], order=31)

In [5]: x + y
Out[5]: GF([ 3, 12, 27, 18,  6, 21, 14, 17,  5, 25], 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([[1, 1],
    [6, 3]], order=2^3)

In [3]: B = GF.Random((2,2)); B
Out[3]: 
GF([[7, 6],
    [2, 1]], order=2^3)

In [4]: np.concatenate((A,B), axis=0)
Out[4]: 
GF([[1, 1],
    [6, 3],
    [7, 6],
    [2, 1]], order=2^3)

In [5]: np.concatenate((A,B), axis=1)
Out[5]: 
GF([[1, 1, 7, 6],
    [6, 3, 2, 1]], 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([23, 24, 17, 12, 13, 22,  4, 25, 10, 19,  3, 29, 23,  7,  3, 29, 14,
     9,  4], 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([23, 24, 17, 12, 13, 22,  4, 25, 10, 19,  3, 29, 23,  7,  3, 29, 14,
        9,  4])
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([188, 252, 109, 212, 243, 252,  26, 189, 219,  77, 143,  98, 195, 191,
     99, 217, 231, 185, 228], 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([30, 13,  5, 29, 24, 30,  7, 21, 20,  2], order=31)

In [3]: y = GF.Random(10, low=1); y
Out[3]: GF([26, 29,  1, 25, 10,  8,  3, 30, 14, 24], order=31)

In [4]: z = np.divide(x, y); z
Out[4]: GF([25,  9,  5, 21, 21, 27, 23, 10, 28, 13], order=31)

In [5]: y * z
Out[5]: GF([30, 13,  5, 29, 24, 30,  7, 21, 20,  2], order=31)
In [6]: np.true_divide(x, y)
Out[6]: GF([25,  9,  5, 21, 21, 27, 23, 10, 28, 13], order=31)

In [7]: x / y
Out[7]: GF([25,  9,  5, 21, 21, 27, 23, 10, 28, 13], order=31)

In [8]: np.floor_divide(x, y)
Out[8]: GF([25,  9,  5, 21, 21, 27, 23, 10, 28, 13], order=31)

In [9]: x // y
Out[9]: GF([25,  9,  5, 21, 21, 27, 23, 10, 28, 13], order=31)
np.inner(a, b)[source]

Returns the inner product of two Galois field arrays.

References

Examples

In [1]: GF = galois.GF(31)

In [2]: a = GF.Random(3); a
Out[2]: GF([ 2,  7, 14], order=31)

In [3]: b = GF.Random(3); b
Out[3]: GF([ 3, 15,  4], order=31)

In [4]: np.inner(a, b)
Out[4]: GF(12, order=31)
In [5]: A = GF.Random((3,3)); A
Out[5]: 
GF([[17,  8,  8],
    [ 5,  5, 16],
    [ 1, 23, 16]], order=31)

In [6]: B = GF.Random((3,3)); B
Out[6]: 
GF([[17,  5, 30],
    [ 8,  7, 23],
    [ 7,  6, 26]], order=31)

In [7]: np.inner(A, B)
Out[7]: 
GF([[11,  4,  3],
    [ 1,  9, 16],
    [23, 10,  3]], 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([0, 7, 1, 4, 1], order=2^3)

In [3]: np.insert(x, 1, [0,1,2,3])
Out[3]: GF([0, 0, 1, 2, 3, 7, 1, 4, 1], 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 base galois.GFMeta.primitive_element. See galois.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([15, 17, 15, 19, 28, 29,  7, 10, 30,  8], order=31)

In [4]: y = np.log(x); y
Out[4]: array([21,  7, 21,  4, 16,  9, 28, 14, 15, 12])

In [5]: alpha ** y
Out[5]: GF([15, 17, 15, 19, 28, 29,  7, 10, 30,  8], order=31)
np.matmul(a, b)[source]

Returns the matrix multiplication of two Galois field arrays.

References

Examples

In [1]: GF = galois.GF(31)

In [2]: A = GF.Random((3,3)); A
Out[2]: 
GF([[20, 22, 25],
    [ 2,  7, 23],
    [29, 28, 28]], order=31)

In [3]: B = GF.Random((3,3)); B
Out[3]: 
GF([[30,  5, 21],
    [12,  7,  6],
    [24, 24,  8]], order=31)

In [4]: np.matmul(A, B)
Out[4]: 
GF([[ 7, 17,  8],
    [14, 22, 20],
    [18, 21,  9]], order=31)

In [5]: A @ B
Out[5]: 
GF([[ 7, 17,  8],
    [14, 22, 20],
    [18, 21,  9]], 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([ 1, 19, 10, 12, 13, 20, 18,  3,  8,  1], order=31)

In [3]: y = GF.Random(10); y
Out[3]: GF([25, 18, 26, 27,  6, 29, 13,  3, 21,  7], order=31)

In [4]: np.multiply(x, y)
Out[4]: GF([25,  1, 12, 14, 16, 22, 17,  9, 13,  7], order=31)

In [5]: x * y
Out[5]: GF([25,  1, 12, 14, 16, 22, 17,  9, 13,  7], 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([22, 13,  6, 20, 24,  8, 24,  2, 10, 28], order=31)

In [8]: np.multiply(x, 3)
Out[8]: GF([ 4,  8, 18, 29, 10, 24, 10,  6, 30, 22], order=31)

In [9]: x * 3
Out[9]: GF([ 4,  8, 18, 29, 10, 24, 10,  6, 30, 22], order=31)
In [10]: print(GF.properties)
GF(31):
  structure: Finite Field
  characteristic: 31
  degree: 1
  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([ 6,  4, 16,  4, 17, 27,  8, 11,  4,  2], order=31)

In [3]: y = np.negative(x); y
Out[3]: GF([25, 27, 15, 27, 14,  4, 23, 20, 27, 29], 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([25, 27, 15, 27, 14,  4, 23, 20, 27, 29], order=31)

In [6]: -1*x
Out[6]: GF([25, 27, 15, 27, 14,  4, 23, 20, 27, 29], order=31)
np.outer(a, b)[source]

Returns the outer product of two Galois field arrays.

References

Examples

In [1]: GF = galois.GF(31)

In [2]: a = GF.Random(3); a
Out[2]: GF([ 4, 25, 28], order=31)

In [3]: b = GF.Random(3); b
Out[3]: GF([26, 22, 13], order=31)

In [4]: np.outer(a, b)
Out[4]: 
GF([[11, 26, 21],
    [30, 23, 15],
    [15, 27, 23]], 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([ 7, 28,  6, 10,  4,  0, 22, 14, 23,  1], order=31)

In [3]: np.power(x, 3)
Out[3]: GF([ 2,  4, 30,  8,  2,  0, 15, 16, 15,  1], order=31)

In [4]: x ** 3
Out[4]: GF([ 2,  4, 30,  8,  2,  0, 15, 16, 15,  1], order=31)

In [5]: x * x * x
Out[5]: GF([ 2,  4, 30,  8,  2,  0, 15, 16, 15,  1], order=31)
In [6]: x = GF.Random(10, low=1); x
Out[6]: GF([13, 30,  5,  3, 11, 11,  2, 21,  6, 24], order=31)

In [7]: y = np.random.randint(-10, 10, 10); y
Out[7]: array([-8, -9, -7,  5,  4,  9, -1, -6, -4, -8])

In [8]: np.power(x, y)
Out[8]: GF([ 9, 30, 25, 26,  9, 23, 16, 16,  5, 28], order=31)

In [9]: x ** y
Out[9]: GF([ 9, 30, 25, 26,  9, 23, 16, 16,  5, 28], 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([25, 19,  3, 26,  1], order=31)

In [3]: y = np.reciprocal(x); y
Out[3]: GF([ 5, 18, 21,  6,  1], order=31)

In [4]: x * y
Out[4]: GF([1, 1, 1, 1, 1], order=31)
In [5]: x ** -1
Out[5]: GF([ 5, 18, 21,  6,  1], order=31)

In [6]: GF(1) / x
Out[6]: GF([ 5, 18, 21,  6,  1], order=31)

In [7]: GF(1) // x
Out[7]: GF([ 5, 18, 21,  6,  1], 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([ 4, 22,  0,  0, 19, 11,  3, 20,  9, 12], order=31)

In [3]: np.square(x)
Out[3]: GF([16, 19,  0,  0, 20, 28,  9, 28, 19, 20], order=31)

In [4]: x ** 2
Out[4]: GF([16, 19,  0,  0, 20, 28,  9, 28, 19, 20], order=31)

In [5]: x * x
Out[5]: GF([16, 19,  0,  0, 20, 28,  9, 28, 19, 20], 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([28, 26,  2, 29,  3, 14,  7, 13,  1, 17], order=31)

In [3]: y = GF.Random(10); y
Out[3]: GF([22, 18, 17, 20,  5, 21, 28, 16, 29,  9], order=31)

In [4]: np.subtract(x, y)
Out[4]: GF([ 6,  8, 16,  9, 29, 24, 10, 28,  3,  8], order=31)

In [5]: x - y
Out[5]: GF([ 6,  8, 16,  9, 29, 24, 10, 28,  3,  8], order=31)
np.vdot(a, b)[source]

Returns the dot product of two Galois field vectors.

References

Examples

In [1]: GF = galois.GF(31)

In [2]: a = GF.Random(3); a
Out[2]: GF([12, 11, 30], order=31)

In [3]: b = GF.Random(3); b
Out[3]: GF([19, 23, 30], order=31)

In [4]: np.vdot(a, b)
Out[4]: GF(17, order=31)
In [5]: A = GF.Random((3,3)); A
Out[5]: 
GF([[16,  0, 25],
    [23,  6, 11],
    [ 4, 10, 13]], order=31)

In [6]: B = GF.Random((3,3)); B
Out[6]: 
GF([[19,  9,  9],
    [19, 28,  0],
    [20, 24,  2]], order=31)

In [7]: np.vdot(A, B)
Out[7]: GF(23, order=31)