1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
""" Challenge 170 Task 2"""
import numpy as np
def get_kronecker_product(matrix: dict) -> np.ndarray:
"""Compute the Kronecker product"""
matrix_a = matrix["a"]
matrix_b = matrix["b"]
# will be used in calculating the right position in the output
row_len = len(matrix_a)
# it is the same, just for code readability
col_len = len(matrix_a)
result = np.empty((row_len * row_len, col_len * col_len))
# process the matrix a row by row
# row by row
for a_row_pos, row in enumerate(matrix_a):
# column by column
for a_col_pos, a_item in enumerate(row):
# mutiply each number with matrix b
for b_row_pos, b_row in enumerate(matrix_b):
for b_col_pos, b_item in enumerate(b_row):
# calculate the right position in the output matrix
result[a_row_pos * row_len + b_row_pos][
a_col_pos * col_len + b_col_pos
] = (a_item * b_item)
return result
in_hash = {
"a": [[1, 2], [3, 4]],
"b": [[5, 6], [7, 8]],
}
assert np.array_equal(
get_kronecker_product(in_hash),
np.array(
[
[5, 6, 10, 12],
[7, 8, 14, 16],
[15, 18, 20, 24],
[21, 24, 28, 32],
]
),
)
|