ulab

Manipulate numeric data similar to numpy

ulab is a numpy-like module for micropython, meant to simplify and speed up common mathematical operations on arrays. The primary goal was to implement a small subset of numpy that might be useful in the context of a microcontroller. This means low-level data processing of linear (array) and two-dimensional (matrix) data.

ulab is adapted from micropython-ulab, and the original project’s documentation can be found at https://micropython-ulab.readthedocs.io/en/latest/

ulab is modeled after numpy, and aims to be a compatible subset where possible. Numpy’s documentation can be found at https://docs.scipy.org/doc/numpy/index.html

class ulab.array(values, *, dtype=float)

1- and 2- dimensional array

shape :tuple

The size of the array, a tuple of length 1 or 2

size :int

The number of elements in the array

itemsize :int

The number of elements in the array

flatten(self, *, order='C')
Parameters:order – Whether to flatten by rows (‘C’) or columns (‘F’)

Returns a new ulab.array object which is always 1 dimensional. If order is ‘C’ (the default”, then the data is ordered in rows; If it is ‘F’, then the data is ordered in columns. “C” and “F” refer to the typical storage organization of the C and Fortran languages.

sort(self, *, axis=1)
Parameters:axis – Whether to sort elements within rows (0), columns (1), or elements (None)
transpose(self)

Swap the rows and columns of a 2-dimensional array

__add__(self)

Adds corresponding elements of the two arrays, or adds a number to all elements of the array. If both arguments are arrays, their sizes must match.

__sub__(self)

Subtracts corresponding elements of the two arrays, or adds a number to all elements of the array. If both arguments are arrays, their sizes must match.

__mul__(self)

Multiplies corresponding elements of the two arrays, or multiplies all elements of the array by a number. If both arguments are arrays, their sizes must match.

__div__(self)

Multiplies corresponding elements of the two arrays, or divides all elements of the array by a number. If both arguments are arrays, their sizes must match.

__pow__()

Computes the power (x**y) of corresponding elements of the the two arrays, or one number and one array. If both arguments are arrays, their sizes must match.

__getitem__()

Retrieve an element of the array.

__setitem__()

Set an element of the array.

ulab.int8

Type code for signed integers in the range -128 .. 127 inclusive, like the ‘b’ typecode of array.array

ulab.int16

Type code for signed integers in the range -32768 .. 32767 inclusive, like the ‘h’ typecode of array.array

ulab.float

Type code for floating point values, like the ‘f’ typecode of array.array

ulab.uint8

Type code for unsigned integers in the range 0 .. 255 inclusive, like the ‘H’ typecode of array.array

ulab.uint16

Type code for unsigned integers in the range 0 .. 65535 inclusive, like the ‘h’ typecode of array.array

ulab.ones(shape, *, dtype=float)

Return a new array of the given shape with all elements set to 1.

ulab.zeros(shape, *, dtype)

Return a new array of the given shape with all elements set to 0.

ulab.eye(size, *, dtype=float)

Return a new square array of size, with the diagonal elements set to 1 and the other elements set to 0.

ulab.linspace(start, stop, *, dtype=float, num=50, endpoint=True)

Return a new 1-D array with num elements ranging from start to stop linearly.