new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
28
Task/Entropy/Ada/entropy.ada
Normal file
28
Task/Entropy/Ada/entropy.ada
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;
|
||||
84
Task/Entropy/REXX/entropy-1.rexx
Normal file
84
Task/Entropy/REXX/entropy-1.rexx
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/* Rexx ***************************************************************
|
||||
* 28.02.2013 Walter Pachl
|
||||
* 12.03.2013 Walter Pachl typo in log corrected. thanx for testing
|
||||
**********************************************************************/
|
||||
s="1223334444"
|
||||
occ.=0
|
||||
n=0
|
||||
Do i=1 To length(s)
|
||||
c=substr(s,i,1)
|
||||
occ.c=occ.c+1
|
||||
n=n+1
|
||||
End
|
||||
do c=1 To 4
|
||||
p.c=occ.c/n
|
||||
say c p.c
|
||||
End
|
||||
e=0
|
||||
Do c=1 To 4
|
||||
e=e+p.c*log(p.c,,2)
|
||||
End
|
||||
Say 'Entropy of' s 'is' (-e)
|
||||
Exit
|
||||
|
||||
log: Procedure
|
||||
/***********************************************************************
|
||||
* Return log(x) -- with specified precision and a specified base
|
||||
* Three different series are used for the ranges 0 to 0.5
|
||||
* 0.5 to 1.5
|
||||
* 1.5 to infinity
|
||||
* 03.09.1992 Walter Pachl
|
||||
***********************************************************************/
|
||||
Parse Arg x,prec,b
|
||||
If prec='' Then prec=9
|
||||
Numeric Digits (2*prec)
|
||||
Numeric Fuzz 3
|
||||
Select
|
||||
When x<=0 Then r='*** invalid argument ***'
|
||||
When x<0.5 Then Do
|
||||
z=(x-1)/(x+1)
|
||||
o=z
|
||||
r=z
|
||||
k=1
|
||||
Do i=3 By 2
|
||||
ra=r
|
||||
k=k+1
|
||||
o=o*z*z
|
||||
r=r+o/i
|
||||
If r=ra Then Leave
|
||||
End
|
||||
r=2*r
|
||||
End
|
||||
When x<1.5 Then Do
|
||||
z=(x-1)
|
||||
o=z
|
||||
r=z
|
||||
k=1
|
||||
Do i=2 By 1
|
||||
ra=r
|
||||
k=k+1
|
||||
o=-o*z
|
||||
r=r+o/i
|
||||
If r=ra Then Leave
|
||||
End
|
||||
End
|
||||
Otherwise /* 1.5<=x */ Do
|
||||
z=(x+1)/(x-1)
|
||||
o=1/z
|
||||
r=o
|
||||
k=1
|
||||
Do i=3 By 2
|
||||
ra=r
|
||||
k=k+1
|
||||
o=o/(z*z)
|
||||
r=r+o/i
|
||||
If r=ra Then Leave
|
||||
End
|
||||
r=2*r
|
||||
End
|
||||
End
|
||||
If b<>'' Then
|
||||
r=r/log(b)
|
||||
Numeric Digits (prec)
|
||||
r=r+0
|
||||
Return r
|
||||
9
Task/Entropy/Ruby/entropy-1.rb
Normal file
9
Task/Entropy/Ruby/entropy-1.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def entropy(s)
|
||||
counts = Hash.new(0)
|
||||
s.each_char { |c| counts[c] += 1 }
|
||||
|
||||
counts.values.reduce(0) do |entropy, count|
|
||||
freq = count / s.length.to_f
|
||||
entropy - freq * Math.log2(freq)
|
||||
end
|
||||
end
|
||||
10
Task/Entropy/Tcl/entropy-1.tcl
Normal file
10
Task/Entropy/Tcl/entropy-1.tcl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
proc entropy {str} {
|
||||
set log2 [expr log(2)]
|
||||
foreach char [split $str ""] {dict incr counts $char}
|
||||
set entropy 0.0
|
||||
foreach count [dict values $counts] {
|
||||
set freq [expr {$count / double([string length $str])}]
|
||||
set entropy [expr {$entropy - $freq * log($freq)/$log2}]
|
||||
}
|
||||
return $entropy
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue