Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create elias.py #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions elias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
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)