Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
15
Task/Entropy/AWK/entropy-1.awk
Normal file
15
Task/Entropy/AWK/entropy-1.awk
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/awk -f
|
||||
{
|
||||
for (i=1; i<= length($0); i++) {
|
||||
H[substr($0,i,1)]++;
|
||||
N++;
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
for (i in H) {
|
||||
p = H[i]/N;
|
||||
E -= p * log(p);
|
||||
}
|
||||
print E/log(2);
|
||||
}
|
||||
2
Task/Entropy/AWK/entropy-2.awk
Normal file
2
Task/Entropy/AWK/entropy-2.awk
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
echo 1223334444 |./entropy.awk
|
||||
1.84644
|
||||
25
Task/Entropy/Aime/entropy.aime
Normal file
25
Task/Entropy/Aime/entropy.aime
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
integer i, l;
|
||||
record r;
|
||||
real h, x;
|
||||
text s;
|
||||
|
||||
s = argv(1);
|
||||
l = length(s);
|
||||
|
||||
i = l;
|
||||
while (i) {
|
||||
i -= 1;
|
||||
rn_a_integer(r, cut(s, i, 1), 1);
|
||||
}
|
||||
|
||||
h = 0;
|
||||
if (r_first(r, s)) {
|
||||
do {
|
||||
x = r_q_integer(r, s);
|
||||
x /= l;
|
||||
h -= x * log2(x);
|
||||
} while (r_greater(r, s, s));
|
||||
}
|
||||
|
||||
o_real(6, h);
|
||||
o_newline();
|
||||
26
Task/Entropy/C++/entropy.cpp
Normal file
26
Task/Entropy/C++/entropy.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
double log2( double number ) {
|
||||
return log( number ) / log( 2 ) ;
|
||||
}
|
||||
|
||||
int main( int argc , char *argv[ ] ) {
|
||||
std::string teststring( argv[ 1 ] ) ;
|
||||
std::map<char , int> frequencies ;
|
||||
for ( char c : teststring )
|
||||
frequencies[ c ] ++ ;
|
||||
int numlen = teststring.length( ) ;
|
||||
double infocontent = 0 ;
|
||||
for ( std::pair<char , int> p : frequencies ) {
|
||||
double freq = static_cast<double>( p.second ) / numlen ;
|
||||
infocontent += freq * log2( freq ) ;
|
||||
}
|
||||
infocontent *= -1 ;
|
||||
std::cout << "The information content of " << teststring
|
||||
<< " is " << infocontent << " !\n" ;
|
||||
return 0 ;
|
||||
}
|
||||
9
Task/Entropy/Clojure/entropy-1.clj
Normal file
9
Task/Entropy/Clojure/entropy-1.clj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defn entropy [s]
|
||||
(let [len (count s)
|
||||
freqs (frequencies s)
|
||||
log-of-2 (Math/log 2)]
|
||||
(->> (keys freqs)
|
||||
(map (fn [c]
|
||||
(let [rf (/ (get freqs c) len)]
|
||||
(* -1 rf (/ (Math/log rf) log-of-2)))))
|
||||
(reduce +))))
|
||||
2
Task/Entropy/Clojure/entropy-2.clj
Normal file
2
Task/Entropy/Clojure/entropy-2.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(H "1223334444")
|
||||
1.8464393446710154
|
||||
19
Task/Entropy/Erlang/entropy.erl
Normal file
19
Task/Entropy/Erlang/entropy.erl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
-module( entropy ).
|
||||
|
||||
-export( [shannon/1, task/0] ).
|
||||
|
||||
shannon( String ) -> shannon_information_content( lists:foldl(fun count/2, dict:new(), String), erlang:length(String) ).
|
||||
|
||||
task() -> shannon( "1223334444" ).
|
||||
|
||||
|
||||
|
||||
count( Character, Dict ) -> dict:update_counter( Character, 1, Dict ).
|
||||
|
||||
shannon_information_content( Dict, String_length ) ->
|
||||
{_String_length, Acc} = dict:fold( fun shannon_information_content/3, {String_length, 0.0}, Dict ),
|
||||
Acc / math:log( 2 ).
|
||||
|
||||
shannon_information_content( _Character, How_many, {String_length, Acc} ) ->
|
||||
Frequency = How_many / String_length,
|
||||
{String_length, Acc - (Frequency * math:log(Frequency))}.
|
||||
21
Task/Entropy/Go/entropy.go
Normal file
21
Task/Entropy/Go/entropy.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const s = "1223334444"
|
||||
|
||||
func main() {
|
||||
m := map[rune]float64{}
|
||||
for _, r := range s {
|
||||
m[r]++
|
||||
}
|
||||
hm := 0.
|
||||
for _, c := range m {
|
||||
hm += c * math.Log2(c)
|
||||
}
|
||||
const l = float64(len(s))
|
||||
fmt.Println(math.Log2(l) - hm/l)
|
||||
}
|
||||
11
Task/Entropy/Icon/entropy.icon
Normal file
11
Task/Entropy/Icon/entropy.icon
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
procedure main(a)
|
||||
s := !a | "1223334444"
|
||||
write(H(s))
|
||||
end
|
||||
|
||||
procedure H(s)
|
||||
P := table(0.0)
|
||||
every P[!s] +:= 1.0/*s
|
||||
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
|
||||
return h
|
||||
end
|
||||
7
Task/Entropy/MATLAB/entropy-1.m
Normal file
7
Task/Entropy/MATLAB/entropy-1.m
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function E = entropy(d)
|
||||
if ischar(d), d=abs(d); end;
|
||||
[Y,I,J] = unique(d);
|
||||
H = sparse(J,1,1);
|
||||
p = full(H(H>0))/length(d);
|
||||
E = -sum(p.*log2(p));
|
||||
end;
|
||||
2
Task/Entropy/MATLAB/entropy-2.m
Normal file
2
Task/Entropy/MATLAB/entropy-2.m
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> entropy('1223334444')
|
||||
ans = 1.8464
|
||||
5
Task/Entropy/Perl-6/entropy-1.pl6
Normal file
5
Task/Entropy/Perl-6/entropy-1.pl6
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
sub entropy(@a) {
|
||||
[+] map -> \p { p * -log p }, bag(@a).values »/» +@a;
|
||||
}
|
||||
|
||||
say log(2) R/ entropy '1223334444'.comb;
|
||||
9
Task/Entropy/Perl-6/entropy-2.pl6
Normal file
9
Task/Entropy/Perl-6/entropy-2.pl6
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use MONKEY_TYPING;
|
||||
augment class Bag {
|
||||
method entropy {
|
||||
[+] map -> \p { - p * log p },
|
||||
self.values »/» +self;
|
||||
}
|
||||
}
|
||||
|
||||
say '1223334444'.comb.Bag.entropy / log 2;
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
sub entropy {
|
||||
my %count; $count{$_}++ for @_;
|
||||
my @p = map $_/@_, values %count;
|
||||
my $entropy = 0;
|
||||
$entropy += - $_ * log $_ for @p;
|
||||
for (values %count) {
|
||||
my $p = $_/@_;
|
||||
$entropy -= $p * log $p;
|
||||
}
|
||||
$entropy / log 2
|
||||
}
|
||||
|
||||
|
|
|
|||
34
Task/Entropy/Python/entropy-1.py
Normal file
34
Task/Entropy/Python/entropy-1.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from __future__ import division
|
||||
import math
|
||||
|
||||
def hist(source):
|
||||
hist = {}; l = 0;
|
||||
for e in source:
|
||||
l += 1
|
||||
if e not in hist:
|
||||
hist[e] = 0
|
||||
hist[e] += 1
|
||||
return (l,hist)
|
||||
|
||||
def entropy(hist,l):
|
||||
elist = []
|
||||
for v in hist.values():
|
||||
c = v / l
|
||||
elist.append(-c * math.log(c ,2))
|
||||
return sum(elist)
|
||||
|
||||
def printHist(h):
|
||||
flip = lambda (k,v) : (v,k)
|
||||
h = sorted(h.iteritems(), key = flip)
|
||||
print 'Sym\thi\tfi\tInf'
|
||||
for (k,v) in h:
|
||||
print '%s\t%f\t%f\t%f'%(k,v,v/l,-math.log(v/l, 2))
|
||||
|
||||
|
||||
|
||||
source = "1223334444"
|
||||
(l,h) = hist(source);
|
||||
print '.[Results].'
|
||||
print 'Length',l
|
||||
print 'Entropy:', entropy(h, l)
|
||||
printHist(h)
|
||||
10
Task/Entropy/Python/entropy-2.py
Normal file
10
Task/Entropy/Python/entropy-2.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
>>> import math
|
||||
>>> from collections import Counter
|
||||
>>>
|
||||
>>> def entropy(s):
|
||||
... p, lns = Counter(s), float(len(s))
|
||||
... return -sum( count/lns * math.log(count/lns, 2) for count in p.values())
|
||||
...
|
||||
>>> entropy("1223334444")
|
||||
1.8464393446710154
|
||||
>>>
|
||||
|
|
@ -1,17 +1,23 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
(provide entropy hash-entropy list-entropy digital-entropy)
|
||||
|
||||
(define (log2 x)
|
||||
(/ (log x) (log 2)))
|
||||
(define (hash-entropy h)
|
||||
(define (log2 x) (/ (log x) (log 2)))
|
||||
(define n (for/sum [(c (in-hash-values h))] c))
|
||||
(- (for/sum ([c (in-hash-values h)] #:unless (zero? c))
|
||||
(* (/ c n) (log2 (/ c n))))))
|
||||
|
||||
(define (digits x)
|
||||
(for/list ([c (number->string x)])
|
||||
(- (char->integer c) (char->integer #\0))))
|
||||
(define (list-entropy x) (hash-entropy (samples->hash x)))
|
||||
|
||||
(define (entropy x)
|
||||
(define ds (digits x))
|
||||
(define n (length ds))
|
||||
(- (for/sum ([(d c) (in-hash (samples->hash ds))])
|
||||
(* (/ d n) (log2 (/ d n))))))
|
||||
(define entropy (compose list-entropy string->list))
|
||||
(define digital-entropy (compose entropy number->string))
|
||||
|
||||
(entropy 1223334444)
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
(check-= (entropy "1223334444") 1.8464393446710154 1E-8)
|
||||
(check-= (digital-entropy 1223334444) (entropy "1223334444") 1E-8)
|
||||
(check-= (digital-entropy 1223334444) 1.8464393446710154 1E-8)
|
||||
(check-= (entropy "xggooopppp") 1.8464393446710154 1E-8))
|
||||
|
||||
(module+ main (entropy "1223334444"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue