Skip to content

Commit

Permalink
Add numpy.block snippet (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetazero committed Jul 16, 2022
1 parent 2b4292a commit a2c5ece
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion snippets/numpy/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

from .function_base import *
from .polynomial import *
from .type_check import *
from .type_check import *
from .block import *
17 changes: 17 additions & 0 deletions snippets/numpy/lib/block.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions snippets/tests/numpy/lib/block.py
Original file line number Diff line number Diff line change
@@ -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]]))
9 changes: 9 additions & 0 deletions snippets/tests/numpy/lib/block.py.exp
Original file line number Diff line number Diff line change
@@ -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]])

0 comments on commit a2c5ece

Please sign in to comment.