Unary coding
Unary coding, or the unary numeral system, is an entropy encoding that represents a natural number, , with ones followed by a zero (if the term natural number is understood as non-negative integer) or with ones followed by a zero (if the…
≈ 7 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Unary coding, or the unary numeral system, is an entropy encoding that represents a natural number, , with ones followed by a zero (if the term natural number is understood as non-negative integer) or with ones followed by a zero (if the term natural number is understood as strictly positive integer). A unary number's code length would thus be with that first definition, or with that second definition.
Unary code when vertical behaves like mercury in a thermometer that gets taller or shorter as n gets bigger or smaller, and so is sometimes called thermometer code. An alternative representation uses or zeros followed by a one, effectively swapping the ones and zeros, without loss of generality.
For example, the first ten unary codes are: Unary coding is an optimally efficient encoding for the following discrete probability distribution : \operatorname{P}(n) = 2^{-n}\, for n=1,2,3,... . In symbol-by-symbol coding, it is optimal for any geometric distribution : \operatorname{P}(n) = (k-1)k^{-n}\, for which , the golden ratio, or, more generally, for any discrete distribution for which : \operatorname{P}(n) \ge \operatorname{P}(n+1) + \operatorname{P}(n+2)\, for n=1,2,3,... .…
Unary coding, or the unary numeral system, is an entropy encoding that represents a natural number, , with ones followed by a zero (if the term natural number is understood as non-negative integer) or with ones followed by a zero (if the term natural number is understood as strictly positive integer). A unary number's code length would thus be with that first definition, or with that second definition.
Unary code when vertical behaves like mercury in a thermometer that gets taller or shorter as n gets bigger or smaller, and so is sometimes called thermometer code. An alternative representation uses or zeros followed by a one, effectively swapping the ones and zeros, without loss of generality.
For example, the first ten unary codes are: Unary coding is an optimally efficient encoding for the following discrete probability distribution \operatorname{P}(n) = 2^{-n}\, for n=1,2,3,... . In symbol-by-symbol coding, it is optimal for any geometric distribution \operatorname{P}(n) = (k-1)k^{-n}\, for which , the golden ratio, or, more generally, for any discrete distribution for which \operatorname{P}(n) \ge \operatorname{P}(n+1) + \operatorname{P}(n+2)\, for n=1,2,3,... .
Although it is the optimal symbol-by-symbol coding for such probability distributions, Golomb coding achieves better compression capability for the geometric distribution because it does not consider input symbols independently, but rather implicitly groups the inputs. For the same reason, arithmetic encoding performs better for general probability distributions, as in the last case above.
Unary coding is both a prefix-free code and a self-synchronizing code.
Unary code in use today
Examples of unary code uses include: In Golomb Rice code, unary encoding is used to encode the quotient part of the Golomb code word. In UTF-8, unary encoding is used in the leading byte of a multi-byte sequence to indicate the number of bytes in the sequence so that the length of the sequence can be determined without examining the continuation bytes. Instantaneously trained neural networks use unary coding for efficient data representation.
Unary coding in biological networks
Unary coding is used in the neural circuits responsible for birdsong production. The nucleus in the brain of the songbirds that plays a part in both the learning and the production of bird song is the HVC (high vocal center). The command signals for different notes in the birdsong emanate from different points in the HVC. This coding works as space coding which is an efficient strategy for biological circuits due to its inherent simplicity and robustness.
Standard run-length unary codes
All binary data is defined by the ability to represent unary numbers in alternating run-lengths of 1s and 0s. This conforms to the standard definition of unary i.e. digits of the same number 1 or 0. All run-lengths by definition have at least one digit and thus represent strictly positive integers.
These codes are guaranteed to end validly on any length of data (when reading arbitrary data) and in the (separate) write cycle allow for the use and transmission of an extra bit (the one used for the first bit) while maintaining overall and per-integer unary code lengths of exactly .
Uniquely decodable non-prefix unary codes
Following is an example of uniquely decodable unary codes that is not a prefix code and is not instantaneously decodable (need look-ahead to decode) These codes also (when writing unsigned integers) allow for the use and transmission of an extra bit (the one used for the first bit). Thus they are able to transmit integers * unary bits and 1 additional bit of information within bits of data.
Symmetric unary codes
The following symmetric unary codes can be read and instantaneously decoded in either direction: These codes have a unique property wherein if one were to scan data (1s and 0s) of any probability distribution for symmetric unary codes, the average length of the scanned symmetric unary code is always around 2. This can help with timing concerns as almost every protocol has to put in checks and balances for continuous streams of low entropy data which can damage components like USB peripherals etc. N ( Integer >= 0 ) = A x + B y + C z ; ( A,B,C >= 0 and 0 and 0 ) = A/x + B/y + C/z ; ( A,B,C > 0 and 0 ) = A/x , B/x , C/y , D/z , E/z ; ( A,B,C,D,E > 0 and 0 ) = A/x + B/y + C/z ; ( A,B,C > 0 and < x,y,z and x < y < z for non trivial fractions ) Simply omit the 0 and increase the base in every step beginning with 2.
Reduction in the visible base implies that it is a new number and no separator is required. This increases the bit sizes somewhat but those are low entropy or one can even write a count of the digits first and then permutate the digits in DIGIT_COUNT! ways to store DIGIT_COUNT! amount of data for practically free (just the cost of a small number of the count of the digits).
As is visible, no non trivial fraction can be greater than 1, so if there is a possibility that the integer part of P(I) can cause a large number of fractions, so it may be easier to start with (2*I-1)/(2*I) and work backwards (even a start and count should suffice) till the remaining value reaches < 0 and then changing direction (starting from 1/2 upwards).
Generalized unary coding
A generalized version of unary coding was presented by Subhash Kak to represent numbers much more efficiently than standard unary coding. Here's an example of generalized unary coding for integers from 0 through 15 that requires only 7 bits (where three bits are arbitrarily chosen in place of a single one in standard unary to show the number).
Note that the representation is cyclic where one uses markers to represent higher integers in higher cycles. Generalized unary coding requires that the range of numbers to be represented to be pre-specified because this range determines the number of bits that are needed.
See also
Unary numeral system
Text derived from https://en.wikipedia.org/w/index.php?oldid=1376491300 under CC BY-SA 4.0; modifications have been made.