2D Arrays/Matrix in micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
2dof
Posts: 3
Joined: Wed Aug 10, 2022 2:42 pm

2D Arrays/Matrix in micropython

Post by 2dof » Wed Aug 10, 2022 10:36 pm

I'm looking for information or libraries with 2D arrays/matrix implementation, unfortunately i have been searching
on the forum also on git-hub repos and I have not found anything similar to Python Numpy ( where arrays.matrix operation are base on
LAPACK/BLAS interface ), micropython array.array is simple and do not even cover to create 2D array.

Does anyone present on the forum has encountered this problem and maybe found solution?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: 2D Arrays/Matrix in micropython

Post by jimmo » Sat Aug 13, 2022 2:33 pm

2dof wrote:
Wed Aug 10, 2022 10:36 pm
Does anyone present on the forum has encountered this problem and maybe found solution?
Sounds like you're looking for ulab -- https://github.com/v923z/micropython-ulab/

jameswise
Posts: 5
Joined: Tue Aug 16, 2022 1:32 am

Re: 2D Arrays/Matrix in micropython

Post by jameswise » Tue Aug 16, 2022 1:44 am

From what I can tell, there isn't a direct equivalent to the NumPy library for MicroPython. However, there are some workarounds that you can use to get similar functionality. One workaround is to use the built-in array and bytearray types. You can use these types to create two-dimensional arrays and then use the methods from the array module to perform operations on them.

Another workaround is to use the uarray module. This module provides an efficient way to create and manipulate NumPy-like arrays. The uarray module is not part of the standard MicroPython distribution, so you will need to install it using the upip tool.

Once you have installed the uarray module, you can use it to create and manipulate NumPy-like arrays. The following code snippet shows an example of how to create a two-dimensional array and perform some operations on it:

Code: Select all

import uarray

a = uarray.zeros((10, 10))

a[0, 0] = 1
a[1, 1] = 2
a[2, 2] = 3

print(a)

# Output:

# [[1 0 0 0 0 0 0 0 0 0]
#  [0 2 0 0 0 0 0 0 0 0]
#  [0 0 3 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]]

b = uarray.ones((10, 10))

print(b)

# Output:

# [[1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]]

c = a + b

print(c)

# Output:

# [[2 1 1 1 1 1 1 1 1 1]
#  [1 3 1 1 1 1 1 1 1 1]
#  [1 1 4 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1]]
You can find more information about the uarray module in the MicroPython documentation: https://docs.micropython.org/en/v1.14/l ... array.html
Last edited by jameswise on Tue Aug 16, 2022 10:36 am, edited 1 time in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 2D Arrays/Matrix in micropython

Post by pythoncoder » Tue Aug 16, 2022 8:50 am

@jameswise That documentation link is incorrect.
Peter Hinch
Index to my micropython libraries.

2dof
Posts: 3
Joined: Wed Aug 10, 2022 2:42 pm

Re: 2D Arrays/Matrix in micropython

Post by 2dof » Tue Aug 16, 2022 10:41 am

jimmo wrote:
Sat Aug 13, 2022 2:33 pm
Sounds like you're looking for ulab -- https://github.com/v923z/micropython-ulab/
This is exactly what i need and even more - Thanks!

jameswise - thanks for explanation - I'm going to use arrays/matrix in dsp processing and micropython array/uarray is not applicable for this, and uarray ( did You meant: ( https://uarray.org/en/latest/ , https://github.com/Quansight-Labs/uarray ?? ) - > is a python build.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: 2D Arrays/Matrix in micropython

Post by OlivierLenoir » Wed Aug 17, 2022 5:14 am

MicroPython - Matrix, basic matrix operations, using list. The project is available here.

Post Reply