How Much Do Two Arrow Lists Agree?

complex numbers · inner product · complex conjugate · zip · sum

A vector here is a Python list of complex numbers (ints and floats are allowed too). To measure how much two vectors u and v of the same length agree, multiply them entry by entry and add up, but first take the complex conjugate of every entry of u:

\langle u|v\rangle = \sum_{i} \overline{u_i}\, v_i

The conjugate \overline{z} of z = a + bi is a - bi: the same number with the sign of its imaginary part flipped. In Python it is z.conjugate(), and ints and floats have .conjugate() too. The conjugate goes on the first vector; conjugating v instead gives the conjugate of the answer, which is a different number whenever the answer is not real.

Why conjugate at all? Because then \langle u|u\rangle = \sum_i |u_i|^2, a real number that is never negative: the squared length of u. Without the conjugate, [1j] with itself would give i \cdot i = -1. When \langle u|v\rangle = 0 the two vectors are called orthogonal. This product is the basic measuring tool of the simulator you are building.

Write inner(u, v) that returns \langle u|v\rangle as a number.

Examples

Input:  u = [1j, 1], v = [1, 0]
Output: -1j
Explanation: conj(1j) * 1 + conj(1) * 0 = -1j * 1 + 0 = -1j.
Conjugating v instead would give 1j.

Input:  u = [1+1j, 2], v = [1+1j, 2]
Output: 6
Explanation: (1-1j)(1+1j) + 2 * 2 = 2 + 4 = 6. A vector with itself always gives a
real number; Python prints it as (6+0j), which counts as 6.

Input:  u = [0.5, 0.5j], v = [0.5j, 0.5]
Output: 0
Explanation: 0.5 * 0.5j + (-0.5j) * 0.5 = 0.25j - 0.25j = 0: the vectors are orthogonal.
Without any conjugate you would get 0.5j.

Constraints

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

Goals

  • Compute the inner product of two complex vectors with the conjugate on the first one
  • See that conjugating the wrong vector changes the answer
  • See that a vector's inner product with itself is a real number, its squared length
Starting Python…