Page 1 of 1

2D Arrays/Matrix in micropython

Posted: Wed Aug 10, 2022 10:36 pm
by 2dof
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?

Re: 2D Arrays/Matrix in micropython

Posted: Sat Aug 13, 2022 2:33 pm
by jimmo
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/

Re: 2D Arrays/Matrix in micropython

Posted: Tue Aug 16, 2022 1:44 am
by jameswise
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

Re: 2D Arrays/Matrix in micropython

Posted: Tue Aug 16, 2022 8:50 am
by pythoncoder
@jameswise That documentation link is incorrect.

Re: 2D Arrays/Matrix in micropython

Posted: Tue Aug 16, 2022 10:41 am
by 2dof
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.

Re: 2D Arrays/Matrix in micropython

Posted: Wed Aug 17, 2022 5:14 am
by OlivierLenoir
MicroPython - Matrix, basic matrix operations, using list. The project is available here.