package Information;

require Exporter;

use strict;
use Vector;

our (@ISA,@EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(information);

{
    # accepts a list of counts or relative frequencies
    sub information(@) {
	my $sum = Vector::sum(@_);
	return 0 if abs($sum) < 1e-8;
	my $info = 0;
	foreach my $x (@_) {
	    die "Negative count $x" if !($x >= 0);
	    if ($x > 0) {
		my $f = $x/$sum;
		$info -= $f * log($f);
	    }
	}
	return($info);
    }
}

return 1;
