File Coverage

blib/lib/Shannon/Entropy.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Shannon::Entropy;
2              
3 2     2   250636 use 5.006;
  2         8  
4 2     2   15 use strict;
  2         14  
  2         92  
5 2     2   13 use warnings;
  2         17  
  2         235  
6              
7 2     2   1101 use Import::Export;
  2         47416  
  2         15  
8              
9 2     2   120 use base qw/Import::Export/;
  2         5  
  2         713  
10              
11             our $VERSION = '1.11';
12              
13             our %EX = (
14             entropy => [qw/all/]
15             );
16              
17             sub entropy {
18 5     5 1 273856 my ($entropy, $len, $p, %t) = (0, length($_[0]));
19 5         78 $t{$_}++ foreach split '', $_[0];
20 5         24 foreach (values %t) {
21 57         99 $p = $_/$len;
22 57         103 $entropy -= $p * log $p;
23             }
24 5         63 return $entropy / log 2;
25             }
26              
27             1;
28              
29             __END__
30              
31             =head1 NAME
32              
33             Shannon::Entropy - Calculate the Shannon entropy H of a given input string.
34              
35             =head1 VERSION
36              
37             Version 1.11
38              
39             =cut
40              
41             =head1 SYNOPSIS
42              
43             Calculate the Shannon entropy H of a given input string.
44              
45             use Shannon::Entropy qw/entropy/;
46              
47             entropy('1223334444'); # 1.8464393446710154
48             entropy('0123456789abcdef'); # 4
49              
50             =head2 entropy
51              
52             =head1 AUTHOR
53              
54             Robert Acock, C<< <thisusedtobeanemail at gmail.com> >>
55              
56             =head1 BUGS
57              
58             Please report any bugs or feature requests to C<bug-shannon-entropy at rt.cpan.org>, or through
59             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Shannon-Entropy>. I will be notified, and then you'll
60             automatically be notified of progress on your bug as I make changes.
61              
62             =head1 SUPPORT
63              
64             You can find documentation for this module with the perldoc command.
65              
66             perldoc Shannon::Entropy
67              
68             You can also look for information at:
69              
70             =over 4
71              
72             =item * RT: CPAN's request tracker (report bugs here)
73              
74             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Shannon-Entropy>
75              
76             =item * AnnoCPAN: Annotated CPAN documentation
77              
78             L<http://annocpan.org/dist/Shannon-Entropy>
79              
80             =item * CPAN Ratings
81              
82             L<http://cpanratings.perl.org/d/Shannon-Entropy>
83              
84             =item * Search CPAN
85              
86             L<http://search.cpan.org/dist/Shannon-Entropy/>
87              
88             =back
89              
90             =head1 ACKNOWLEDGEMENTS
91              
92             =head1 LICENSE AND COPYRIGHT
93              
94             Copyright 2018 Robert Acock.
95              
96             This program is free software; you can redistribute it and/or modify it
97             under the terms of the the Artistic License (2.0). You may obtain a
98             copy of the full license at:
99              
100             L<http://www.perlfoundation.org/artistic_license_2_0>
101              
102             Any use, modification, and distribution of the Standard or Modified
103             Versions is governed by this Artistic License. By using, modifying or
104             distributing the Package, you accept this license. Do not use, modify,
105             or distribute the Package, if you do not accept this license.
106              
107             If your Modified Version has been derived from a Modified Version made
108             by someone other than you, you are nevertheless required to ensure that
109             your Modified Version complies with the requirements of this license.
110              
111             This license does not grant you the right to use any trademark, service
112             mark, tradename, or logo of the Copyright Holder.
113              
114             This license includes the non-exclusive, worldwide, free-of-charge
115             patent license to make, have made, use, offer to sell, sell, import and
116             otherwise transfer the Package with respect to any patent claims
117             licensable by the Copyright Holder that are necessarily infringed by the
118             Package. If you institute patent litigation (including a cross-claim or
119             counterclaim) against any party alleging that the Package constitutes
120             direct or contributory patent infringement, then this Artistic License
121             to you shall terminate on the date that such litigation is filed.
122              
123             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
124             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
125             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
126             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
127             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
128             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
129             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
130             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131              
132             =cut
133