numerics-2022/Task 1/convolutions.ipynb
2022-09-24 11:25:54 +03:00

2.3 KiB

In [1]:
import numpy as np
import itertools
In [2]:
c = 3
In [3]:
# Generate some data
np.random.seed(42)
lambda1 = np.random.normal(size=(c, c))
lambda2 = np.random.normal(size=(c, c))
lambda3 = np.random.normal(size=(c, c))
G1 = np.random.normal(size=(c, c, c))
G2 = np.random.normal(size=(c, c, c))
U = np.random.normal(size=(c, c, c, c))
In [4]:
def Z_naive(lambda1, lambda2, lambda3, G1, G2, U):
    c = lambda1.shape[0]
    Z = np.zeros(shape=(c, c, c, c))
    for a, b, c, d, e, f, g, h, i, j in itertools.product(*([range(c)]*10)):
        Z[a, h, i, j] += lambda1[a, b]*lambda2[d, e]*lambda3[g, h]*G1[c, b, d]*G2[f, e, g]*U[i, j, c, f]
    return Z
In [5]:
%%timeit
Z = Z_naive(lambda1, lambda2, lambda3, G1, G2, U)
198 ms ± 30.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [6]:
Z = Z_naive(lambda1, lambda2, lambda3, G1, G2, U)
Z.shape
Out[6]:
(3, 3, 3, 3)