Add numpy.block snippet (#539)

This commit is contained in:
thetazero 2022-07-16 09:00:10 -07:00 committed by GitHub
parent 2b4292abcc
commit a2c5ece991
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 1 deletions

View file

@ -1,4 +1,5 @@
from .function_base import *
from .polynomial import *
from .type_check import *
from .type_check import *
from .block import *

View file

@ -0,0 +1,17 @@
from ulab.numpy import zeros
def block(S):
w = sum([len(m[0]) for m in S[0]])
h = sum([len(row[0]) for row in S])
M = zeros((h, w))
i = 0
j = 0
for row in S:
di = len(row[0])
for matrix in row:
dj = len(matrix[0])
M[i:i + di, j:j + dj] = matrix
j += dj
i += di
j = 0
return M

View file

@ -0,0 +1,19 @@
from ulab.numpy import array, zeros, eye, ones
from snippets import numpy
a = array([[1, 1]])
b = array([[2]])
c = array(
[[3, 3],
[3, 3],
[3, 3]])
d = array(
[[4],
[4],
[4]])
print(numpy.block([[a, b], [c, d]]))
a = zeros((2, 3))
b = eye(2) * 2
c = eye(3) * 5
d = ones((3, 2))
print(numpy.block([[a, b], [c, d]]))

View file

@ -0,0 +1,9 @@
array([[1, 1, 2],
[3, 3, 4],
[3, 3, 4],
[3, 3, 4]])
array([[0, 0, 0, 2, 0],
[0, 0, 0, 0, 2],
[5, 0, 0, 1, 1],
[0, 5, 0, 1, 1],
[0, 0, 5, 1, 1]])