forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added elias gamma and delta data compression codings (keon#564)
* Added elias gamma and delta data compression algorithms * Added more info about the codings.
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Elias γ code or Elias gamma code is a universal code encoding positive integers. | ||
It is used most commonly when coding integers whose upper-bound cannot be determined beforehand. | ||
Elias δ code or Elias delta code is a universal code encoding the positive integers, | ||
that includes Elias γ code when calculating. | ||
Both were developed by Peter Elias. | ||
""" | ||
from math import log,ceil | ||
|
||
log2 = lambda x: log(x,2) | ||
|
||
# Calculates the binary number | ||
def binary(x,l=1): | ||
fmt = '{0:0%db}' % l | ||
return fmt.format(x) | ||
|
||
# Calculates the unary number | ||
def unary(x): | ||
return (x-1)*'1'+'0' | ||
|
||
def elias_generic(lencoding, x): | ||
""" | ||
The compressed data is calculated in two parts. | ||
The first part is the unary number of 1 + ⌊log2(x)⌋. | ||
The second part is the binary number of x - 2^(⌊log2(x)⌋). | ||
For the final result we add these two parts. | ||
""" | ||
if x == 0: return '0' | ||
|
||
first_part = 1 + int(log2(x)) | ||
|
||
a = x - 2**(int(log2(x))) | ||
|
||
k = int(log2(x)) | ||
|
||
return lencoding(first_part) + binary(a,k) | ||
|
||
def elias_gamma(x): | ||
""" | ||
For the first part we put the unary number of x. | ||
""" | ||
return elias_generic(unary, x) | ||
|
||
def elias_delta(x): | ||
""" | ||
For the first part we put the elias_g of the number. | ||
""" | ||
return elias_generic(elias_gamma,x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters