Entropy: Difference between revisions
Content deleted Content added
m →version 2: fixed closing tag for HTML string. |
m →version 2: added/changed whitespace. |
||
Line 2,527: | Line 2,527: | ||
The '''LOG2''' subroutine is only included here for functionality, not to document how to calculate LOG<sub>2</sub> using REXX. |
The '''LOG2''' subroutine is only included here for functionality, not to document how to calculate LOG<sub>2</sub> using REXX. |
||
<lang rexx>/*REXX program calculates the information entropy for a |
<lang rexx>/*REXX program calculates the information entropy for a specified character string. */ |
||
numeric digits length( e() ) % 2 - length(.) /*use 1/2 of the decimal digits of E. */ |
numeric digits length( e() ) % 2 - length(.) /*use 1/2 of the decimal digits of E. */ |
||
parse arg $; if $='' then $= 1223334444 /*obtain the optional input from the CL*/ |
parse arg $; if $='' then $= 1223334444 /*obtain the optional input from the CL*/ |
||
#=0 |
#=0; @.= 0; L= length($) /*define handy-dandy REXX variables. */ |
||
$$= /*initialize the $$ list. */ |
$$= /*initialize the $$ list. */ |
||
do j=1 for L; _= substr($, j, 1) /*process each character in $ string.*/ |
do j=1 for L; _= substr($, j, 1) /*process each character in $ string.*/ |
||
Line 2,544: | Line 2,544: | ||
say ' input string: ' $ |
say ' input string: ' $ |
||
say 'string length: ' L |
say 'string length: ' L |
||
say ' unique chars: ' # |
say ' unique chars: ' #; say |
||
say 'the information entropy of the string ──► ' format(sum,,12) " bits." |
say 'the information entropy of the string ──► ' format(sum,,12) " bits." |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
Line 2,556: | Line 2,556: | ||
ox=izz; ii=ii+is*2**j; end /*while*/; x= x * e** -ii -1; z= 0; _= -1; p= z |
ox=izz; ii=ii+is*2**j; end /*while*/; x= x * e** -ii -1; z= 0; _= -1; p= z |
||
do k=1; _= -_ * x; z= z+_/k; if z=p then leave; p= z; end /*k*/ |
do k=1; _= -_ * x; z= z+_/k; if z=p then leave; p= z; end /*k*/ |
||
r= z + ii; if arg()==2 then return r; return r / log2(2,.)</lang> |
r= z + ii; if arg()==2 then return r; return r / log2(2, .)</lang> |
||
{{out|output|text= when using the default input of: <tt> 1223334444 </tt>}} |
{{out|output|text= when using the default input of: <tt> 1223334444 </tt>}} |
||
<pre> |
<pre> |