Arithmetic coding/As a generalized change of radix: Difference between revisions
Arithmetic coding/As a generalized change of radix (view source)
Revision as of 09:02, 7 April 2024
, 1 month ago→{{header|11l}}: X.throw
Alextretyak (talk | contribs) (Added 11l) |
Alextretyak (talk | contribs) m (→{{header|11l}}: X.throw) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 17:
{{trans|Python}}
<
[Int = Int] cf
V total = 0
Line 100:
I str != dec
X.throw RuntimeError("\tHowever that is incorrect!")</
{{out}}
Line 112:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.Linq;
Line 240:
}
}
}</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 249:
=={{header|D}}==
{{trans|Go}}
<
import std.bigint;
import std.stdio;
Line 400:
}
}
}</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 408:
=={{header|Go}}==
<
import (
Line 570:
}
}
}</
{{out}}
<pre>
Line 581:
=={{header|Groovy}}==
{{trans|Java}}
<
private static class Triple<A, B, C> {
A a
Line 716:
}
}
}</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 727:
Implementation:
<
aekDict=:verb define
d=. ~.y NB. dictionary lists unique characters
Line 778:
echo 'Decoded:'
echo ' ',":dict (#y) aekDec aekEnc y
)</
Example use:
<
Dictionary:
A 1r6
Line 833:
1150764267498783364 15
Decoded:
TOBEORNOTTOBEORTOBEORNOT</
Note that for this task we use our plaintext to generate our dictionary for decoding. Also note that we use rational numbers, rather than floating point, for our dictionary, because floating point tends to be inexact.
Line 839:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.HashMap;
import java.util.Map;
Line 976:
}
}
}</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 985:
=={{header|JavaScript}}==
{{trans|C#}}
<syntaxhighlight lang="javascript">
function CumulativeFreq(freq) {
let total = 0;
Line 1,072:
}
}
</syntaxhighlight>
{{out}}
<pre>DABDDB => 251 * 10^2
Line 1,081:
=={{header|Julia}}==
Taken from the wikipedia example.
<
d = Dict()
for c in s
Line 1,162:
decode(encoded * "0"^z, dfreq))
end
</
<pre>
DABDDB 251 * 10^2 DABDDB
Line 1,172:
=={{header|Kotlin}}==
{{trans|Go}}
<
import java.math.BigInteger
Line 1,298:
if (str != dec) throw Exception("\tHowever that is incorrect!")
}
}</
{{out}}
Line 1,311:
{{trans|Kotlin}}
{{libheader|bignum}}
<
import bignum
Line 1,385:
let dec = arithmeticDecoding(enc, Radix, pow, freq)
echo &"{str:<25}→ {enc:>19} * {Radix}^{pow}"
doAssert str == dec, "\tHowever that is incorrect!"</
{{out}}
Line 1,394:
=={{header|Perl}}==
<
sub cumulative_freq {
Line 1,503:
die "\tHowever that is incorrect!";
}
}</
{{out}}
<pre>
Line 1,516:
{{trans|Kotlin}}
{{libheader|Phix/mpfr}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,621:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-25s=> %19s * %d^%d %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">str</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">encs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ok</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,632:
=={{header|Python}}==
{{works with|Python|3.1+}}
<
def cumulative_freq(freq):
Line 1,729:
if str != dec:
raise Exception("\tHowever that is incorrect!")</
{{out}}
<pre>
Line 1,740:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my %cf;
my $total = 0;
Line 1,850:
die "\tHowever that is incorrect!";
}
}</
{{out}}
<pre>
Line 1,860:
=={{header|Ruby}}==
<
cf = {}
total = 0
Line 1,968:
raise "\tHowever that is incorrect!"
end
end</
{{out}}
<pre>
Line 1,979:
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/EUNJ0zp/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/DVl840oDS2mFvFQ560fxJAScastie (remote JVM)].
<
val (radix, strings) = (10, Seq("DABDDB", "DABDDBBDDBA", "ABRACADABRA", "TOBEORNOTTOBEORTOBEORNOT"))
val fmt = "%-25s=> %19s * %d^%s"
Line 2,054:
if (str != dec) throw new RuntimeException("\tHowever that is incorrect!")
}
}</
=={{header|Sidef}}==
<
var cf = Hash()
var total = 0
Line 2,164:
die "\tHowever that is incorrect!"
}
}</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 2,173:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Text
Imports Freq = System.Collections.Generic.Dictionary(Of Char, Long)
Line 2,299:
End Sub
End Module</
{{out}}
<pre>DABDDB => 251 * 10^2
Line 2,310:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var cumulativeFreq = Fn.new { |freq|
Line 2,429:
Fmt.print(fmt, str, enc, radix, pow)
if (str != dec) Fiber.abort("\tHowever that is incorrect!")
}</
{{out}}
Line 2,441:
=={{header|zkl}}==
Uses libGMP (GNU MP Bignum Library)
<
fcn cumulativeFreq(freqHash){
Line 2,473:
return(enc,powr,freqHash);
}</
<
enc*=radix.pow(powr);
base:=freqHash.values.sum(0);
Line 2,497:
}
decoded.text // Return the decoded output
}</
<
testStrings:=T(
"DABDDB",
Line 2,511:
if(str!=dec) println("\tHowever that is incorrect!");
}</
{{out}}
<pre>
|