Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
28
Task/Entropy/Ada/entropy.adb
Normal file
28
Task/Entropy/Ada/entropy.adb
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
with Ada.Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions;
|
||||
|
||||
procedure Count_Entropy is
|
||||
|
||||
package TIO renames Ada.Text_IO;
|
||||
|
||||
Count: array(Character) of Natural := (others => 0);
|
||||
Sum: Natural := 0;
|
||||
Line: String := "1223334444";
|
||||
|
||||
begin
|
||||
for I in Line'Range loop -- count the characters
|
||||
Count(Line(I)) := Count(Line(I))+1;
|
||||
Sum := Sum + 1;
|
||||
end loop;
|
||||
|
||||
declare -- compute the entropy and print it
|
||||
function P(C: Character) return Float is (Float(Count(C)) / Float(Sum));
|
||||
use Ada.Numerics.Elementary_Functions, Ada.Float_Text_IO;
|
||||
Result: Float := 0.0;
|
||||
begin
|
||||
for Ch in Character loop
|
||||
Result := Result -
|
||||
(if P(Ch)=0.0 then 0.0 else P(Ch) * Log(P(Ch), Base => 2.0));
|
||||
end loop;
|
||||
Put(Result, Fore => 1, Aft => 5, Exp => 0);
|
||||
end;
|
||||
end Count_Entropy;
|
||||
14
Task/Entropy/Agena/entropy.agena
Normal file
14
Task/Entropy/Agena/entropy.agena
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
scope # Show the Shannon entropy of a string
|
||||
|
||||
# the Agena strings.shannon library routine returns three values:
|
||||
# the normalised specific Shannon entropy, the specific Shannon entropy and
|
||||
# the total information entropy
|
||||
# the middle value appears to be the one required for the task
|
||||
local proc shannon( s :: string ) :: number
|
||||
local nse, se, tie := strings.shannon( s );
|
||||
return se
|
||||
end;
|
||||
|
||||
print( shannon( "1223334444" ) )
|
||||
|
||||
epocs
|
||||
40
Task/Entropy/AutoLISP/entropy.lsp
Normal file
40
Task/Entropy/AutoLISP/entropy.lsp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
(defun entropy (str / log2 eCalc strList len ent cnt n new end cPrev)
|
||||
(defun log2 (x)
|
||||
(if (> x 0)
|
||||
(/ (log x) (log 2.0))
|
||||
)
|
||||
)
|
||||
(defun eCalc (cnt len / p)
|
||||
(setq p (/ (float cnt) len))
|
||||
(* p (log2 p))
|
||||
)
|
||||
(setq
|
||||
strList (acad_strlsort (mapcar 'chr (vl-string->list str)))
|
||||
len (length strList)
|
||||
ent 0.0
|
||||
cnt 0
|
||||
n 0
|
||||
)
|
||||
(foreach c strList
|
||||
(setq
|
||||
n (1+ n)
|
||||
new (and cPrev (not (eq c cPrev)))
|
||||
end (= n len)
|
||||
)
|
||||
(if (or new end)
|
||||
(setq
|
||||
cnt (if (and end (not new)) (1+ cnt) cnt)
|
||||
ent (+ ent (eCalc cnt len))
|
||||
cnt 0
|
||||
)
|
||||
)
|
||||
(if (and new end)
|
||||
(setq ent (+ ent (eCalc 1 len)))
|
||||
)
|
||||
(setq
|
||||
cnt (1+ cnt)
|
||||
cPrev c
|
||||
)
|
||||
)
|
||||
(* -1 ent)
|
||||
)
|
||||
15
Task/Entropy/Emacs-Lisp/entropy-1.el
Normal file
15
Task/Entropy/Emacs-Lisp/entropy-1.el
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(defun shannon-entropy (input)
|
||||
(let ((freq-table (make-hash-table))
|
||||
(entropy 0)
|
||||
(length (+ (length input) 0.0)))
|
||||
(mapcar (lambda (x)
|
||||
(puthash x
|
||||
(+ 1 (gethash x freq-table 0))
|
||||
freq-table))
|
||||
input)
|
||||
(maphash (lambda (k v)
|
||||
(set 'entropy (+ entropy
|
||||
(* (/ v length)
|
||||
(log (/ v length) 2)))))
|
||||
freq-table)
|
||||
(- entropy)))
|
||||
2
Task/Entropy/Emacs-Lisp/entropy-2.el
Normal file
2
Task/Entropy/Emacs-Lisp/entropy-2.el
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(shannon-entropy "1223334444")
|
||||
1.8464393446710154
|
||||
11
Task/Entropy/Frink/entropy.frink
Normal file
11
Task/Entropy/Frink/entropy.frink
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
H[x] :=
|
||||
{
|
||||
N = length[x]
|
||||
sum = 0
|
||||
for [symbol, count] = countToDict[charList[x]]
|
||||
sum = sum + count/N log[count/N, 2]
|
||||
|
||||
return negate[sum]
|
||||
}
|
||||
|
||||
println[H["1223334444"]]
|
||||
22
Task/Entropy/Pluto/entropy.pluto
Normal file
22
Task/Entropy/Pluto/entropy.pluto
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
do -- Shannon entropy - translated from the Lua sample
|
||||
|
||||
local function log2 ( x : number ) : number return math.log(x) / math.log(2) end
|
||||
|
||||
local function entropy ( X : string ) : number
|
||||
local N, count, sum, i = X:len(), {}, 0
|
||||
for char = 1, N do
|
||||
i = X[char]
|
||||
if count[i] then
|
||||
count[i] += 1
|
||||
else
|
||||
count[i] = 1
|
||||
end
|
||||
end
|
||||
for n_i, count_i in pairs(count) do
|
||||
sum += count_i / N * log2(count_i / N)
|
||||
end
|
||||
return -sum
|
||||
end
|
||||
|
||||
print( entropy( "1223334444" ) )
|
||||
end
|
||||
9
Task/Entropy/PowerShell/entropy.ps1
Normal file
9
Task/Entropy/PowerShell/entropy.ps1
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function entropy ($string) {
|
||||
$n = $string.Length
|
||||
$string.ToCharArray() | group | foreach{
|
||||
$p = $_.Count/$n
|
||||
$i = [Math]::Log($p,2)
|
||||
-$p*$i
|
||||
} | measure -Sum | foreach Sum
|
||||
}
|
||||
entropy "1223334444"
|
||||
Loading…
Add table
Add a link
Reference in a new issue