Arithmetic coding/As a generalized change of radix: Difference between revisions

simplified the encoding loop a little bit
(added python)
(simplified the encoding loop a little bit)
Line 62:
// frequencies of all previously occurring symbols
bigBase := big.NewInt(int64(base))
bigLim := big.NewInt(int64(base - 1))
 
for i_, c := 0; i < base;range i++chars {
bigLim x := big.NewInt(int64(base - 1)cf[c])
 
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))
pf.Mul(pf, big.NewInt(freq[chars[i]c]))
}
 
Line 256 ⟶ 246:
my %cf = cumulative_freq(\%freq);
 
# Limit and baseBase
my $lim base = Math::BigInt->new($#scalar @chars);
my $base = $lim + 1;
 
# Lower bound
Line 268 ⟶ 257:
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
forforeach (my $ic = 0 ; $i < $base ; $i++(@chars) {
my $x = $cf{$chars[$i]c} * $base**($lim - $i);
$L->baddbmuladd($base, $x * $pf);
$pf->bmul($freq{$chars[$i]c});
}
 
Line 376 ⟶ 365:
my %cf = cumulative_freq(%freq);
 
# Limit and baseBase
my $lim base = @chars.endelems;
my $base = $lim + 1;
 
# Lower bound
Line 388 ⟶ 376:
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
for ^$base@chars -> $ic {
my $x = %cf{@chars[$i]c} * $base**($lim - $i);
$L += $L * $base + $x * $pf;
$pf *= %freq{@chars[$i]c};
}
 
Line 498 ⟶ 486:
cf = cumulative_freq(freq)
 
# Limit and baseBase
lim base = len(bytes)-1
base = lim+1
 
# Lower bound
Line 510 ⟶ 497:
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
for ib in range(base)bytes:
x = cf[bytes[ib]] * base**(lim-i)
lower += lower * base + x*pf
pf *= freq[bytes[i]b]
 
# Upper bound
Line 610 ⟶ 597:
cf = cumulative_freq(freq)
 
# Limit and baseBase
lim base = bytes.size-1
base = lim+1
 
# Lower bound
Line 622 ⟶ 608:
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
(0...base)bytes.each do |ib|
x = cf[bytes[ib]] * base**(lim-i)
lower += lower * base + x*pf
pf *= freq[bytes[i]b]
end
 
Line 732 ⟶ 718:
var cf = cumulative_freq(freq)
 
# Limit and baseBase
var lim base = bytes.endlen
var base = lim+1
 
# Lower bound
Line 744 ⟶ 729:
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
base.rangebytes.each { |ib|
var x = (cf{bytes[i]b} * base**(lim - i))
L += L * base + x*pf
pf *= freq{bytes[i]b}
}
 
Anonymous user