From a2c5ece99108735792cba29bd993108d81079422 Mon Sep 17 00:00:00 2001 From: thetazero Date: Sat, 16 Jul 2022 09:00:10 -0700 Subject: [PATCH] Add numpy.block snippet (#539) --- snippets/numpy/lib/__init__.py | 3 ++- snippets/numpy/lib/block.py | 17 +++++++++++++++++ snippets/tests/numpy/lib/block.py | 19 +++++++++++++++++++ snippets/tests/numpy/lib/block.py.exp | 9 +++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 snippets/numpy/lib/block.py create mode 100644 snippets/tests/numpy/lib/block.py create mode 100644 snippets/tests/numpy/lib/block.py.exp diff --git a/snippets/numpy/lib/__init__.py b/snippets/numpy/lib/__init__.py index 9f5d9d4..698d29f 100644 --- a/snippets/numpy/lib/__init__.py +++ b/snippets/numpy/lib/__init__.py @@ -1,4 +1,5 @@ from .function_base import * from .polynomial import * -from .type_check import * \ No newline at end of file +from .type_check import * +from .block import * \ No newline at end of file diff --git a/snippets/numpy/lib/block.py b/snippets/numpy/lib/block.py new file mode 100644 index 0000000..eacacc1 --- /dev/null +++ b/snippets/numpy/lib/block.py @@ -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 \ No newline at end of file diff --git a/snippets/tests/numpy/lib/block.py b/snippets/tests/numpy/lib/block.py new file mode 100644 index 0000000..71bfb92 --- /dev/null +++ b/snippets/tests/numpy/lib/block.py @@ -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]])) diff --git a/snippets/tests/numpy/lib/block.py.exp b/snippets/tests/numpy/lib/block.py.exp new file mode 100644 index 0000000..8a5a101 --- /dev/null +++ b/snippets/tests/numpy/lib/block.py.exp @@ -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]]) \ No newline at end of file