A hum is made of several waves, and each wave is described by an arrow (a complex number): its length is the wave's strength and its angle the wave's timing. Waves add like their arrows, so the hum's strength is the length |S| of the sum S of the arrows in zs. Arrows that point the same way reinforce each other; arrows that point opposite ways cancel. This adding and cancelling is called interference, and it is the heart of quantum algorithms later in this path.
A noise-cancelling circuit adds one more arrow of a fixed length L whose angle \theta it can choose: the arrow is L\,e^{i\theta}, and the result is S + L\,e^{i\theta}.
Write steer_arrow(zs, L) that returns a tuple (quiet_angle, quiet_length, loud_angle, loud_length):
quiet_angle: the angle \theta that makes |S + L\,e^{i\theta}| as small as possible, andquiet_lengththat smallest length;loud_angleandloud_length: the same for the largest length.
Angles are in radians, in the range [0, 2\pi). If the sum is 0 (that is, |S| < 10^{-9}, which allows for rounding), every angle gives the same length L: return (0.0, L, 0.0, L).
Examples
Input: zs = [1, 1j], L = 1
Output: (3.9269908169872414, 0.41421356237309515, 0.7853981633974483, 2.414213562373095)
Explanation: S = 1+1j has length sqrt(2) and points at pi/4. Pointing the new arrow the
opposite way (5pi/4) leaves sqrt(2) - 1; pointing it along S gives sqrt(2) + 1.
Input: zs = [3], L = 5
Output: (3.141592653589793, 2.0, 0.0, 8.0)
Explanation: the new arrow is longer than the hum: at best it overshoots and leaves 5 - 3 = 2.
Input: zs = [2, -2], L = 0.5
Output: (0.0, 0.5, 0.0, 0.5)
Constraints
0 <= len(zs) <= 10**4,0 < L <= 100- the sum is either 0 up to rounding or has length at least
0.01, and neitherSnor-Slies within1e-6(in angle) below the positive real axis - answers are compared with a tolerance of
1e-6; do not round
Goals
- Add arrows and read the length of their sum
- See that an added arrow cancels most when it points opposite the sum, and adds most when aligned
- Handle a sum of 0, where every direction is equally good