Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
0204458a21 |
57
src/lib.rs
57
src/lib.rs
@ -7,11 +7,11 @@ pub fn shannon_entropy(s: &str) -> f32 {
|
|||||||
return entropy;
|
return entropy;
|
||||||
}
|
}
|
||||||
|
|
||||||
for c in 0..=255u8 {
|
for c in 0..=255 {
|
||||||
let count = s.matches(char::from(c)).count() as f32;
|
let count = s.matches(char::from(c)).count() as f32;
|
||||||
let slen = s.len() as f32;
|
let slen = s.len() as f32;
|
||||||
let p : f32 = count / slen;
|
let p : f32 = count / slen;
|
||||||
println!("{:?} {:?} {:?}", count, slen, p);
|
println!("{}, {:?} {:?} {:?}", c, count, slen, p);
|
||||||
if p > 0.0 {
|
if p > 0.0 {
|
||||||
entropy += - p * p.log2();
|
entropy += - p * p.log2();
|
||||||
}
|
}
|
||||||
@ -85,28 +85,53 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_entropy_equal_distribution1() {
|
fn test_entropy_equal_distribution1() {
|
||||||
let mut bytes = [0u8; 256];
|
let mut stringmerda = String::new();
|
||||||
for i in 0..256 {
|
|
||||||
bytes[i] = i as u8;
|
for _ in 0..2 {
|
||||||
|
for i in 0..=255 {
|
||||||
|
stringmerda.push(char::from(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let h = shannon_entropy(&String::from_utf8_lossy(&bytes));
|
// println!("{:#?}", stringmerda.as_bytes());
|
||||||
assert_eq!(h, 8.0);
|
println!("{}", stringmerda.len());
|
||||||
|
// let h = shannon_entropy(&stringmerda);
|
||||||
|
|
||||||
|
// assert_eq!(h, 8.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
pub fn shannon_entropy2(bytes: &[u8]) -> f32 {
|
||||||
fn test_entropy_equal_distribution2() {
|
let mut entropy = 0.0;
|
||||||
let mut bytes = [0u8; 256*2];
|
let mut counts = [0; 256];
|
||||||
for i in 0..bytes.len() {
|
|
||||||
bytes[i] = (i % 256) as u8;
|
for &b in bytes {
|
||||||
|
counts[b as usize] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let pippo = String::from(&bytes);
|
for &count in counts.iter() {
|
||||||
println!("{:?} {:?}", pippo, pippo.len());
|
if count == 0 { continue }
|
||||||
let h = shannon_entropy(&pippo);
|
|
||||||
assert_eq!(h, 8.0);
|
let p: f32 = (count as f32) / (bytes.len() as f32);
|
||||||
|
entropy -= p * p.log(2.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
entropy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn test_entropy_equal_distribution2() {
|
||||||
|
// let mut bytes = [0u8; 256*2];
|
||||||
|
// for i in 0..bytes.len() {
|
||||||
|
// bytes[i] = (i % 256) as u8;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// println!("{}", bytes.len());
|
||||||
|
// let pippo = String::from_utf8_lossy(&bytes);
|
||||||
|
// println!("{}", pippo.len());
|
||||||
|
// let h = shannon_entropy(&pippo);
|
||||||
|
// assert_eq!(h, 8.0);
|
||||||
|
// }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_entropy_helloworld() {
|
fn test_entropy_helloworld() {
|
||||||
let h = shannon_entropy("hello, world");
|
let h = shannon_entropy("hello, world");
|
||||||
|
Loading…
Reference in New Issue
Block a user