File Coverage

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


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