How to uniquely code 2 reals numbers b and c into a single real number X?

107 Views Asked by At

I am trying to graph 1 number against 2. I need a way to condense those 2 numbers into 1. It is not necessary to retrieve those numbers back, only that the number coded from (b,c) is unique from any other pair of numbers applied in this algorithm.

Bonus to an algorithm that keeps the coded X small and manageable.

1

There are 1 best solutions below

0
On

As kimchi lover (nice name :)) suggests, I think interleaving digits is a good and practical method. That means we get an injection $f:(0,1)^2\to(0,1)$ by: $$ f:(x,y) = (0.a_1a_2a_3\!\ldots, 0.b_1b_2b_3\!\ldots) \mapsto 0.a_1b_1a_2b_2a_3b_3\!\ldots $$ This is unique (i.e. well-defined) and injective, if we choose a convention for numbers with repeating 9's or 0's. It is practical, because we can just take however many digits we want.

This is easily transformed to an injection $\mathbb R^2\to \mathbb R$ by using a bijection $(0,1)\to\mathbb R$, say $g:x\mapsto \log\left(\frac1x-1\right)$. Then we have: $$ h(x,y) := g\circ f\big(g^{-1}(x),g^{-1}(y)\big) $$ is an injection $\mathbb R^2\to\mathbb R$.

Note that this method can be made bijective as shown in this answer.

EDIT: I realised that we can easily extend $f$ to $\mathbb R_+^2 \to \mathbb R_+$ like this: $$ f:(x,y) = (\ldots\!A_2A_1.a_1a_2\!\ldots, \ldots\!B_2B_1.b_1b_2b_3\!\ldots) \mapsto \ldots\! A_2B_2A_1B_1.a_1b_1a_2b_2\!\ldots $$ For example $f(1.23,456.789) = 40516.273809$. I don't see an immediate extension to the whole plain, other than doing a similar trick as before with a bijective function $\mathbb R_+\to \mathbb R$.

I went ahead and plotted $f$. I quite like the fractal shape, combined with nice regularity! This is honestly a nicer function than I would have thought would be easy to find. I can share the (unpolished) code, if you want. In decimal:

In binary: enter image description here