Arithmetic coding/As a generalized change of radix: Difference between revisions
Content added Content deleted
(added python) |
(simplified the encoding loop a little bit) |
||
Line 62: | Line 62: | ||
// frequencies of all previously occurring symbols |
// frequencies of all previously occurring symbols |
||
bigBase := big.NewInt(int64(base)) |
bigBase := big.NewInt(int64(base)) |
||
⚫ | |||
for |
for _, c := range chars { |
||
⚫ | |||
bigI := big.NewInt(int64(i)) |
|||
diff := big.NewInt(0) |
|||
diff.Sub(bigLim, bigI) |
|||
pow := big.NewInt(0) |
|||
pow.Exp(bigBase, diff, nil) |
|||
x := big.NewInt(1) |
|||
x.Mul(big.NewInt(cf[chars[i]]), pow) |
|||
L.Mul(L, bigBase) |
|||
L.Add(L, x.Mul(x, pf)) |
L.Add(L, x.Mul(x, pf)) |
||
pf.Mul(pf, big.NewInt(freq[ |
pf.Mul(pf, big.NewInt(freq[c])) |
||
} |
} |
||
Line 256: | Line 246: | ||
my %cf = cumulative_freq(\%freq); |
my %cf = cumulative_freq(\%freq); |
||
# |
# Base |
||
my $ |
my $base = Math::BigInt->new(scalar @chars); |
||
my $base = $lim + 1; |
|||
# Lower bound |
# Lower bound |
||
Line 268: | Line 257: | ||
# Each term is multiplied by the product of the |
# Each term is multiplied by the product of the |
||
# frequencies of all previously occurring symbols |
# frequencies of all previously occurring symbols |
||
foreach my $c (@chars) { |
|||
my $x = $cf{$ |
my $x = $cf{$c}; |
||
$L-> |
$L->bmuladd($base, $x * $pf); |
||
$pf->bmul($freq{$ |
$pf->bmul($freq{$c}); |
||
} |
} |
||
Line 376: | Line 365: | ||
my %cf = cumulative_freq(%freq); |
my %cf = cumulative_freq(%freq); |
||
# |
# Base |
||
my $ |
my $base = @chars.elems; |
||
my $base = $lim + 1; |
|||
# Lower bound |
# Lower bound |
||
Line 388: | Line 376: | ||
# Each term is multiplied by the product of the |
# Each term is multiplied by the product of the |
||
# frequencies of all previously occurring symbols |
# frequencies of all previously occurring symbols |
||
for |
for @chars -> $c { |
||
my $x = %cf{ |
my $x = %cf{$c}; |
||
$L |
$L = $L * $base + $x * $pf; |
||
$pf *= %freq{ |
$pf *= %freq{$c}; |
||
} |
} |
||
Line 498: | Line 486: | ||
cf = cumulative_freq(freq) |
cf = cumulative_freq(freq) |
||
# |
# Base |
||
base = len(bytes) |
|||
base = lim+1 |
|||
# Lower bound |
# Lower bound |
||
Line 510: | Line 497: | ||
# Each term is multiplied by the product of the |
# Each term is multiplied by the product of the |
||
# frequencies of all previously occurring symbols |
# frequencies of all previously occurring symbols |
||
for |
for b in bytes: |
||
x = cf[ |
x = cf[b] |
||
lower |
lower = lower * base + x*pf |
||
pf *= freq[ |
pf *= freq[b] |
||
# Upper bound |
# Upper bound |
||
Line 610: | Line 597: | ||
cf = cumulative_freq(freq) |
cf = cumulative_freq(freq) |
||
# |
# Base |
||
base = bytes.size |
|||
base = lim+1 |
|||
# Lower bound |
# Lower bound |
||
Line 622: | Line 608: | ||
# Each term is multiplied by the product of the |
# Each term is multiplied by the product of the |
||
# frequencies of all previously occurring symbols |
# frequencies of all previously occurring symbols |
||
bytes.each do |b| |
|||
x = cf[ |
x = cf[b] |
||
lower |
lower = lower * base + x*pf |
||
pf *= freq[ |
pf *= freq[b] |
||
end |
end |
||
Line 732: | Line 718: | ||
var cf = cumulative_freq(freq) |
var cf = cumulative_freq(freq) |
||
# |
# Base |
||
var |
var base = bytes.len |
||
var base = lim+1 |
|||
# Lower bound |
# Lower bound |
||
Line 744: | Line 729: | ||
# Each term is multiplied by the product of the |
# Each term is multiplied by the product of the |
||
# frequencies of all previously occurring symbols |
# frequencies of all previously occurring symbols |
||
bytes.each { |b| |
|||
var x = |
var x = cf{b} |
||
L |
L = L * base + x*pf |
||
pf *= freq{ |
pf *= freq{b} |
||
} |
} |
||