Positional numeration systems
A positional numeration system is the most common way of representing numbers. It consists of a number , the base, and a set of numbers , the digits. Usually the base and digits are positive integers, but the definition can be generalized to any abelian group, with multiplication by a base being replaced with a group endomorphism [optimality-width-w]. However, for the purposes of this project, it will suffice to generalize the notion to complex numbers.
Definition 1.1. A positional numeration system is an ordered pair , where and is a finite set.
Definition 1.2. Let be a positional numeration system and a finite sequence where each . Then the number represented by the sequence in base is
The sequence is called a -representation of , is its least significant digit and is its most significant digit.
Example. Let . Then
Example. Let . Then
Example. Let . Then
Note. A positional representation of a number is not necessarily unique. For example, with ,
Definition 1.3. A positional representation is called reduced if . As a special case, the empty representation (representing the number ) is reduced.
Since adding leading zeros to a representation does not change its value, we will usually consider representations that differ only in leading zeros to be the same, with the reduced representation being their canonical form. In some cases, it is more useful to consider all representations as implicitly having an infinite sequence of zeros to their left.
It is a well-known fact that if is an integer greater than and , as in our usual decimal systems, then every non-negative integer has a unique reduced representation. However, the basic concept of the proof is going to be useful later for proving similar facts about more unusual systems.
Definition 1.4. Let . Then is the standard positional numeration system with base .
Theorem 1.5. Let be a standard positional numeration system. Then every has exactly one reduced -representation .
Proof. It follows immediately from
Definition 1.2 that if
, then
. Since every digit from
belongs to a different congruence class modulo
and all classes are covered, this uniquely determines
. Since
(which also follows from the definition),
has to be a
-representation of
(which is obviously a non-negative integer). Therefore, the same argument can be used to uniquely find the value of
. Since
for all
, repeating this process will eventually result in trying to find a representation of
. Since
obviously has exactly one represention that does not start with
β the empty string, this gives an algorithm for finding the
-representation of
along with a proof of its uniqueness.
Example. Let us find the base- representation of (with digits ). We start by dividing by with remainder:
From this, we know that the representation ends in , which is preceded by the representation of . We perform another division:
This tells us that the representation of ends in , preceded by the representation of . Since , its representation is just the single digit ; if we were to continue dividing, we would get an infinite sequence of leading zeros. Therefore, the standard base- representation of is .
NAF binary representations
Consider the positional numeration system with and . For the sake of compactness, we will write as when used as a digit (this notation is not to be confused with the notation for complex conjugation). This system can represent any , including negative integers: simply take the standard binary representation of and multiply all digits by . In this system, many integers have multiple representations, for example . However, it turns out that if we add a simple restriction to which representations are eligible, reduced representations will once again be unique. [binary-arithmetic]
Definition 1.6. A positional representation is in non-adjacent form (NAF) if there are no two non-zero digits next to each other, that is,
using the convention of representations having infinitely many leading zeros.
Note. If all non-zero digits have an absolute value of
(as is the case with
),
Definition 1.6 can be equivalently written in a more compact, but also more obfuscated way:
Example. Out of the aforementioned representations of : , precisely one is in non-adjacent form, namely . It turns out that this is the case for all integers, as shown in the following theorem.
Theorem 1.7. Let . Then every has exactly one reduced -representation with the NAF property.
Proof. Analogously as in the proof of
Theorem 1.5, we need to have
. If
is even, this uniquely determines
to be
; we can then recursively find a represention of
. If
is odd, we have two options: either choose
and find a representation of
, or choose
and find a representation of
. Either way,
will be a non-zero digit, so in order to fulfill the NAF constraint, we need
, which means that
has to be even. Since exactly one of
is even, we have precisely one choice. Repeating this process, we will eventually reach
, since for all non-zero
we have
.
Example. Let us find the NAF binary representation of . Since is odd, its representation has to end in either or . If we were to choose , we would have to find a representation of and then append the to it. However, since is also odd, the resulting representation would end in two non-zero digits, making it not NAF. So instead, we need to use as the last digit and prepend the representation of . is even, so its representation ends in a zero, exactly as we need. After dividing by , we get the even number , giving us another zero. After that, we have to find a representation of . Again, it is necessary to choose because choosing would violate the NAF condition. After a few more divisions by , we get to , which must be represented as because any other of its infinitely many representations is not NAF:
Putting all this together, we obtain the desired NAF representation
The system with also has the useful property that it is possible to tell the sign of a number just from the first non-zero digit of its representation, no matter whether the representation is NAF. We will need this property later, so here is a simple proof.
Theorem 1.8. Let and be a reduced representation of a non-zero integer . Then .
Proof. We shall prove the case
, the proof for
is analogous. From
Definition 1.2:
NAF binary representations have the interesting property that they contain the fewest non-zero digits out of all -representations of the same number. As mentioned in the introduction, this has positive implications for parallel multiplication algorithms. In order to prove this fact, we first need some definitions and a lemma.
Definition 1.9. The Hamming weight of a -representation is its count of non-zero digits:
Note. If all non-zero digits in
have an absolute value of
, then
Definition 1.9 can be equivalently expressed as
Definition 1.10. A -representation is optimal if its Hamming weight is minimal among all -representations of the same number.
Lemma 1.11. Let
. Then any
-representation
can be converted to a NAF
-representation of the same number by repeatedly applying the following substitutions:
- ,
- ,
- ,
- .
Proof. It is obvious from
Definition 1.2 that the substitutions preserve the value of the representation. Also, if this procedure terminates, then the resulting representation has to be NAF, because if there was a pair of non-zero digits next to each other, one of the substitutions could be applied to the first such pair. Therefore, we only need to prove that this procedure terminates. Suppose that
; the proof is analogous for
. Clearly, the only way the representation can increase in length is by applying the third substitution at the beginning:
In order for the representation to increase in length again, a series of substitutions would have to change
into
. However, by
Theorem 1.8, the former represents a negative number, whereas the latter represents a positive number. Since the substitutions preserve the value, this is impossible. Therefore, the length of the initial representation can increase at most once. In other words, the length of all intermediate representations is at most
. For a given intermediate representation
, we define
It can be easily verified that every substitution strictly decreases
. Since
is a non-negative integer, the procedure always terminates.
Example. Consider the number
with its standard binary representation
. By applying these substitutions from right to left, we obtain
This gave us the same result as the modular algorithm, which was to be expected because binary NAF representations are unique according to
Theorem 1.7. Notice that several of the substitutions decreased the Hamming weight of the representation, whereas none of them increased it. This is the key to proving that binary NAF representations are optimal, as formalized by the following theorem.
Theorem 1.12. Binary NAF representations are optimal: Let . Let and be two -representations of the same integer, with also being NAF. Then .
Proof. By
Lemma 1.11, we can convert
into a NAF representation
of the same number. None of the possible substitutions increase the Hamming weight, therefore
. According to
Theorem 1.7, binary NAF representations of the same number are equal, so
, which concludes the proof.
Not only are binary NAF representations optimal, they have asymptotically fewer non-zero digits on average than standard binary representations. To be specific: If we list all possible standard binary representations of some fixed length (not necessarily reduced), obviously exactly half of the digits will be and the other half will be . However, if we do the same with NAF representations in signed binary, the total proportion of non-zero digits will only be approximately , as shown in the following theorem.
Theorem 1.13. Let . Let be the number of all (not necessarily reduced) NAF -representations of length and the sum of the Hamming weights of these representations. Then . That is, for an average sufficiently long NAF -representation, about of its digits are non-zeros.
Proof. [Proof (taken and modified from [algebraicke-metody]).]
Let us find an explicit formula for and . We can think of NAF -representations as consisting of three kinds of βblocksβ: . Note that these blocks can only be used to construct representations starting with a zero, so we are actually expressing and , but this does not matter when .
Take one representation with digits and let be its Hamming weight, which corresponds to the number of and blocks. These blocks take up digits, so the number of blocks is , for a total of blocks. There are ways to choose which of these blocks contain a non-zero digit and ways to choose whether the digit will be or . Therefore, the total number of these representations is
and the sum of Hamming weights is
where we define for so that we do not have to worry about the upper limit. If we also define for , the formula will work for all , which we can use in finding a recursive expression for :
Solving this recurrence using its characteristic polynomial , we get
for some . Clearly , otherwise some terms would be negative. For , we analogously have
This is a non-homogenous linear recurrence. The homogenous solution is the same as for , except with possibly different coefficients :
The particular solution is of the form
for some . We can find those by substituting in the recurrence:
Collecting terms with and cancelling, we obtain
This suffices to evaluate the desired limit, since only the fastest-growing terms matter:
In Theorem 1.13, we have shown that NAF binary representations of a fixed length have an asymptotically smaller Hamming weight than standard binary representations. However, one might object that if, on average, the NAF representation of a given number is significantly longer than the standard binary representation, this might not result in such an improvement for fixed numbers. However, as shown in the following theorem, a NAF binary representation of a given integer is at most one digit longer than its standard binary representation. This is in fact already apparent from the proof of Lemma 1.11, but can easily be proven separately.
Theorem 1.14. Let . Let be a non-zero integer. Then the reduced NAF -representation of has at most digits.
Proof. Let
be the smallest positive integer whose binary NAF representation consists of exactly
digits. From
Theorem 1.8, we know that the representation starts with
. To minimize the value while respecting the NAF condition, it is easy to see that this
is followed by alternating
and
, so for all
,
Consider first the odd case
. By
Definition 1.2,
Since
has the minimal absolute value out of all integers with an
-digit NAF
-representation, this concludes the proof for odd
. For even
,
, so by the already proven odd case,
Complex integers
There are two different natural ways to extend the concept of an integer to complex numbers: either using a square lattice, or using a triangular lattice. These generalizations are called Gaussian integers and Eisenstein integers respectively.
Definition 1.15. The Gaussian integers are the numbers
Definition 1.16. Let . The Eisenstein integers are the numbers
It is obvious how to naturally extend the notion of divisibility and modular arithmetic to integers. A more interesting question is how to extend the notion of parity. We could, just like in the ordinary integers, define a complex integer to be even if it is divisible by . However, this way we would lose the useful property that if is even, then is odd and vice-versa. Instead, we will use the following definition for the case of Gaussian integers, which preserves this property (additionally ensuring that also has the opposite parity from ):
Definition 1.17. Let . If , then is even, otherwise is odd. (Note that divisibility by is the same as divisibility by , we are choosing the former for reasons that will be apparent soon.)
However, it is not possible to do this with Eisenstein integers, the reason being that the triangular grid is not a bipartite graph. Note that if there were names for different congruence classes modulo , they could be generalized to Eisenstein integers by considering congruence classes modulo .
The most obvious way to represent complex integers using a positional numeration system is to simply choose a system for representing regular integers and apply it component-wise. However, with the right choice of base and digits, we can naturally represent both kinds of complex integers directly. [binary-system-complex-numbers]
Theorem 1.18. Let . Then every Gaussian integer has a unique reduced -representation .
Proof. Analogously as in the proof of
Theorem 1.5, if
is even (by
Definition 1.17), then
, otherwise
. We then recursively find a representation of
. It remains to show that this procedure terminates. We can estimate
This implies that if
, then
. Since
is always the square root of an integer, after finitely many steps we will reach an
such that
. Therefore, we just have to manually check that every such
has a representation. There are 21 such numbers:
Note. This system is known as the Penney numeration system due to Walter F. Penney being one of the first mathematicians to work with it.
Note. It is apparent from the proofs of
Theorem 1.5 and
Theorem 1.18 that they could be generalized to a much larger class of positional numeration systems, with the only two requirements being that the digits uniquely cover all congruence classes modulo
and that all numbers with an absolute value within some bound have a representation. The general statement is outside the scope of this project, but can be found in
[kvaterniony].
Note. It is
not possible to represent every Gaussian integer with
and
. If we tried to find such a representation for some numbers, such as
, using the modular algorithm, we would get into an infinite loop. This shows that verifying the representability of certain small numbers is an important part of the proof of
Theorem 1.18 that cannot be left out.
Theorem 1.19. Let . Then every Eisenstein integer has a unique reduced -representation .
Proof. Analogous to the proof of
Theorem 1.18. The proof is left as an exercise for the reader or it can be found in
[kvaterniony].