Turn It Over, Then Reflect

matrices · transpose · complex conjugate · conjugate transpose · nested lists

A matrix here is a list of rows: m[i][j] is the entry in row i, column j, and the entries are complex numbers (ints and floats are allowed too). Its conjugate transpose, written M^\dagger and read "M dagger", swaps rows and columns and takes the complex conjugate of every entry:

(M^\dagger)_{ij} = \overline{M_{ji}}

The conjugate of a + bi is a - bi; in Python z.conjugate(), which ints and floats have too. If m has r rows of k entries, its dagger has k rows of r entries. The dagger is to matrices what the conjugate is to numbers, and the simulator you are building uses it to undo gates.

Write dagger(m) that returns M^\dagger as a new list of rows.

Examples

Input:  m = [[1, 2j], [3, 4]]
Output: [[1, 3], [-2j, 4]]
Explanation: column 0 of m is [1, 3], which becomes row 0; column 1 is [2j, 4], which
becomes row 1 after conjugating: [-2j, 4]. Only transposing would give [[1, 3], [2j, 4]],
only conjugating [[1, -2j], [3, 4]].

Input:  m = [[1+1j, 0, 5j]]
Output: [[(1-1j)], [0], [-5j]]
Explanation: one row of three entries becomes three rows of one entry.

Constraints

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

Goals

  • Transpose a matrix stored as a list of rows: rows become columns
  • Conjugate every entry while transposing
  • Get the shape right for a matrix that is not square
Starting Python…