# Basics [Numba]¶

Basic functions and operations using Numba, NumPy and Python.

# 0. Installation¶

Installation command for anaconda and pip:

$conda install numba or $ pip install numba

# 1. Jit¶

Jit is the principal and most fundamental Numba's feature. It is a compiler which basically converts a function into efficient machine code.

For example, given a matrix $M$ which is expected the sum of all elements. In a "Pythonic" way, the probom could be solved by:

Despite Numpy having a native proper function to get this value, jit can converts the function in such as faster as np.sum. The procedure is basically to pass the original functions as an argument.

## 1.1. Using decorator @jit¶

A common way to work with numba's features is through decorators. The equivalent process to get the function conversion jit()(sum_matrix) using decorator would be by:

## 1.2. nopython and @njit¶

Run jit entirely without the involvement of the Python interpreter. This mode promotes a best performance for loop based functions. For any non numerical situation which python interpreter is important, this mode must be avoided.

Both decorators @njit and @jit(nopython=True) are the same.

## 1.3. parallel flag and prange¶

Jit has a flag which enables the automatic parallelization, and in addition, Numba has implemented the ability to run loops in parallel using prange.

# 2. Vectorization¶

Functions which operate over array elements.

## 2.1. @vectorize¶

Efficient way to write functions ufunc which operate over each element of n-dimensional arrays. The auxiliar arguments can be scalar or other arrays which must have the same dims so each element will obey the same order.

The following example calculates the Greatest Common Factor between each element pair from the matrices $M_1$ and $M_2$. The "vectorized" function operates over the relative elements $x_{1,i}$ and $x_{2,i}$.

## 2.2. @guvectorize¶

Similar to @vectorize but operates over arbitrary number of elements of input arrays. One difference is the function doesn't necessarily return a value. Instead, it can take the result as an argument. At its declaration, the function's layout is defined in symbolic form like '(m,n),(),()->(m)', where the first argument '(m,n)' is and array with dimensions mxn, the following two arguments '()' are scalar or one-element array and the last argument '(m)' is the output, which is an array with size m.

The following example calculates the density of a point based on the tridimensional distances from the other points. The input P is a list of n 3D coordinates and the output R is an array of densities of the points.

# 3. Stencil¶

Efficient way to create stencil kernels. Similarly to vectorization, it operates over elements but with the possibility of moving around the element's neighborhood.

Stencil promotes a very convenient way to produce spatial filtering for image processing.

## 3.1. Box Filter¶

The box filter results in the average value from the kernel defined by each pixel's neighborhood. The following example shows the box filtering with a kernel 7x7.