---
title: "Polynomial Counting 4, Addendum"
description: |
  Complex embeddings of irrational -adic expansions.
format:
  html:
    html-math-method: katex
date: "2021-02-09"
date-modified: "2025-02-12"
categories:
  - algebra
  - haskell
  - interactive
execute:
  eval: false
---


After converting my original [Two 2's post](../), I found myself much more pleased after making the
  diagrams it more reproducible.
However, I noticed some things which required further examination.


Balanced vs. Binary *κ*-adics
-----------------------------

In the parent post, it was discussed that the integer four has a non-repeating expansion
  when expressed as an *κ*-adic integer.
It followed in multiple ways from the repeating expansion of the integer two in the balanced ternary alphabet.
A more unusual consequence of this is that despite the initial choice of alphabet,
  [negative numerals could be cleared](../#all-positive)
  from the expansion by using an extra-greedy borrow.

There is only one significant difference in how the two are implemented: the choice of integer division function.
In Haskell, there exists both `quotRem` and `divMod`, with the two disagreeing on negative remainders:

::: {.row layout-ncol="2"}
```{haskell}
quotRem (-27) 5
```

```{haskell}
divMod (-27) 5
```
:::

We can factor our choice out of our two digit-wide carry function by passing it as an argument:

```{haskell}
-- Widened carry of a particular repeated amount
-- i.e., for carry qr 2, the carry is 22 = 100
carry2 qr b = carry2' []
  where carry2' zs (x:y:z:xs)
          | q == 0    = carry' (x:zs) (y:z:xs)  -- try carrying at a higher place value
          | otherwise = foldl (flip (:)) ys zs  -- carry here
            where ys     = r : y-x+r : z+q : xs
                  (q, r) = x `qr` b
```

Finally, let's show the iterates of applying this function to "4".
We happen to know the root for $\langle 2, 2|$ is $\sqrt 3 + 1$, so using an approximation,
  we can check that we get a roughly constant value by evaluating at each step.

```{haskell}
-- | layout-ncol: 2
-- | code-fold: true

import IHaskell.Display

-- Horner evaluation on polynomials of ascending powers
hornerEval x = foldr (\c a -> a * x + c) 0
-- Pair a polynomial with its evaluation at x
pairEval x = (,) <*> hornerEval x . map fromIntegral

-- Directly expand the integer `n`, using the `carry` showing each step for `count` steps
expandSteps count carry n = take count $ iterate carry $ n:replicate count 0

cendree4QuotRem10Steps = map (pairEval (sqrt 3 + 1)) $ expandSteps 10 (carry2 quotRem 2) 4
cendree4DivMod10Steps = map (pairEval (sqrt 3 + 1)) $ expandSteps 10 (carry2 divMod 2) 4

markdown "`quotRem`"
markdown "`divMod`"
putStrLn . unlines . map show $ cendreeQuotRem10Steps
putStrLn . unlines . map show $ cendreeDivMod10Steps
```

And fortunately, regardless of which function we pick, the iterates are all roughly four.
Note that since `quotRem` allows negative remainders, implementing the carry with it causes
  negative numbers to show up in our expansions.
Conversely, negative numbers *cannot* show up if we use `divMod`.


### Chaos before Four

Recall the series for two in the *κ*-adics:

$$
2 = ...1\bar{1}1\bar{1}1\bar{1}100_{\kappa}
$$

Since the `divMod` implementation clears negative numbers from expansions, we can try using it on this series.
The result is another chaotic series:

```{haskell}
cendree2DivModCycleExpansion = take 11 $ iterate (carry2 divMod 2) $ take 15 $ 0:0:cycle [1,-1]
putStrLn . unlines . map show $ cendree2DivModCycleExpansion
```

We get the same series if we expand 2 directly (which we can also use to check its validity):

```{haskell}
cendree2DivMod15Steps = map (pairEval (sqrt 3 + 1)) $ expandSteps 15 (carry2 divMod 2) 2
putStrLn . unlines . map show $ cendree2DivMod15Steps
```

We can *also* use this to get a series for negative one, a number which has a terminating expansion
  in the balanced alphabet.

```{haskell}
cendreeNeg1DivMod15Steps = map (pairEval (sqrt 3 + 1)) $ expandSteps 15 (carry2 divMod 2) (-1)
putStrLn . unlines . map show $ cendreeNeg1DivMod15Steps
```

The most natural property of negative one should be that if we add one to it, we get zero.
If we take the last iterate of this, increment the zeroth place value, and apply the carry,
  we find that everything clears properly.

```{haskell}
cendree0FromNeg1DivMod = (\(x:xs) -> (x + 1):xs) $ snd $ last cendreeNeg1DivMod15Steps
cendree0IncrementSteps = map (pairEval (sqrt 3 + 1)) $ take 15 $ iterate (carry2 divMod 2) cendree0FromNeg1DivMod

putStrLn . unlines . map show $ cendree0IncrementSteps
```

Naturally, it should be possible to use the the expansions of negative one and two in tandem on any
  series in the balanced alphabet to convert it to the binary alphabet.
Actually demonstrating this and proving it is left as an exercise.


Are these really -adic?
-----------------------

Perhaps it is still unconvincing that expanding the integers in this way gives something
  indeed related to *p*-adics.
In fact, since the expansions are in binary or (balanced) ternary, the integers should just
  be a subset of the 2-adics or 3-adics.

Still, wanted to see what these numbers actually "look" like, so I whipped up an interactive diagram.
You should definitely see [this page](/interactive/adic/) for more information, but
  the gist is that *p*-adics can be sent into the complex plane in a fractal-like way.


```{ojs}
// | echo: false

{{< include ./showAdicWithKappa.ojs >}}
```

First, notice that with $b = 2$ and $p = 2$, switching between the "b-adic" option
  and the "*κ*-adic" option appears cause some points to appear and disappear.
This is easiest to see when $c \approx 0.5$.

Next, notice that when plotting the *κ*-adics, some self-similarity different from the 2- and 3-adics
  can be observed for $c \approx 0.75$.
There appear to be four clusters of points, with the topmost and rightmost appearing to be
  similar to one another.
Within these two clusters, the rightmost portion of them appears to be the same shape
  as the larger figure.

This is actually great news -- if you switch between the *κ*-adics and the "random binary" option,
  you can see that the latter option tends to the same pattern as the 2-adics.
Thus, even if the expansions for the integers are individually chaotic, together they possess a
  much different structure than pure randomness.
