Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Entropy/CLU/entropy.clu
Normal file
29
Task/Entropy/CLU/entropy.clu
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
% NOTE: when compiling with Portable CLU,
|
||||
% this program needs to be merged with 'useful.lib' to get log()
|
||||
%
|
||||
% pclu -merge $CLUHOME/lib/useful.lib -compile entropy.clu
|
||||
|
||||
shannon = proc (s: string) returns (real)
|
||||
% find the frequency of each character
|
||||
freq: array[int] := array[int]$fill(0, 256, 0)
|
||||
for c: char in string$chars(s) do
|
||||
i: int := char$c2i(c)
|
||||
freq[i] := freq[i] + 1
|
||||
end
|
||||
|
||||
% calculate the component for each character
|
||||
h: real := 0.0
|
||||
rlen: real := real$i2r(string$size(s))
|
||||
for i: int in array[int]$indexes(freq) do
|
||||
if freq[i] ~= 0 then
|
||||
f: real := real$i2r(freq[i]) / rlen
|
||||
h := h - f * log(f) / log(2.0)
|
||||
end
|
||||
end
|
||||
return (h)
|
||||
end shannon
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
stream$putl(po, f_form(shannon("1223334444"), 1, 6))
|
||||
end start_up
|
||||
Loading…
Add table
Add a link
Reference in a new issue