June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,27 +1,19 @@
|
|||
# calculate the shannon entropy of a string #
|
||||
|
||||
PROC shannon entropy = ( STRING s )REAL:
|
||||
BEGIN
|
||||
# calculate the shannon entropy of a string #
|
||||
PROC shannon entropy = ( STRING s )REAL:
|
||||
BEGIN
|
||||
|
||||
INT string length = ( UPB s - LWB s ) + 1;
|
||||
|
||||
# count the occurances of each character #
|
||||
|
||||
# count the occurences of each character #
|
||||
[ 0 : max abs char ]INT char count;
|
||||
|
||||
FOR char pos FROM LWB char count TO UPB char count DO
|
||||
char count[ char pos ] := 0
|
||||
OD;
|
||||
|
||||
FOR char pos FROM LWB s TO UPB s DO
|
||||
char count[ ABS s[ char pos ] ] +:= 1
|
||||
OD;
|
||||
|
||||
# calculate the entropy, we use log base 10 and then convert #
|
||||
# to log base 2 after calculating the sum #
|
||||
|
||||
REAL entropy := 0;
|
||||
|
||||
FOR char pos FROM LWB char count TO UPB char count DO
|
||||
IF char count[ char pos ] /= 0
|
||||
THEN
|
||||
|
|
@ -30,14 +22,10 @@ PROC shannon entropy = ( STRING s )REAL:
|
|||
entropy -:= probability * log( probability )
|
||||
FI
|
||||
OD;
|
||||
|
||||
entropy / log( 2 )
|
||||
END; # shannon entropy #
|
||||
|
||||
|
||||
|
||||
main:
|
||||
(
|
||||
# test the shannon entropy routine #
|
||||
print( ( shannon entropy( "1223334444" ), newline ) )
|
||||
)
|
||||
|
||||
END
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue