# `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

`ulab.``_DType`
`ulab.``_float`

Type alias of the bulitin float

`ulab.``_bool`

Type alias of the bulitin bool

`ulab.``_Index`
class `ulab.``array`(values: Union[array, Iterable[Union[_float, _bool, Iterable[Any]]]], *, dtype: _DType = ulab.float)

1- and 2- dimensional array

Parameters

The `values` sequence can either be another ~ulab.array, sequence of numbers (in which case a 1-dimensional array is created), or a sequence where each subsequence has the same length (in which case a 2-dimensional array is created).

Passing a `ulab.array` and a different dtype can be used to convert an array from one dtype to another.

In many cases, it is more convenient to create an array from a function like `zeros` or `linspace`.

`ulab.array` implements the buffer protocol, so it can be used in many places an `array.array` can be used.

`shape` :Tuple[int, ...]

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

`size` :int

The number of elements in the array

`itemsize` :int

The size of a single item in the array

`strides` :Tuple[int, ...]

Tuple of bytes to step in each dimension, a tuple of length 1 or 2

`copy`(self)array

Return a copy of the array

`dtype`(self)_DType

Returns the dtype of the array

`flatten`(self, *, order: str = 'C')array
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.

`reshape`(self, shape: Tuple[int, ])array

Returns an array containing the same data with a new shape.

`sort`(self, *, axis: Optional[int] = 1)None
Parameters

axis – Whether to sort elements within rows (0), columns (1), or elements (None)

`tobytes`(self)bytearray

Return the raw data bytes in the array

`transpose`(self)array

Swap the rows and columns of a 2-dimensional array

`__add__`(self, other: Union[array, _float])array

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.

`__radd__`(self, other: _float)array
`__sub__`(self, other: Union[array, _float])array

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

`__rsub__`(self, other: _float)array
`__mul__`(self, other: Union[array, _float])array

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.

`__rmul__`(self, other: _float)array
`__div__`(self, other: Union[array, _float])array

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.

`__rdiv__`(self, other: _float)array
`__pow__`(self, other: Union[array, _float])array

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.

`__rpow__`(self, other: _float)array
`__inv__`(self)array
`__neg__`(self)array
`__pos__`(self)array
`__abs__`(self)array
`__len__`(self)int
`__lt__`(self, other: Union[array, _float])array

Return self<value.

`__le__`(self, other: Union[array, _float])array

Return self<=value.

`__gt__`(self, other: Union[array, _float])array

Return self>value.

`__ge__`(self, other: Union[array, _float])array

Return self>=value.

`__iter__`(self)Union[Iterator[array], Iterator[_float]]
`__getitem__`(self, index: _Index)Union[array, _float]

Retrieve an element of the array.

`__setitem__`(self, index: _Index, value: Union[array, _float])None

Set an element of the array.

`ulab.``_ArrayLike`

`ulab.array`, `List[float]`, `Tuple[float]` or `range`

`ulab.``int8` :_DType

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

`ulab.``int16` :_DType

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

`ulab.``float` :_DType

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

`ulab.``uint8` :_DType

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

`ulab.``uint16` :_DType

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

`ulab.``bool` :_DType

Type code for boolean values

`ulab.``get_printoptions`()Dict[str, int]

Get printing options

`ulab.``set_printoptions`(threshold: Optional[int] = None, edgeitems: Optional[int] = None)None

Set printing options

`ulab.``ndinfo`(array: array)None
`ulab.``arange`(stop: _float, step: _float = 1, *, dtype: _DType = ulab.float)array
`ulab.``arange`(start: _float, stop: _float, step: _float = 1, *, dtype: _DType = ulab.float)array

Return a new 1-D array with elements ranging from `start` to `stop`, with step size `step`.

`ulab.``concatenate`(arrays: Tuple[array], *, axis: int = 0)array

Join a sequence of arrays along an existing axis.

`ulab.``diag`(a: array, *, k: int = 0)array

Return specified diagonals.

`ulab.``eye`(size: int, *, M: Optional[int] = None, k: int = 0, dtype: _DType = ulab.float)array

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

`ulab.``full`(shape: Union[int, Tuple[int, ]], fill_value: Union[_float, _bool], *, dtype: _DType = ulab.float)array

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

`ulab.``linspace`(start: _float, stop: _float, *, dtype: _DType = ulab.float, num: int = 50, endpoint: _bool = True, retstep: _bool = False)array

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

`ulab.``logspace`(start: _float, stop: _float, *, dtype: _DType = ulab.float, num: int = 50, endpoint: _bool = True, base: _float = 10.0)array

Return a new 1-D array with `num` evenly spaced elements on a log scale. The sequence starts at `base ** start`, and ends with `base ** stop`.

`ulab.``ones`(shape: Union[int, Tuple[int, ]], *, dtype: _DType = ulab.float)array

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

`ulab.``zeros`(shape: Union[int, Tuple[int, ]], *, dtype: _DType = ulab.float)array

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

`ulab.``frombuffer`(buffer: _typing.ReadableBuffer, dtype: _DType = ulab.float, count: int = - 1, offset: int = 0)array

Interpret a buffer as a 1-dimensional array.