logoalt Hacker News

sreantoday at 2:26 PM1 replyview on HN

META: Pulling this out of its original context because I think more readers would find the code amusing. I am breaking the rules, but hopefully for a good/pardonable reason.

> Most of the time we think of complex numbers as vectors in R2 or as rotation+scaling operators, but rarely do we actually we want them in both roles at the same time.

I can give one counterexample.

I was asked to comment on a piece of code that did 2D geometry in Python. There was one piece that was a tangle of trigonometry to find the angular bisector of an angle subtended at the origin by two points.

Using the fact that points can be represented by complex numbers and that rotation is just multiplication one can make that function into a one liner.

      √(z1 * z2)
The geometric mean of the two points as represented by complex numbers gives you the bisector. Python has native support for complex numbers so all the computation is handled by the runtime.

Replies

Chinjuttoday at 2:31 PM

This is like how one often wants to distinguish the points of an affine space from the vectors representing displacements in that space (there is no distinguished origin for the physical world, but there is a distinguished concept of zero displacement). One can add a vector to a point to get a point, or a vector to a vector to get a vector, but cannot add a point to a point to get another point. Yet, it is meaningful to treat a linear combination of points in an affine space as yielding another point in the same space when the weights of the linear combination sum to 1.

The exact same thing is happening here, only multiplicatively, where z1^(1/2) * z2^(1/2) is a combination with two weights of 1/2 (thus, summing to 1). It is geometrically meaningful to treat 2d vectors (displacements in a plane) as complex numbers, raise them to exponents summing to 1, and then multiply these together to get another vector in the same plane. But it is not generally geometrically meaningful to just multiply one vector by another vector to get a third vector in the same space (because this would require distinguishing some particular direction and magnitude as "1").

show 1 reply