Every Pair, in Order

Kronecker product · tensor product · nested loops · indexing · list comprehensions

Vectors here are Python lists of complex numbers (ints and floats are allowed too). The Kronecker product (or tensor product) a \otimes b of a vector a of length m and a vector b of length n is a vector of length m * n that holds every product a[i] * b[j], in this order:

kron(a, b)[i * n + j] = a[i] * b[j]        for 0 <= i < m and 0 <= j < n

So it starts with a[0] times all of b, then a[1] times all of b, and so on. There is no conjugate. The order of the two vectors matters: kron(b, a) holds the same numbers, arranged differently. The simulator you are building uses this product to combine the states of separate parts into one state.

Write kron(a, b) that returns a \otimes b as a list.

Examples

Input:  a = [1, 2], b = [3, 4, 5]
Output: [3, 4, 5, 6, 8, 10]
Explanation: 1 times [3, 4, 5], then 2 times [3, 4, 5]. With the vectors the other way
round the answer would be [3, 6, 4, 8, 5, 10].

Input:  a = [0.6, 0.8j], b = [1j, 1]
Output: [0.6j, 0.6, -0.8, 0.8j]
Explanation: 0.6 * 1j, 0.6 * 1, 0.8j * 1j = -0.8, 0.8j * 1.

Constraints

  • answers are compared with a tolerance of 1e-6

Goals

  • Build the Kronecker product of two vectors from its index rule
  • See that kron(a, b) and kron(b, a) hold the same numbers in a different order
  • Turn a pair of positions (i, j) into one position i * len(b) + j
Starting Python…