September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,41 +1,42 @@
import system'math.
import system'collections.
import system'routines.
import extensions.
import system'math;
import system'collections;
import system'routines;
import extensions;
extension $op
extension op
{
logTwo
= self ln / 2 ln.
logTwo()
= self.ln() / 2.ln();
}
symbol program =
[
var input := console readLine.
var infoC := 0.0r.
var table := Dictionary new.
public program()
{
var input := console.readLine();
var infoC := 0.0r;
var table := new Dictionary();
input forEach(:ch)
[
var n := table[ch].
if ($nil == n)
[
table[ch] := 1.
];
[
table[ch] := n + 1.
]
].
input.forEach:(ch)
{
var n := table[ch];
if (nil == n)
{
table[ch] := 1
}
else
{
table[ch] := n + 1
}
};
var freq := 0.
table forEach(:letter)
[
freq := letter int; realDiv(input length).
var freq := 0;
table.forEach:(letter)
{
freq := letter.toInt().realDiv(input.Length);
infoC += (freq * freq logTwo).
].
infoC += (freq * freq.logTwo())
};
infoC *= -1.
infoC *= -1;
console printLine("The Entropy of ", input, " is ", infoC).
].
console.printLine("The Entropy of ", input, " is ", infoC)
}

View file

@ -1,31 +1,15 @@
(function(shannon) {
// Create a dictionary of character frequencies and iterate over it.
function process(s, evaluator) {
var h = Object.create(null), k;
s.split('').forEach(function(c) {
h[c] && h[c]++ || (h[c] = 1); });
if (evaluator) for (k in h) evaluator(k, h[k]);
return h;
};
// Measure the entropy of a string in bits per symbol.
shannon.entropy = function(s) {
var sum = 0,len = s.length;
process(s, function(k, f) {
var p = f/len;
sum -= p * Math.log(p) / Math.log(2);
});
return sum;
};
})(window.shannon = window.shannon || {});
const entropy = (s) => {
const split = s.split('');
const counter = {};
split.forEach(ch => {
if (!counter[ch]) counter[ch] = 1;
else counter[ch]++;
});
// Log the Shannon entropy of a string.
function logEntropy(s) {
console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
}
logEntropy('1223334444');
logEntropy('0');
logEntropy('01');
logEntropy('0123');
logEntropy('01234567');
logEntropy('0123456789abcdef');
const lengthf = s.length * 1.0;
const counts = Object.values(counter);
return -1 * counts
.map(count => count / lengthf * Math.log2(count / lengthf))
.reduce((a, b) => a + b);
};

View file

@ -1,3 +1,3 @@
entropy(s) = -sum(x -> x * log(2, x), count(x -> x == c, s) / length(s) for c in unique(s))
@show entropy("1223334444")
@show entropy([1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 4, 5]
@show entropy([1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 4, 5])

View file

@ -1,20 +1,20 @@
fn entropy(s: &[u8]) -> f32 {
let mut entropy: f32 = 0.0;
let mut histogram = [0; 256];
let mut histogram = [0u64; 256];
for i in 0..s.len() {
histogram.get_mut(s[i] as usize).map(|v| *v += 1);
for &b in s {
histogram[b as usize] += 1;
}
for i in 0..256 {
if histogram[i] > 0 {
let ratio = (histogram[i] as f32 / s.len() as f32) as f32;
entropy -= (ratio * ratio.log2()) as f32;
}
}
entropy
histogram
.iter()
.cloned()
.filter(|&h| h != 0)
.map(|h| h as f32 / s.len() as f32)
.map(|ratio| -ratio * ratio.log2())
.sum()
}
fn main() {
let arg = std::env::args().nth(1).expect("Need a string.");
println!("Entropy of {} is {}.", arg, entropy(&arg.bytes().collect::<Vec<_>>()));
println!("Entropy of {} is {}.", arg, entropy(arg.as_bytes()));
}