Add numpy.block snippet (#539)
This commit is contained in:
parent
2b4292abcc
commit
a2c5ece991
4 changed files with 47 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
|
||||
from .function_base import *
|
||||
from .polynomial import *
|
||||
from .type_check import *
|
||||
from .type_check import *
|
||||
from .block import *
|
||||
17
snippets/numpy/lib/block.py
Normal file
17
snippets/numpy/lib/block.py
Normal 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
|
||||
19
snippets/tests/numpy/lib/block.py
Normal file
19
snippets/tests/numpy/lib/block.py
Normal 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]]))
|
||||
9
snippets/tests/numpy/lib/block.py.exp
Normal file
9
snippets/tests/numpy/lib/block.py.exp
Normal 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]])
|
||||
Loading…
Reference in a new issue