Skip to content

Instantly share code, notes, and snippets.

@rain-1
Created June 26, 2022 18:45
Show Gist options
  • Save rain-1/ca47915bf87520b0ca6541e9079b3810 to your computer and use it in GitHub Desktop.
Save rain-1/ca47915bf87520b0ca6541e9079b3810 to your computer and use it in GitHub Desktop.
Ring Quotient For Programmers

quick recap on complex numbers

Take a number, square it, the result is non-negative. Because positive * positive = positive and negative * negative is positive. Or $0^2 = 0^2$.

But someone wanted to take square roots of negative numbers, so they did, and called it 'i'. $\sqrt{-1} = i$. A lot of people were frustrated upon learning this "You can't do that!", "How do you know that it doesn't lead to contradictions".

The solution, to put imaginary and complex numbers on a solid foundation is something called a ring quotient. What you do is you start with the ring (meaning number system) of polynomials over the real numbers $R[i]$, which looks like this:

  • $1, 2 3.5, \pi$ etc.
  • $i, i^2, 0.3 + 9.5 i + 23 i^3$ and so on.

Then you take a quotient $R[i]/(i^2+1)$ which causes the equation $i^2 + 1 = 0$ to be true, equivalently $i^2 = -1$.

In terms of programming the way this is done is every instance if $i^n$ is reduced down to either 1, -1, i or -i and at the end of the day it lets you implement the entire number system of "complex numbers" as a pair (real,imag).

units

Another thing that we do a lot in calculations, especially in physics, is include units. But it's kind of "loose", we know that you can only add units that are like. But you can multiply and divide anything and the units join. For example if distance is measured in meters and time in seconds, $v = d / t$ has units $meters / seconds$. Also you can have conversions between e.g. inches and meters, or meters and cm.

This is the amazing part I want to share in this post: You can do all this with a ring quotient.

Let's define $R = \mathbb R[m,cm,s]/(100 cm - 1 m)$ this ring $R$ lets us do calculations with meters, seconds and has compatability between cm and m. Absolutely perfect!

So in terms of implementation, I would start with code that implements multi-variable polynomials with basic arithmetic, reading in and printing out. And then add a normalization procedure which converts the variable cm into 1/100 m.

Functions like $\sin$ should probably only be applied to 'raw' numbers without any unit next to it. And in this system we can add e.g. $2 m + 3 s$ but the result is just $2 m + 3 s$, nothing happens to it. So that is fine really.

I think that this is a really useful idea for programming calculations that involve units, but I also think it's valuable just in terms of how much it clarifies the informal way we work with numbers that have units.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment