35 lines
939 B
Plaintext
35 lines
939 B
Plaintext
include "NSLog.incl"
|
|
|
|
double local fn Entropy( array as CFArrayRef )
|
|
CFMutableDictionaryRef count = fn MutableDictionaryNew
|
|
|
|
for CFStringRef element in array
|
|
if ( count[element] )
|
|
count[element] = @(fn NumberIntegerValue( count[element] ) + 1)
|
|
else
|
|
count[element] = @(1)
|
|
end if
|
|
next
|
|
|
|
double entropy = 0.0
|
|
NSUInteger total = fn ArrayCount( array )
|
|
CFArrayRef valuesArr = fn DictionaryAllValues( count )
|
|
|
|
for CFNumberRef value in valuesArr
|
|
double p = fn NumberDoubleValue( value ) / total
|
|
entropy -= p * log(p)
|
|
next
|
|
return entropy / log(2)
|
|
end fn = 0.0
|
|
|
|
void local fn DoIt
|
|
CFStringRef string = @"1,2,2,3,3,3,4,4,4,4"
|
|
CFArrayRef characters = fn StringComponentsSeparatedByString( string, @"," )
|
|
double result = fn Entropy( characters )
|
|
NSLog( @"Entrophy of \"%@\": %.15f", fn StringByReplacingOccurrencesOfString( string, @",", @"" ), result )
|
|
end fn
|
|
|
|
fn DoIt
|
|
|
|
HandleEvents
|