The Mixing Desk

matrices · matrix-vector product · complex numbers · list comprehensions · zip

A sound engineer's mixing desk takes k input channels and produces r output channels. Output channel i is a weighted sum of all inputs, with weight m[i][j] for input j. The weights may be complex numbers (a complex weight both scales a signal and shifts its phase). In maths this is the matrix-vector product w = M v:

w_i = \sum_{j} M_{ij}\, v_j

The matrix is a list of rows: m[i] is row i, and m[i][j] the entry in row i, column j. It has r rows of k entries each, and the vector v has k entries. The answer has one entry per row. There is no conjugate in this product. The simulator you are building applies every gate to its state exactly like this.

Write mat_vec(m, v) that returns M v as a list of r numbers.

Examples

Input:  m = [[1, 2j], [0, 1]], v = [1, 1]
Output: [(1+2j), 1]
Explanation: row 0 gives 1 * 1 + 2j * 1 = 1 + 2j; row 1 gives 0 * 1 + 1 * 1 = 1.
Combining the columns with v instead would give [1, (1+2j)].

Input:  m = [[1, 1j, 0], [0, 1, -1]], v = [2, 1j, 3]
Output: [1, (-3+1j)]
Explanation: two rows of three entries, so the answer has two entries:
1 * 2 + 1j * 1j + 0 * 3 = 2 - 1 = 1 and 0 * 2 + 1 * 1j + (-1) * 3 = -3 + 1j.

Constraints

  • answers are compared with a tolerance of 1e-6; a complex answer with imaginary part 0 counts as the real number

Goals

  • Store a matrix as a list of rows and read entry m[row][col]
  • Multiply a matrix by a vector: each output entry is one row combined with the vector
  • Handle matrices that are not square
Starting Python…