# Getting Started¶

The Getting Started guide is intended to assist the user with installing the library, creating two example arrays, and performing basic array arithmetic. See Basic Usage for more detailed discussions and examples.

## Install the package¶

The latest version of galois can be installed from PyPI using pip.

\$ python3 -m pip install galois


Import the galois package in Python.

In : import galois

In : galois.__version__
Out: '0.3.5'


## Create a FieldArray subclass¶

Next, create a FieldArray subclass for the specific finite field you’d like to work in. This is created using the GF() class factory. In this example, we are working in $$\mathrm{GF}(3^5)$$.

In : GF = galois.GF(3**5)

In : print(GF.properties)
Galois Field:
name: GF(3^5)
characteristic: 3
degree: 5
order: 243
irreducible_poly: x^5 + 2x + 1
is_primitive_poly: True
primitive_element: x


The FieldArray subclass GF is a subclass of ndarray that performs all arithmetic in the Galois field $$\mathrm{GF}(3^5)$$, not in $$\mathbb{R}$$.

In : issubclass(GF, galois.FieldArray)
Out: True

In : issubclass(GF, np.ndarray)
Out: True


See Array Classes for more details.

## Create two FieldArray instances¶

Next, create a new FieldArray x by passing an ArrayLike object to GF’s constructor.

In : x = GF([236, 87, 38, 112]); x
Out: GF([236,  87,  38, 112], order=3^5)


The array x is an instance of FieldArray and also an instance of ndarray.

In : isinstance(x, galois.FieldArray)
Out: True

In : isinstance(x, np.ndarray)
Out: True


Create a second FieldArray y by converting an existing NumPy array (without copying it) by invoking .view(). When finished working in the finite field, view it back as a NumPy array with .view(np.ndarray).

# y represents an array created elsewhere in the code
In : y = np.array([109, 17, 108, 224]); y
Out: array([109,  17, 108, 224])

In : y = y.view(GF); y
Out: GF([109,  17, 108, 224], order=3^5)


See Array Creation for more details.

## Change the element representation¶

The representation of finite field elements can be set to either the integer ("int"), polynomial ("poly"), or power ("power") representation. The default representation is the integer representation since integers are natural when working with integer NumPy arrays.

Set the element representation by passing the repr keyword argument to GF() or by calling the repr() classmethod. Choose whichever element representation is most convenient.

# The default is the integer representation
In : x
Out: GF([236,  87,  38, 112], order=3^5)

In : GF.repr("poly"); x
Out:
GF([2α^4 + 2α^3 + 2α^2 + 2,               α^4 + 2α,
α^3 + α^2 + 2,      α^4 + α^3 + α + 1], order=3^5)

In : GF.repr("power"); x
Out: GF([α^204,  α^16, α^230,  α^34], order=3^5)

# Reset to the integer representation
In : GF.repr("int");


See Element Representation for more details.

## Perform array arithmetic¶

Once you have two Galois field arrays, nearly any arithmetic operation can be performed using normal NumPy arithmetic. The traditional NumPy broadcasting rules apply.

Standard element-wise array arithmetic – addition, subtraction, multiplication, and division – are easily preformed.

In : x + y
Out: GF([ 18,  95, 146,   0], order=3^5)

In : x - y
Out: GF([127, 100, 173, 224], order=3^5)

In : x * y
Out: GF([ 21, 241, 179,  82], order=3^5)

In : x / y
Out: GF([ 67,  47, 192,   2], order=3^5)


More complicated arithmetic, like square root and logarithm base $$\alpha$$, are also supported.

In : np.sqrt(x)
Out: GF([ 51, 135,  40,  16], order=3^5)

In : np.log(x)
Out: array([204,  16, 230,  34])


See Array Arithmetic for more details.