First publication
2022-05-14
Last update
2022-05-14

I want to create a slider that lets you pick any value between 0 and positive infinity.

Since my slider needs to fit on a screen, this means that I need a smooth twp-way mapping between the finite slider position and any real positive numbers.

To makes things easier, let's pick our input interval as $\mathopen[ -1 ; +1 \mathclose]$ and scale the output so the midpoint of our slider corresponds to the output 1. This way values smaller than 1 will be on in the left half and larger values in the right half.

To summarize, we're looking for a function $f$ such that:

  • $f \colon \mathopen[ -1 ; +1 \mathclose] \to \mathopen[ 0 ; + \infty \mathclose[$
  • $f(0) = 1$
  • $f$ is derivable and strictly growing

A simple piece-wise function

As mentioned in the introduction, the first half of our input should produce values smaller than 1. There, we only need to go from $\mathopen[ -1 ; 0 \mathclose]$ to $\mathopen[ 0 ; +1 \mathclose]$. This is a simple transformation, $x + 1$ will do the trick.

What about the right half? We can mirror the left part, but flip it with $1/a$. This gives $1 \over {1-x}$.

Here is the final result:

$$ f_0(x) = \begin{cases} x + 1, & \text{if } x \le 0 \\ 1 \over {1-x}, & \text{if } x \gt 0 \end{cases} $$

$f_0$ has some nice properties:

  • The computations between the input and output are extremely easy.
  • A number and its inserse (e.g. $1 \over 2$ and $2$) are at the same distance from the midpoint.

However, it also has a small flaw: $f_0$ smooth, but not infinitely smooth. Due to the piece-wise definition, we can't keep deriving forever. The left part will quickly flatline at zero, while the right part will grow faster and faster.

Using the logarithm

From now on, let's avoid piece-wise functions to get smoother results. Mathematics are full of various functions so we don't have to look very far to find some that already map a finite interval to an infinite one.

Let's start with the logarithm: it maps $\mathopen] 0 ; 1 \mathclose]$ to $\mathopen] -\infty ; 0 \mathclose]$. We can flip it and stretch it to fit our bounds:

$$ f_1(x) = { \ln ({{1 - x} \over 2}) \over \ln ( {1 \over 2} ) } $$

Using $f_1$ is nice: our slider now represents a logarithmic scale, familiar to many people. However, we lose the symmetry around the midpoint for inverses.

Using the tangent

Another famous function is the tangent. It maps $\mathopen] 0 ; { \pi \over 2 } \mathclose]$ to $\mathopen[ 0 ; + \infty \mathclose[$. With a slight bit of stretching we get:

$$ f_2(x) = \tan( { { \pi (x + 1) } \over 4 } ) $$

It's nice because we get back mirrored inverses, and it's infinitely derivable. In my particular use-case, I'll often have a value and its inverse, so it's a particularly attractive feature.

With a rational function

$f_2$ is probably the best function for my use case, but while I was thinking about this problem I decided to dig a bit deeper.

We can easily define functions with zeros and infinities anywhere we want by multiplying factors like $(x - a)$ and $1 \over {x - b}$. Such a product is named a rational function.

We want $f(-1) = 0$ and $f(1) \to +\infty$. By combining the base factors and a constant, we get:

$$ f_3(x) = - { { x + 1 } \over { x - 1 } } $$

This is probably the simplest way to define the function we want using only simple arithmetic. Unfortunately, it's also fairly different from $f_1$ and $f_2$ with their nice properties: no logarithmic scale or inverse mirroring.

To improve the situation, we may add an infinity at $-3$. The idea is to balance the infinity at $+1$, so we get symmetry around the zero at $-1$.

$$ f_4(x) = -3 { { x + 1 } \over { (x - 1)(x-3) } } $$

Hey! That's surprisingly close to the tangent-based function and its mirroring properties! I guess that if computing a real tangent is too expensive, $f_4$ may serve as a good substitute to $f_2$.

Padé approximants

The similarity between $f_2$ and $f_4$ reminded me of Taylor series. Taylor series allow to find polynomials to approximate derivable functions. The issue is that sometimes this approximation requires a lot of terms to be good. For example they are not efficient for our situation with a singularity at $f(1)$.

Is there a generalization of truncated Taylor series using rational functions instead of polynoms? Sure enough, someone already thought about this.

After a quick search I found out about Padé approximants.

They are exactly this: rational functions approximating any derivable function. The regular technique computes an approximation around a single point, but there are extensions to compute the approximation using more points from the main function.

The idea is to find an approximation $f(x) ≃ {P_m(x) \over Q_n(x)}$, where $P_m$ and $Q_n$ are two polynomials of degree $m$ and $n$.

We can rewrite the previous equation as $Q_n(x) \times f(x) - P_m(x) ≃ 0$.

If we replace $f$ by its taylor expansion, it means that we are now having an equation involving only polynomials. The $≃ 0$ may be interpreted as the total polynomial and its first few derivatives being equal to zero at the points of interest.

The 3 points we care about are $f(-1) = 0$, $f(0) = 1$, and $f(1) \to +\infty$.

Since $f(1)$ can't be evaluated meaningfully, we will instead work with $g(x) = {1 \over f(x)}$ there.

With this transformation, the approximation becomes $P_m(x) \times g(x) - Q_n(x) ≃ 0$.

Let's see how to approximate $f(x) = f_2(x) = \tan( {π \over 4}(x+1) )$ with this technique.

Let's start with the derivatives of $f$ (setting $a = {π \over 4}$):

$$ \begin{align*} f(x) &= \tan(a(x + 1)) \\ f'(x) &= a(1 + f^2(x)) \\ \end{align*} $$

Deriving adds a factor of $a$ and splits into a lower and higher power of $f(x)$. We can use this pattern to quickly fill the derivatives:

$$ \begin{align*} f(x) &= a^0(& & & 1f(x) &+& & & & & & & & & &) \\ f'(x) &= a^1(& 1 &+& & & 1f^2(x) & & & & & & & & &) \\ f^{(2)}(x) &= a^2(& & & 2f(x) &+& & & 2f^3(x) & & & & & & &) \\ f^{(3)}(x) &= a^3(& 2 &+& & & 8f^2(x) &+& & & 6f^4(x) & & & & &) \\ f^{(4)}(x) &= a^4(& & & 16f(x) &+& & & 40f^3(x) &+& & & 24f^5(x) & & &) \\ f^{(5)}(x) &= a^5(& 16 &+& & & 136f^2(x) &+& & & 240f^4(x) &+& & & 120f^6(x) &) \\ \end{align*} $$

For $g$, we notice that we have $g(x) = {1 \over f(x)} = -f(x - 2)$, so we have $g^{(n)}(x) = -f^{(n)}(x - 2)$.

Next, the Taylor expansions at the different points:

  • $f$ near $-1$:

    $$ f(x) = {π \over 4}(x+1) + {1 \over 3} ({π \over 4})^3 (x+1)^3 + {2 \over 15} ({π \over 4})^5 (x+1)^5 + \ldots $$

  • $f$ near $0$:

    $$ f(x) = 1 + 2 {π \over 4} x + 2 ({π \over 4})^2 x^2 + {8 \over 3} ({π \over 4})^3 x^3 + {10 \over 3} ({π \over 4})^4 x^4 + {64 \over 15} ({π \over 4})^5 x^5 + \ldots $$

  • $g$ near $+1$:

    $$ g(x) = -{π \over 4}(x-1) - {1 \over 3} ({π \over 4})^3 (x-1)^3 - {2 \over 15} ({π \over 4})^5 (x-1)^5 + \ldots $$

Let's now compute the multi-point Padé approximant of order $(1,2)$. This order implies:

$$ \begin{align*} P(x) &= & p_0 &+& p_1x \\ Q(x) &= & 1 &+& q_1x &+& q_2x^2 \\ \end{align*} $$

Let's now expand the Padé equations:

  • $f$ near $-1$:

    $$ \begin{align*} eq_{-1}(x) &= (1 + q_1x + q_2x^2) \times f(x) - P_m(x) \\ &= 1 \\ \end{align*} $$

  • $f$ near $0$:

    $$ \begin{align*} eq_{0}(x) &= Q_n(x) \times f(x) - P_m(x) \\ &= 1 \\ \end{align*} $$