Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
34
Task/Entropy/FutureBasic/entropy.basic
Normal file
34
Task/Entropy/FutureBasic/entropy.basic
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue