Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -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