Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!

Zoltán Vörös' ulab Brings NumPy-Style Fast Math to MicroPython in Just 25kB of Compiled Code

Written in 'Pythonic C,' ulab adds NumPy-inspired functionality to the microcontroller-focused MicroPython for on-device fast math.

Developer Zoltán Vörös has released ulab, a library designed to bring NumPy-like fast array manipulation to the microcontroller-focused MicroPython programming language.

First released by Jim Hugunin in 1995 as Numeric, based on earlier work by Jim Fulton, before being rewritten and rebranded by Travis Oliphant 2005 to add features from the competing Numeric library, the community-driven NumPy library is designed to expand the capabilities of Python with a focus on high-performance numerical computation. Designed to work around the poor performance of mathematical algorithms in CPython, NumPy has proven popular for scientific computing as well as robotics, computer vision, and artificial intelligence projects.

Vörös' ulab isn't quite a port of NumPy, but is designed to replicate the same functionality on the microcontroller-focused MicroPython platform. "ulab is a NumPy-like module for MicroPython, meant to simplify and speed up common mathematical operations on arrays," Vörös explains. "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.

"The main points of ulab are: Compact, iterable and slicable containers of numerical data in one and two dimensions (arrays and matrices). These containers support all the relevant unary and binary operators (e.g., len, ==, +, *, etc.); vectorised computations on MicroPython iterables and numerical arrays/matrices (in NumPy-speak, universal functions); basic linear algebra routines (matrix inversion, multiplication, reshaping, transposition, determinant, and eigenvalues); polynomial fits to numerical data; fast Fourier transforms."

Vörös admits that high-performance numerical compute directly on a microcontroller is an unusual approach, but makes the case for a fast math library nevertheless after finding himself in need of a Fourier transform of an incoming analogue-to-digital converter (ADC) signal. To keep performance up and hardware requirements down ulab is written in C, but in a manner to "behave in a Pythonic way," and adds around 25kB of extra compiled code to the MicroPython firmware when enabled.

Vörös has released ulab under the MIT Licence, with full source available on his GitHub repository. The software itself is supported by a wealth of documentation published via Read the Docs.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles