Entropy: Difference between revisions

32,463 bytes added ,  3 months ago
m
(Add new Common Lisp entry instead of overwriting somebody else's)
imported>Thebeez
 
(92 intermediate revisions by 49 users not shown)
Line 37:
:* [[Entropy/Narcissist]]
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F entropy(source)
DefaultDict[Char, Int] hist
L(c) source
hist[c]++
V r = 0.0
L(v) hist.values()
V c = Float(v) / source.len
r -= c * log2(c)
R r
 
print(entropy(‘1223334444’))</syntaxhighlight>
{{out}}
<pre>
1.84644
</pre>
 
=={{header|Ada}}==
Uses Ada 2012.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions;
 
procedure Count_Entropy is
Line 67 ⟶ 84:
Put(Result, Fore => 1, Aft => 5, Exp => 0);
end;
end Count_Entropy;</langsyntaxhighlight>
 
=={{header|Aime}}==
<syntaxhighlight lang ="aime">integer i, lc;
real h, v;
record r;
real h,index x;
textdata s;
 
s = argv(1);
l = length(s);
 
for (, c in (s = argv(1))) {
i = l;
x[c] += 1r;
while (i) {
i -= 1;
rn_a_integer(r, cut(s, i, 1), 1);
}
 
h = 0;
iffor (r_first(r, s)v in x) {
dov {/= ~s;
h -= v * x = r_q_integerlog2(r, sv);
x /= l;
h -= x * log2(x);
} while (r_greater(r, s, s));
}
 
o_form("/d6/\n", h);</syntaxhighlight>
o_real(6, h);
o_newline();</lang>
Examples:
<pre>$ aime -a tmp/entr 1223334444
Line 104 ⟶ 112:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<lang algol68> # calculate the shannon entropy of a string #
PROC shannon entropy = ( STRING s )REAL:
 
PROC shannon entropy = ( STRING s )REAL:
BEGIN
 
INT string length = ( UPB s - LWB s ) + 1;
# count the occurences of each character #
 
# count the occurances of each character #
 
[ 0 : max abs char ]INT char count;
 
FOR char pos FROM LWB char count TO UPB char count DO
char count[ char pos ] := 0
OD;
 
FOR char pos FROM LWB s TO UPB s DO
char count[ ABS s[ char pos ] ] +:= 1
OD;
 
# calculate the entropy, we use log base 10 and then convert #
# to log base 2 after calculating the sum #
 
REAL entropy := 0;
 
FOR char pos FROM LWB char count TO UPB char count DO
IF char count[ char pos ] /= 0
Line 137 ⟶ 136:
FI
OD;
 
entropy / log( 2 )
END; # shannon entropy #
 
 
 
main:
(
# test the shannon entropy routine #
print( ( shannon entropy( "1223334444" ), newline ) )
 
)
END</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 156 ⟶ 150:
=={{header|ALGOL W}}==
{{trans|ALGOL 68}}
<langsyntaxhighlight lang="algolw">begin
% calculates the shannon entropy of a string %
% strings are fixed length in algol W and the length is part of the %
Line 209 ⟶ 203:
write( shannon_entropy( "1223334444", 10 ) )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 216 ⟶ 210:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
ENTROPY←{-+/R×2⍟R←(+⌿⍵∘.=∪⍵)÷⍴⍵}
 
Line 241 ⟶ 235:
-+/RATIO×2⍟RATIO
1.846439345
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 247 ⟶ 241:
1.846439345
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">entropy: function [s][
t: #[]
loop s 'c [
unless key? t c -> t\[c]: 0
t\[c]: t\[c] + 1
]
result: new 0
loop values t 'x ->
'result - (x//(size s)) * log x//(size s) 2
 
return result
]
 
print entropy "1223334444"</syntaxhighlight>
 
{{out}}
 
<pre>1.846439344671015</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % Entropy(1223334444)
 
Entropy(n)
Line 266 ⟶ 280:
}
return, e
}</langsyntaxhighlight>
{{out}}
<pre>1.846440</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang ="awk">#!/usr/bin/awk -f
{
N = length
for (i=1; i<= length($0); i++) {
for (i = 1; i <= N; ++i)
H[substr($0,i,1)]++;
++H[substr($0, i, 1)]
N++;
}
}
 
END {
for (i in H) {
p S += H[i]/N; * log(H[i])
E print (log(N) -= S p/ N) */ log(p2);
}</syntaxhighlight>
}
print E/log(2);
}</lang>
{{out|Usage}}
<langsyntaxhighlight bashlang="sh"> echo 1223334444 |./entropy.awk
1.84644 </langsyntaxhighlight>
 
=={{header|BASIC}}==
Works with older (unstructured) Microsoft-style BASIC.
<syntaxhighlight lang="basic">10 DEF FN L(X)=LOG(X)/LOG(2)
20 S$="1223334444"
30 U$=""
40 FOR I=1 TO LEN(S$)
50 K=0
60 FOR J=1 TO LEN(U$)
70 IF MID$(U$,J,1)=MID$(S$,I,1) THEN K=1
80 NEXT J
90 IF K=0 THEN U$=U$+MID$(S$,I,1)
100 NEXT I
110 DIM R(LEN(U$)-1)
120 FOR I=1 TO LEN(U$)
130 C=0
140 FOR J=1 TO LEN(S$)
150 IF MID$(U$,I,1)=MID$(S$,J,1) THEN C=C+1
160 NEXT J
170 R(I-1)=(C/LEN(S$))*FN L(C/LEN(S$))
180 NEXT I
190 E=0
200 FOR I=0 TO LEN(U$)-1
210 E=E-R(I)
220 NEXT I
230 PRINT E</syntaxhighlight>
{{out}}
<pre>1.84643935</pre>
 
==={{header|QBasic}}===
<syntaxhighlight lang="qbasic">FUNCTION L (X)
L = LOG(X) / LOG(2)
END FUNCTION
 
S$ = "1223334444"
U$ = ""
FOR I = 1 TO LEN(S$)
K = 0
FOR J = 1 TO LEN(U$)
IF MID$(U$, J, 1) = MID$(S$, I, 1) THEN K = 1
NEXT J
IF K = 0 THEN U$ = U$ + MID$(S$, I, 1)
NEXT I
DIM R(LEN(U$) - 1)
FOR I = 1 TO LEN(U$)
C = 0
FOR J = 1 TO LEN(S$)
IF MID$(U$, I, 1) = MID$(S$, J, 1) THEN C = C + 1
NEXT J
R(I - 1) = (C / LEN(S$)) * L(C / LEN(S$))
NEXT I
E = 0
FOR I = 0 TO LEN(U$) - 1
E = E - R(I)
NEXT I
PRINT E
END</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
<syntaxhighlight lang="basic"> 10 LET X$="1223334444"
20 LET U$=""
30 FOR I=1 TO LEN X$
40 LET K=0
50 FOR J=1 TO LEN U$
60 IF U$(J)=X$(I) THEN LET K=K+1
70 NEXT J
80 IF K=0 THEN LET U$=U$+X$(I)
90 NEXT I
100 DIM R(LEN U$)
110 FOR I=1 TO LEN U$
120 LET C=0
130 FOR J=1 TO LEN X$
140 IF U$(I)=X$(J) THEN LET C=C+1
150 NEXT J
160 LET R(I)=C/LEN X$*LN (C/LEN X$)/LN 2
170 NEXT I
180 LET E=0
190 FOR I=1 TO LEN U$
200 LET E=E-R(I)
210 NEXT I
220 PRINT E</syntaxhighlight>
{{out}}
<pre>1.8464393</pre>
==={{header|uBasic/4tH}}===
{{Trans|QBasic}}
uBasic/4tH is an integer BASIC only. So, fixed point arithmetic is required go fulfill this task. Some loss of precision is unavoidable.
<syntaxhighlight lang="basic">If Info("wordsize") < 64 Then Print "This program requires a 64-bit uBasic" : End
 
s := "1223334444"
u := ""
x := FUNC(_Fln(FUNC(_Ntof(2)))) ' calculate LN(2)
 
For i = 0 TO Len(s)-1
k = 0
For j = 0 TO Len(u)-1
If Peek(u, j) = Peek(s, i) Then k = 1
Next
If k = 0 THEN u = Join(u, Char (Peek (s, i)))
Next
 
Dim @r(Len(u)-1)
 
For i = 0 TO Len(u)-1
c = 0
For J = 0 TO Len(s)-1
If Peek(u, i) = Peek (s, j) Then c = c + 1
Next
q = FUNC(_Fdiv(c, Len(s)))
@r(i) = FUNC(_Fmul(q, FUNC(_Fdiv(FUNC(_Fln(q)), x))))
Next
 
e = 0
For i = 0 To Len(u) - 1
e = e - @r(i)
Next
 
Print Using "+?.####"; FUNC(_Ftoi(e))
 
End
 
_Fln Param (1) : Return (FUNC(_Ln(a@*4))/4)
_Fmul Param (2) : Return ((a@*b@)/16384)
_Fdiv Param (2) : Return ((a@*16384)/b@)
_Ntof Param (1) : Return (a@*16384)
_Ftoi Param (1) : Return ((10000*a@)/16384)
 
_Ln
Param (1)
Local (2)
 
c@=681391
If (a@<32768) Then a@=SHL(a@, 16) : c@=c@-726817
If (a@<8388608) Then a@=SHL(a@, 8) : c@=c@-363409
If (a@<134217728) Then a@=SHL(a@, 4) : c@=c@-181704
If (a@<536870912) Then a@=SHL(a@, 2) : c@=c@-90852
If (a@<1073741824) Then a@=SHL(a@, 1) : c@=c@-45426
b@=a@+SHL(a@, -1) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-26573
b@=a@+SHL(a@, -2) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-14624
b@=a@+SHL(a@, -3) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-7719
b@=a@+SHL(a@, -4) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-3973
b@=a@+SHL(a@, -5) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-2017
b@=a@+SHL(a@, -6) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-1016
b@=a@+SHL(a@, -7) : If (AND(b@, 2147483648)) = 0 Then a@=b@ : c@=c@-510
a@=2147483648-a@;
c@=c@-SHL(a@, -15)
Return (c@)</syntaxhighlight>
{{Out}}
<pre>1.8461
 
0 OK, 0:638</pre>
 
=={{header|BBC BASIC}}==
{{trans|APL}}
<syntaxhighlight lang="bbcbasic">REM >entropy
PRINT FNentropy("1223334444")
END
:
DEF FNentropy(x$)
LOCAL unique$, count%, n%, ratio(), u%, i%, j%
unique$ = ""
n% = LEN x$
FOR i% = 1 TO n%
IF INSTR(unique$, MID$(x$, i%, 1)) = 0 THEN unique$ += MID$(x$, i%, 1)
NEXT
u% = LEN unique$
DIM ratio(u% - 1)
FOR i% = 1 TO u%
count% = 0
FOR j% = 1 TO n%
IF MID$(unique$, i%, 1) = MID$(x$, j%, 1) THEN count% += 1
NEXT
ratio(i% - 1) = (count% / n%) * FNlogtwo(count% / n%)
NEXT
= -SUM(ratio())
:
DEF FNlogtwo(n)
= LN n / LN 2</syntaxhighlight>
{{out}}
<pre>1.84643934</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">H ← -∘(+´⊢×2⋆⁼⊢)∘((+˝⊢=⌜⍷)÷≠)
 
H "1223334444"</syntaxhighlight>
{{out}}
<pre>1.8464393446710154</pre>
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang="burlesque">blsq ) "1223334444"F:u[vv^^{1\/?/2\/LG}m[?*++
1.8464393446710157</langsyntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
 
#define MAXLEN 100 //maximum string length
 
int makehist(unsigned char *S,int *hist,int len){
int wherechar[256];
int i,histlen;
Line 318 ⟶ 515:
return histlen;
}
 
double entropy(int *hist,int histlen,int len){
int i;
Line 328 ⟶ 525:
return H;
}
 
int main(void){
unsigned char S[MAXLEN];
int len,*hist,histlen;
double H;
Line 341 ⟶ 538:
printf("%lf\n",H);
return 0;
}</langsyntaxhighlight>
Examples:
<syntaxhighlight lang="text">$ ./entropy
1223334444
1.846439
$ ./entropy
Rosetta Code is the best site in the world!
3.646513</langsyntaxhighlight>
 
=={{header|C++ sharp|C#}}==
<lang cpp>#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>
 
double log2( double number ) {
return log( number ) / log( 2 ) ;
}
 
int main( int argc , char *argv[ ] ) {
std::string teststring( argv[ 1 ] ) ;
std::map<char , int> frequencies ;
for ( char c : teststring )
frequencies[ c ] ++ ;
int numlen = teststring.length( ) ;
double infocontent = 0 ;
for ( std::pair<char , int> p : frequencies ) {
double freq = static_cast<double>( p.second ) / numlen ;
infocontent += freq * log2( freq ) ;
}
infocontent *= -1 ;
std::cout << "The information content of " << teststring
<< " is " << infocontent << " !\n" ;
return 0 ;
}</lang>
{{out}}
<pre>The information content of 1223334444 is 1.84644 !</pre>
 
=={{header|Clojure}}==
<lang Clojure>(defn entropy [s]
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
(map (fn [[_ v]]
(let [rf (/ v len)]
(-> (Math/log rf) (/ log-2) (* rf) Math/abs))))
(reduce +))))</lang>
{{out}}
<lang Clojure>(entropy "1223334444")
1.8464393446710154</lang>
=={{header|C sharp}}==
Translation of C++.
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 433 ⟶ 589:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>The Entropy of 1223334444 is 1.84643934467102</pre>
Without using Hashtables or Dictionaries:
<langsyntaxhighlight lang="csharp">using System;
namespace Entropy
{
Line 479 ⟶ 635:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>
 
double log2( double number ) {
return log( number ) / log( 2 ) ;
}
 
int main( int argc , char *argv[ ] ) {
std::string teststring( argv[ 1 ] ) ;
std::map<char , int> frequencies ;
for ( char c : teststring )
frequencies[ c ] ++ ;
int numlen = teststring.length( ) ;
double infocontent = 0 ;
for ( std::pair<char , int> p : frequencies ) {
double freq = static_cast<double>( p.second ) / numlen ;
infocontent -= freq * log2( freq ) ;
}
std::cout << "The information content of " << teststring
<< " is " << infocontent << std::endl ;
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>(entropy "1223334444")
The information content of 1223334444 is 1.84644</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn entropy [s]
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
(map (fn [[_ v]]
(let [rf (/ v len)]
(-> (Math/log rf) (/ log-2) (* rf) Math/abs))))
(reduce +))))</syntaxhighlight>
{{out}}
<syntaxhighlight lang="clojure">(entropy "1223334444")
1.8464393446710154</syntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% NOTE: when compiling with Portable CLU,
% this program needs to be merged with 'useful.lib' to get log()
%
% pclu -merge $CLUHOME/lib/useful.lib -compile entropy.clu
 
shannon = proc (s: string) returns (real)
% find the frequency of each character
freq: array[int] := array[int]$fill(0, 256, 0)
for c: char in string$chars(s) do
i: int := char$c2i(c)
freq[i] := freq[i] + 1
end
% calculate the component for each character
h: real := 0.0
rlen: real := real$i2r(string$size(s))
for i: int in array[int]$indexes(freq) do
if freq[i] ~= 0 then
f: real := real$i2r(freq[i]) / rlen
h := h - f * log(f) / log(2.0)
end
end
return (h)
end shannon
 
start_up = proc ()
po: stream := stream$primary_output()
stream$putl(po, f_form(shannon("1223334444"), 1, 6))
end start_up </syntaxhighlight>
{{out}}
<pre>1.846439</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">entropy = (s) ->
freq = (s) ->
result = {}
Line 494 ⟶ 726:
((frq[f]/n for f of frq).reduce ((e, p) -> e - p * Math.log(p)), 0) * Math.LOG2E
 
console.log "The entropy of the string '1223334444' is #{entropy '1223334444'}"</langsyntaxhighlight>
{{out}}
<pre>The entropy of the string '1223334444' is 1.8464393446710157</pre>
 
=={{header|Common Lisp}}==
 
Not very Common Lisp-y version:
<syntaxhighlight lang="lisp">(defun entropy (string)
 
<lang lisp>(defun entropy (string)
(let ((table (make-hash-table :test 'equal))
(entropy 0))
Line 511 ⟶ 741:
(log (/ v (length input-string)) 2))))
table)
entropy))</langsyntaxhighlight>
 
More like Common Lisp version:
 
<langsyntaxhighlight lang="lisp">(defun entropy (string &aux (length (length string)))
(declare (type string string))
(let ((table (make-hash-table)))
Line 522 ⟶ 752:
(- (loop for freq being each hash-value in table
for freq/length = (/ freq length)
sum (* freq/length (log freq/length 2))))))</langsyntaxhighlight>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby"># Method to calculate sum of Float64 array
def sum(array : Array(Float64))
res = 0
array.each do |n|
res += n
end
res
end
 
# Method to calculate which char appears how often
def histogram(source : String)
hist = {} of Char => Int32
l = 0
source.each_char do |e|
if !hist.has_key? e
hist[e] = 0
end
hist[e] += 1
end
return Tuple.new(source.size, hist)
end
 
# Method to calculate entropy from histogram
def entropy(hist : Hash(Char, Int32), l : Int32)
elist = [] of Float64
hist.each do |el|
v = el[1]
c = v / l
elist << (-c * Math.log(c, 2))
end
return sum elist
end
 
source = "1223334444"
hist_res = histogram source
l = hist_res[0]
h = hist_res[1]
puts ".[Results]."
puts "Length: " + l.to_s
puts "Entropy: " + (entropy h, l).to_s</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.math;
 
double entropy(T)(T[] s)
Line 540 ⟶ 812:
void main() {
"1223334444"d.dup.entropy.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>1.84644</pre>
 
=={{header|Delphi}}==
{{libheader| StrUtils}}
{{libheader| Math}}
{{Trans|Pascal}}
Just fix Pascal code to run in Delphi.
<syntaxhighlight lang="delphi">
program Entropytest;
 
uses
StrUtils,
Math;
 
type
FArray = array of CARDINAL;
 
var
strng: string = '1223334444';
 
// list unique characters in a string
function uniquechars(str: string): string;
var
n: CARDINAL;
begin
Result := '';
for n := 1 to length(str) do
if (PosEx(str[n], str, n) > 0) and (PosEx(str[n], Result, 1) = 0) then
Result := Result + str[n];
end;
 
// obtain a list of character-frequencies for a string
// given a string containing its unique characters
function frequencies(str, ustr: string): FArray;
var
u, s, p, o: CARDINAL;
begin
SetLength(Result, Length(ustr) + 1);
p := 0;
for u := 1 to length(ustr) do
for s := 1 to length(str) do
begin
o := p;
p := PosEx(ustr[u], str, s);
if (p > o) then
INC(Result[u]);
end;
end;
 
// Obtain the Shannon entropy of a string
function entropy(s: string): EXTENDED;
var
pf: FArray;
us: string;
i, l: CARDINAL;
begin
us := uniquechars(s);
pf := frequencies(s, us);
l := length(s);
Result := 0.0;
for i := 1 to length(us) do
Result := Result - pf[i] / l * log2(pf[i] / l);
end;
 
begin
Writeln('Entropy of "', strng, '" is ', entropy(strng): 2: 5, ' bits.');
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func entropy s$ .
len d[] 255
for c$ in strchars s$
d[strcode c$] += 1
.
for cnt in d[]
if cnt > 0
prop = cnt / len s$
entr -= (prop * log10 prop / log10 2)
.
.
return entr
.
print entropy "1223334444"
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'hash)
;; counter: hash-table[key]++
Line 564 ⟶ 921:
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
;; by increasing entropy
 
Line 580 ⟶ 937:
(H (for/list ((i 1000_000)) (random 1000_000))) → 19.104028424596976
 
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 36.2x :
<langsyntaxhighlight lang="elena">import system'math.;
import system'collections.;
import system'routines.;
import extensions.;
 
extension $op
{
logTwo()
= self .ln() / 2 .ln.();
}
 
symbolpublic program =()
{
[
var input := console readLine.readLine();
var infoC := 0.0r.;
var table := Dictionary new.new();
input .forEach(::(ch)
[{
var n := table[ch].;
if ($nil == n)
[{
table[ch] := 1.
];}
[else
table[ch] := n + 1.{
table[ch] := n + 1
]. }
};
var freq := 0.
tablevar freq forEach(:letter)= 0;
table.forEach::(letter)
[
{
freq := letter int; realDiv(input length).
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)
].</lang>
}</syntaxhighlight>
{{out}}
<pre>
Line 633 ⟶ 992:
{{works with|Erlang/OTP|18}}
<code>:math.log2</code> was added in OTP 18.
<langsyntaxhighlight lang="elixir">defmodule RC do
def entropy(str) do
leng = String.length(str)
Line 646 ⟶ 1,005:
end
 
IO.inspect RC.entropy("1223334444")</langsyntaxhighlight>
 
{{out}}
Line 654 ⟶ 1,013:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">(defun shannon-entropy (input)
(let ((freq-table (make-hash-table))
(entropy 0)
Line 668 ⟶ 1,027:
(log (/ v length) 2)))))
freq-table)
(- entropy)))</langsyntaxhighlight>
 
{{out}}
Line 675 ⟶ 1,034:
as shown below (type ctrl-j at the end of the first line
and the output will be placed by emacs on the second line).
<langsyntaxhighlight lang="lisp">(shannon-entropy "1223334444")
1.8464393446710154</langsyntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( entropy ).
 
Line 699 ⟶ 1,058:
Frequency = How_many / String_length,
{String_length, Acc - (Frequency * math:log(Frequency))}.
</syntaxhighlight>
</lang>
 
{{out}}
Line 708 ⟶ 1,067:
 
=={{header|Euler Math Toolbox}}==
<langsyntaxhighlight EulerMathToolboxlang="eulermathtoolbox">>function entropy (s) ...
$ v=strtochar(s);
$ m=getmultiplicities(unique(v),v);
Line 715 ⟶ 1,074:
$endfunction
>entropy("1223334444")
1.84643934467</langsyntaxhighlight>
 
=={{header|Excel}}==
This solution uses the <code>LAMBDA</code>, <code>LET</code>, and <code>MAP</code> functions introduced into the Microsoft 365 version of Excel in 2021. The <code>LET</code> function is able to use functions as first class citizens. Taking advantage of this makes the solution much simpler. The solution below looks for the string in cell <code>A1</code>.
<syntaxhighlight lang="excel">
=LET(
_MainS,A1,
_N,LEN(_MainS),
_Chars,UNIQUE(MID(_MainS,SEQUENCE(LEN(_MainS),1,1,1),1)),
calcH,LAMBDA(_c,(_c/_N)*LOG(_c/_N,2)),
getCount,LAMBDA(_i,LEN(_MainS)-LEN(SUBSTITUTE(_MainS,_i,""))),
_CharMap,MAP(_Chars,LAMBDA(a, calcH(getCount(a)))),
-SUM(_CharMap)
)
</syntaxhighlight>
_Chars uses the <code>SEQUENCE</code> function to split the text into an array. The <code>UNIQUE</code> function then returns a list of unique characters in the string.
 
<code>calcH</code> applies the calculation described at the top of the page that will then be summed for each character
 
<code>getCount</code> uses the <code>SUBSTITUTE</code> method to count the occurrences of a character within the string.
 
If you needed to re-use this calculation then you could wrap it in a <code>LAMBDA</code> function within the name manager, changing <code>A1</code> to a variable name (e.g. <code>String</code>):
<syntaxhighlight lang="excel">
ShannonEntropyH2=LAMBDA(String,LET(_MainS,String,_N,LEN(_MainS),_Chars,UNIQUE(MID(_MainS,SEQUENCE(LEN(_MainS),1,1,1),1)),calcH,LAMBDA(_c,(_c/_N)*LOG(_c/_N,2)),getCount,LAMBDA(_i,LEN(_MainS)-LEN(SUBSTITUTE(_MainS,_i,""))),_CharMap,MAP(_Chars,LAMBDA(a, calcH(getCount(a)))),-SUM(_CharMap)))
</syntaxhighlight>
Then you can just use the named lambda. E.g. If A1 = 1223334444 then:
<syntaxhighlight lang="excel">
=ShannonEntropyH2(A1)
</syntaxhighlight>
Returns 1.846439345
 
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let ld x = Math.Log x / Math.Log 2.
Line 729 ⟶ 1,118:
|> Seq.fold (fun e p -> e - p * ld p) 0.
 
printfn "%f" (entropy "1223334444")</langsyntaxhighlight>
{{out}}
<pre>1.846439</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: assocs kernel math math.functions math.statistics
prettyprint sequences ;
IN: rosetta-code.entropy
 
: shannon-entropy ( str -- entropy )
[ length ] [ histogram >alist [ second ] map ] bi
[ swap / ] with map
[ dup log 2 log / * ] map-sum neg ;
"1223334444" shannon-entropy .
"Factor is my favorite programming language." shannon-entropy .</syntaxhighlight>
{{out}}
<pre>
1.846439344671015
4.04291723248433
</pre>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: flog2 ( f -- f ) fln 2e fln f/ ;
 
create freq 256 cells allot
Line 755 ⟶ 1,162:
 
s" 1223334444" entropy f. \ 1.84643934467102 ok
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
 
Please find the GNU/linux compilation instructions along with sample run among the comments at the start of the FORTRAN 2008 source. This program acquires input from the command line argument, thereby demonstrating the fairly new get_command_argument intrinsic subroutine. The expression of the algorithm is a rough translated of the j solution. Thank you.
<syntaxhighlight lang="fortran">
<lang FORTRAN>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Tue May 21 21:43:12
Line 819 ⟶ 1,225:
 
end program shannonEntropy
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 25-06-2015
' compile with: fbc -s console
 
Line 854 ⟶ 1,261:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Char count
Line 867 ⟶ 1,274:
Sort of hacky, but friendly interactive shell isn't really optimized for mathematic tasks (in fact, it doesn't even have associative arrays).
 
<langsyntaxhighlight lang="fishshell">function entropy
for arg in $argv
set name count_$arg
Line 887 ⟶ 1,294:
echo "$entropy / l(2)" | bc -l
end
entropy (echo 1223334444 | fold -w1)</langsyntaxhighlight>
{{out}}
<pre>1.84643934467101549345</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Entropy}}
 
'''Solution'''
 
[[File:Fōrmulæ - Entropy 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Entropy 02.png]]
 
[[File:Fōrmulæ - Entropy 03.png]]
 
[[File:Fōrmulæ - Entropy 04.png]]
 
[[File:Fōrmulæ - Entropy 05.png]]
 
=={{header|Go}}==
===Go: Slice version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 904 ⟶ 1,330:
}
 
// for ASCII strings
func H(data string) (entropy float64) {
if data == "" {
Line 915 ⟶ 1,342:
}
return entropy
}</langsyntaxhighlight>
{{out}}
<pre>
Line 922 ⟶ 1,349:
 
===Go: Map version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 932 ⟶ 1,359:
const s = "1223334444"
 
l := float64(0)
m := map[rune]float64{}
for _, r := range s {
m[r]++
l++
}
var fmhm float64
for _, c := range m {
hm += c * math.Log2(c)
}
const l = float64(len(s))
fmt.Println(math.Log2(l) - hm/l)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 949 ⟶ 1,377:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">String.metaClass.getShannonEntrophy = {
-delegate.inject([:]) { map, v -> map[v] = (map[v] ?: 0) + 1; map }.values().inject(0.0) { sum, v ->
def p = (BigDecimal)v / delegate.size()
sum + p * Math.log(p) / Math.log(2)
}
}</langsyntaxhighlight>
Testing
<langsyntaxhighlight lang="groovy">[ '1223334444': '1.846439344671',
'1223334444555555555': '1.969811065121',
'122333': '1.459147917061',
Line 966 ⟶ 1,394:
println "Checking $s has a shannon entrophy of $expected"
assert sprintf('%.12f', s.shannonEntrophy) == expected
}</langsyntaxhighlight>
{{out}}
<pre>Checking 1223334444 has a shannon entrophy of 1.846439344671
Line 977 ⟶ 1,405:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
 
main = print $ entropy "1223334444"
Line 984 ⟶ 1,412:
entropy = sum . map lg . fq . map genericLength . group . sort
where lg c = -c * logBase 2 c
fq c = let sc = sum c in map (/ sc) c</langsyntaxhighlight>
 
 
Or, inlining with an applicative expression (turns out to be fractionally faster):
 
<syntaxhighlight lang="haskell">import Data.List (genericLength, group, sort)
 
entropy
:: (Ord a, Floating c)
=> [a] -> c
entropy =
sum .
map (negate . ((*) <*> logBase 2)) .
(map =<< flip (/) . sum) . map genericLength . group . sort
 
main :: IO ()
main = print $ entropy "1223334444"</syntaxhighlight>
 
{{out}}
Line 990 ⟶ 1,434:
 
=={{header|Icon}} and {{header|Unicon}}==
 
Hmmm, the 2nd equation sums across the length of the string (for the
example, that would be the sum of 10 terms). However, the answer cited
Line 998 ⟶ 1,441:
description.
 
<langsyntaxhighlight lang="unicon">procedure main(a)
s := !a | "1223334444"
write(H(s))
Line 1,008 ⟶ 1,451:
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
return h
end</langsyntaxhighlight>
 
{{out}}
Line 1,018 ⟶ 1,461:
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j"> entropy=: +/@(-@* 2&^.)@(#/.~ % #)</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="j"> entropy '1223334444'
1.84644
entropy i.256
Line 1,029 ⟶ 1,472:
1
entropy 256$0 1 2 3
2</langsyntaxhighlight>
 
So it looks like entropy is roughly the number of bits which would be needed to ''distinguish between'' each item in the argument (for example, with perfect compression). Note that in some contexts this might not be the same thing as information because the choice of the items themselves might matter. But it's good enough in contexts with a fixed set of symbols.
Line 1,037 ⟶ 1,480:
{{trans|REXX}}
{{works with|Java|7+}}
<langsyntaxhighlight lang="java5">import java.lang.Math;
import java.util.Map;
import java.util.HashMap;
Line 1,087 ⟶ 1,530:
return;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,100 ⟶ 1,543:
 
=={{header|JavaScript}}==
{{works with|ECMA-262ECMAScript (5.1)2015}}
Calculate the entropy of a string by determining the frequency of each character, then summing each character's probability multiplied by the log base 2 of that same probability, taking the negative of the sum.
The proces function builds a histogram of character frequencies then iterates over it.
<syntaxhighlight lang="javascript">// Shannon entropy in bits per symbol.
function entropy(str) {
const len = str.length
 
// Build a frequency map from the string.
The entropy function calls into process and evaluates the frequencies as they're passed back.
const frequencies = Array.from(str)
<lang JavaScript>(function(shannon) {
.reduce((freq, c) => (freq[c] = (freq[c] || 0) + 1) && freq, {})
// Create a dictionary of character frequencies and iterate over it.
 
function process(s, evaluator) {
// Sum the frequency of each character.
var h = Object.create(null), k;
return Object.values(frequencies)
s.split('').forEach(function(c) {
.reduce((sum, f) => sum - f/len * Math.log2(f/len), 0)
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 || {});
// Log the Shannon entropy of a string.
function logEntropy(s) {
console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
}
 
logEntropyconsole.log(entropy('1223334444');) // 1.8464393446710154
console.log(entropy('0')) // 0
logEntropy('0');
console.log(entropy('01')) // 1
logEntropy('01');
console.log(entropy('0123')) // 2
logEntropy('0123');
logEntropyconsole.log(entropy('01234567');) // 3
logEntropyconsole.log(entropy('0123456789abcdef');) // 4</langsyntaxhighlight>
{{out}}
<pre>1.8464393446710154
0
1
2
3
4</pre>
;Another variant
<syntaxhighlight lang="javascript">const entropy = (s) => {
const split = s.split('');
const counter = {};
split.forEach(ch => {
if (!counter[ch]) counter[ch] = 1;
else counter[ch]++;
});
 
 
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);
};</syntaxhighlight>
{{out}}
<pre>Entropy of console.log(entropy("1223334444")); in bits per symbol:// 1.8464393446710154</pre>
Entropy of "0" in bits per symbol: 0
Entropy of "01" in bits per symbol: 1
Entropy of "0123" in bits per symbol: 2
Entropy of "01234567" in bits per symbol: 3
Entropy of "0123456789abcdef" in bits per symbol: 4</pre>
 
=={{header|jq}}==
Line 1,149 ⟶ 1,596:
The helper function, ''counter'', could be defined as an inner function of ''entropy'', but for the sake of clarity and because it is independently useful,
it is defined separately.
<langsyntaxhighlight lang="jq"># Input: an array of strings.
# Output: an object with the strings as keys, the values of which are the corresponding frequencies.
def counter:
Line 1,158 ⟶ 1,605:
(explode | map( [.] | implode ) | counter
| [ .[] | . * log ] | add) as $sum
| ((length|log) - ($sum / length)) / (2|log) ;</langsyntaxhighlight>
 
{{out|Example}}
<langsyntaxhighlight lang="jq">"1223334444" | entropy # => 1.8464393446710154</langsyntaxhighlight>
 
=={{header|Jsish}}==
From Javascript entry.
<syntaxhighlight lang="javascript">/* Shannon entropy, in Jsish */
 
function values(obj:object):array {
var vals = [];
for (var key in obj) vals.push(obj[key]);
return vals;
}
 
function entropy(s) {
var split = s.split('');
var counter = {};
split.forEach(function(ch) {
if (!counter[ch]) counter[ch] = 1;
else counter[ch]++;
});
 
var lengthf = s.length * 1.0;
var counts = values(counter);
return -1 * counts.map(function(count) {
return count / lengthf * (Math.log(count / lengthf) / Math.log(2));
})
.reduce(function(a, b) { return a + b; }
);
};
 
if (Interp.conf('unitTest')) {
; entropy('1223334444');
; entropy('Rosetta Code');
; entropy('password');
}</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish --U entropy.jsi
entropy('1223334444') ==> 1.84643934467102
entropy('Rosetta Code') ==> 3.08496250072116
entropy('password') ==> 2.75</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
A oneliner, probably not efficient on very long strings.
<langsyntaxhighlight Julialang="julia">entropy(s) = -sum(x -> x * log(2, x), [count(x -> x == c, s) / length(s) for c in unique(s)])</lang>
@show entropy("1223334444")
{{Out}}
@show entropy([1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 4, 5])</syntaxhighlight>
<pre>julia> entropy("1223334444")
 
1.8464393446710154</pre>
{{out}}
<pre>entropy("1223334444") = 1.8464393446710154
entropy([1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 4, 5]) = 2.103909910282364</pre>
 
=={{header|K}}==
{{works with|ngn/k}}
<syntaxhighlight lang="k">entropy: {(`ln[#x]-(+/{x*`ln@x}@+/{x=\:?x}x)%#x)%`ln@2}
 
entropy "1223334444"</syntaxhighlight>
{{out}}
<pre>1.8464393446710161</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight scalalang="kotlin">// version 1.0.6
 
fun log2(d: Double) = Math.log(d) / Math.log(2.0)
Line 1,203 ⟶ 1,700:
println("------------------------------------ ------------------")
for (sample in samples) println("${sample.padEnd(36)} -> ${"%18.16f".format(shannon(sample))}")
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 1,217 ⟶ 1,713:
Rosetta Code -> 3.0849625007211556
</pre>
 
=={{header|Ksh}}==
{{works with|ksh93}}
<syntaxhighlight lang="ksh">function entropy {
typeset -i i len=${#1}
typeset -X13 r=0
typeset -Ai counts
 
for ((i = 0; i < len; ++i))
do
counts[${1:i:1}]+=1
done
for i in "${counts[@]}"
do
r+='i * log2(i)'
done
r='log2(len) - r / len'
print -r -- "$r"
}
 
printf '%g\n' "$(entropy '1223334444')"</syntaxhighlight>
{{out}}
<pre>1.84644</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def entropy
 
{def entropy.count
{lambda {:s :c :i}
{let { {:c {/ {A.get :i :c} {A.length :s}}}
} {* :c {log2 :c}}}}}
 
{def entropy.sum
{lambda {:s :c}
{- {+ {S.map {entropy.count :s :c}
{S.serie 0 {- {A.length :c} 1}}}}}}}
 
{lambda {:s}
{entropy.sum {A.split :s} {cdr {W.frequency :s}}}}}
-> entropy
 
The W.frequency function is explained in rosettacode.org/wiki/Letter_frequency#Lambdatalk
 
{def txt 1223334444}
-> txt
{def F {W.frequency {txt}}}
-> F
characters: {car {F}} -> [1,2,3,4]
frequencies: {cdr {F}} -> [1,2,3,4]
{entropy {txt}}
-> 1.8464393446710154
 
{entropy 0}
-> 0
{entropy 00000000000000}
-> 0
{entropy 11111111111111}
-> 0
{entropy 01}
-> 1
{entropy Lambdatalk}
-> 2.8464393446710154
{entropy entropy}
-> 2.807354922057604
{entropy abcdefgh}
-> 3
{entropy Rosetta Code}
-> 3.084962500721156
{entropy Longtemps je me suis couché de bonne heure}
-> 3.8608288771249444
{entropy abcdefghijklmnopqrstuvwxyz}
-> 4.70043971814109
{entropy abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz}
-> 4.70043971814109
 
</syntaxhighlight>
 
=={{header|Lang5}}==
<syntaxhighlight lang="lang5">: -rot rot rot ; [] '__A set : dip swap __A swap 1 compress append '__A
set execute __A -1 extract nip ; : nip swap drop ; : sum '+ reduce ;
: 2array 2 compress ; : comb "" split ; : lensize length nip ;
: <group> #( a -- 'a )
grade subscript dup 's dress distinct strip
length 1 2array reshape swap
'A set
: `filter(*) A in A swap select ;
'`filter apply
;
 
: elements(*) lensize ;
: entropy #( s -- n )
length "<group> 'elements apply" dip /
dup neg swap log * 2 log / sum ;
 
"1223334444" comb entropy . # 1.84643934467102</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim countOfChar( 255) ' all possible one-byte ASCII chars
 
Line 1,252 ⟶ 1,844:
logBase =log( x) /log( 2)
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre> Characters used and the number of occurrences of each
Line 1,261 ⟶ 1,853:
Entropy of '1223334444' is 1.84643934 bits.
The result should be around 1.84644 bits.</pre>
 
 
 
=={{header|Lang5}}==
<lang lang5>: -rot rot rot ; [] '__A set : dip swap __A swap 1 compress append '__A
set execute __A -1 extract nip ; : nip swap drop ; : sum '+ reduce ;
: 2array 2 compress ; : comb "" split ; : lensize length nip ;
: <group> #( a -- 'a )
grade subscript dup 's dress distinct strip
length 1 2array reshape swap
'A set
: `filter(*) A in A swap select ;
'`filter apply
;
 
: elements(*) lensize ;
: entropy #( s -- n )
length "<group> 'elements apply" dip /
dup neg swap log * 2 log / sum ;
 
"1223334444" comb entropy . # 1.84643934467102</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function log2 (x) return math.log(x) / math.log(2) end
 
function entropy (X)
Line 1,302 ⟶ 1,873:
end
 
print(entropy("1223334444"))</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">shE[s_String] := -Plus @@ ((# Log[2., #]) & /@ ((Length /@ Gather[#])/
Length[#]) &[Characters[s]])</langsyntaxhighlight>
{{out|Example}}<langsyntaxhighlight Mathematicalang="mathematica"> shE["1223334444"]
1.84644
shE["Rosetta Code"]
3.08496</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
This version allows for any input vectors,
including strings, floats, negative integers, etc.
<langsyntaxhighlight MATLABlang="matlab">function E = entropy(d)
if ischar(d), d=abs(d); end;
[Y,I,J] = unique(d);
Line 1,321 ⟶ 1,892:
p = full(H(H>0))/length(d);
E = -sum(p.*log2(p));
end; </langsyntaxhighlight>
{{out|Usage}}
<langsyntaxhighlight MATLABlang="matlab">> entropy('1223334444')
ans = 1.8464</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">entropy = function(s)
count = {}
for c in s
if count.hasIndex(c) then count[c] = count[c]+1 else count[c] = 1
end for
sum = 0
for x in count.values
countOverN = x / s.len
sum = sum + countOverN * log(countOverN, 2)
end for
return -sum
end function
 
print entropy("1223334444")</syntaxhighlight>
 
{{out}}
<pre>1.846439</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE Entropy;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
FROM Strings IMPORT Length;
FROM MathLib IMPORT ln;
 
PROCEDURE entropy(s: ARRAY OF CHAR): REAL;
VAR freq: ARRAY [0..255] OF CARDINAL;
i, length: CARDINAL;
h, f: REAL;
BEGIN
(* the entropy of the empty string is zero *)
length := Length(s);
IF length = 0 THEN RETURN 0.0; END;
(* find the frequency of each character *)
FOR i := 0 TO 255 DO freq[i] := 0; END;
FOR i := 0 TO length-1 DO
INC(freq[ORD(s[i])]);
END;
(* calculate the component for each character *)
h := 0.0;
FOR i := 0 TO 255 DO
IF freq[i] # 0 THEN
f := FLOAT(freq[i]) / FLOAT(length);
h := h - f * (ln(f) / ln(2.0));
END;
END;
RETURN h;
END entropy;
 
BEGIN
WriteReal(entropy("1223334444"), 14);
WriteLn;
END Entropy.</syntaxhighlight>
{{out}}
<pre> 1.8464394E+00</pre>
 
=={{header|NetRexx}}==
{{trans|REXX}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols
 
Line 1,401 ⟶ 2,031:
end report
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,417 ⟶ 2,047:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import tables, math
 
proc entropy(s: string): float =
var t = initCountTable[char]()
for c in s: t.inc(c)
for x in t.values: result -= x/s.len * log2(x/s.len)
 
echo entropy("1223334444")</langsyntaxhighlight>
 
 
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Collection;
 
class Entropy {
function : native : GetShannonEntropy(result : String) ~ Float {
frequencies := IntMap->New();
 
each(i : result) {
c := result->Get(i);
 
if(frequencies->Has(c)) {
count := frequencies->Find(c)->As(IntHolder);
count->Set(count->Get() + 1);
}
else {
frequencies->Insert(c, IntHolder->New(1));
};
};
 
length := result->Size();
entropy := 0.0;
 
counts := frequencies->GetValues();
each(i : counts) {
count := counts->Get(i)->As(IntHolder)->Get();
freq := count->As(Float) / length;
entropy += freq * (freq->Log() / 2.0->Log());
};
 
return -1 * entropy;
}
 
function : Main(args : String[]) ~ Nil {
inputs := [
"1223334444",
"1223334444555555555",
"122333",
"1227774444",
"aaBBcccDDDD",
"1234567890abcdefghijklmnopqrstuvwxyz",
"Rosetta Code"];
 
each(i : inputs) {
input := inputs[i];
"Shannon entropy of '{$input}': "->Print();
GetShannonEntropy(inputs[i])->PrintLine();
};
}
}</syntaxhighlight>
 
Output:
<pre>
Shannon entropy of '1223334444': 1.84644
Shannon entropy of '1223334444555555555': 1.96981
Shannon entropy of '122333': 1.45915
Shannon entropy of '1227774444': 1.84644
Shannon entropy of 'aaBBcccDDDD': 1.93626
Shannon entropy of '1234567890abcdefghijklmnopqrstuvwxyz': 5.16993
Shannon entropy of 'Rosetta Code': 3.08496
</pre>
 
=={{header|OCaml}}==
;By using a map, purely functional
<lang ocaml>(* generic OCaml, using a mutable Hashtbl *)
<syntaxhighlight lang="ocaml">module CharMap = Map.Make(Char)
 
let entropy s =
let count map c =
CharMap.update c (function Some n -> Some (n +. 1.) | None -> Some 1.) map
and calc _ n sum =
sum +. n *. Float.log2 n
in
let sum = CharMap.fold calc (String.fold_left count CharMap.empty s) 0.
and len = float (String.length s) in
Float.log2 len -. sum /. len
 
let () =
entropy "1223334444" |> string_of_float |> print_endline</syntaxhighlight>
;By using a mutable Hashtbl
<syntaxhighlight lang="ocaml">
(* pre-bake & return an inner-loop function to bin & assemble a character frequency map *)
let get_fproc (m: (char, int) Hashtbl.t) :(char -> unit) =
Line 1,469 ⟶ 2,163:
 
-1.0 *. List.fold_left (fun b x -> b +. calc x) 0.0 relative_probs
</syntaxhighlight>
</lang>
{{out}}
 
<pre>1.84643934467</pre>
'''output:'''
 
1.84643934467
 
=={{header|Oforth}}==
<syntaxhighlight lang="oforth">: entropy(s) -- f
 
<lang Oforth>: entropy(s) -- f
| freq sz |
s size dup ifZero: [ return ] asFloat ->sz
Line 1,484 ⟶ 2,175:
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
entropy("1223334444") .</langsyntaxhighlight>
 
{{out}}
<pre>1.84643934467102</pre>
<pre>
1.84643934467102
</pre>
 
=={{header|PascalooRexx}}==
{{trans|REXX}}
<syntaxhighlight lang="oorexx">/* REXX */
Numeric Digits 16
Parse Arg s
If s='' Then
s="1223334444"
occ.=0
chars=''
n=0
cn=0
Do i=1 To length(s)
c=substr(s,i,1)
If pos(c,chars)=0 Then Do
cn=cn+1
chars=chars||c
End
occ.c=occ.c+1
n=n+1
End
do ci=1 To cn
c=substr(chars,ci,1)
p.c=occ.c/n
/* say c p.c */
End
e=0
Do ci=1 To cn
c=substr(chars,ci,1)
e=e+p.c*rxcalclog(p.c)/rxcalclog(2)
End
Say s 'Entropy' format(-e,,12)
Exit
 
::requires 'rxmath' LIBRARY </syntaxhighlight>
Free Pascal (http://freepascal.org).
{{out}}
<pre>1223334444 Entropy 1.846439344671</pre>
 
=={{header|PARI/GP}}==
<lang Pascal>
<syntaxhighlight lang="parigp">entropy(s)=s=Vec(s);my(v=vecsort(s,,8));-sum(i=1,#v,(x->x*log(x))(sum(j=1,#s,v[i]==s[j])/#s))/log(2)</syntaxhighlight>
<pre>>entropy("1223334444")
%1 = 1.8464393446710154934341977463050452232</pre>
 
=={{header|Pascal}}==
Free Pascal (http://freepascal.org).
<syntaxhighlight lang="pascal">
PROGRAM entropytest;
 
Line 1,546 ⟶ 2,275:
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,552 ⟶ 2,281:
Entropy of "1223334444" is 1.84644 bits.
</pre>
 
=={{header|PARI/GP}}==
<lang parigp>entropy(s)=s=Vec(s);my(v=vecsort(s,,8));-sum(i=1,#v,(x->x*log(x))(sum(j=1,#s,v[i]==s[j])/#s))/log(2)</lang>
<pre>>entropy("1223334444")
%1 = 1.8464393446710154934341977463050452232</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub entropy {
my %count; $count{$_}++ for @_;
my $entropy = 0;
Line 1,569 ⟶ 2,293:
}
print entropy split //, "1223334444";</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{works with|rakudo|2015-09-09}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Perl 6>sub entropy(@a) {
<span style="color: #008080;">function</span> <span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
[+] map -> \p { p * -log p }, bag(@a).values »/» +@a;
<span style="color: #004080;">sequence</span> <span style="color: #000000;">symbols</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
}
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
say log(2) R/ entropy '1223334444'.comb;</lang>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">,</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">symbols</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">,</span><span style="color: #000000;">si</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">H</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ci</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">N</span>
<span style="color: #000000;">H</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">ci</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">H</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1223334444"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
<pre>1.84643934467102</pre>
1.846439345
</pre>
 
=={{header|PHP}}==
In case we would like to add this function to Perl 6's core, here is one way it could be done:
<syntaxhighlight lang="php"><?php
 
function shannonEntropy($string) {
<lang Perl 6>use MONKEY-TYPING;
$h = 0.0;
augment class Bag {
$len = strlen($string);
method entropy {
foreach (count_chars($string, 1) as $count) {
[+] map -> \p { - p * log p },
$h -= (double) ($count / $len) * log((double) ($count / $len), 2);
self.values »/» +self;
}
return $h;
}
 
$strings = array(
say '1223334444'.comb.Bag.entropy / log 2;</lang>
'1223334444',
'1225554444',
'aaBBcccDDDD',
'122333444455555',
'Rosetta Code',
'1234567890abcdefghijklmnopqrstuvwxyz',
);
 
foreach ($strings AS $string) {
=={{header|Phix}}==
printf(
<lang Phix>function log2(atom v)
'%36s : %s' . PHP_EOL,
return log(v)/log(2)
$string,
end function
number_format(shannonEntropy($string), 6)
);
}</syntaxhighlight>
 
{{out}}
function entropy(sequence s)
<pre> 1223334444 : 1.846439
sequence symbols = {},
counts = {} 1225554444 : 1.846439
aaBBcccDDDD : 1.936260
integer N = length(s)
122333444455555 : 2.149255
for i=1 to N do
Rosetta Code : 3.084963
object si = s[i]
1234567890abcdefghijklmnopqrstuvwxyz : 5.169925</pre>
integer k = find(si,symbols)
 
if k=0 then
=={{header|Picat}}==
symbols = append(symbols,si)
<syntaxhighlight lang="picat">go =>
counts = append(counts,1)
["1223334444",
else
"Rosetta Code is the best site in the world!",
counts[k] += 1
"1234567890abcdefghijklmnopqrstuvwxyz",
end if
"Picat is fun"].map(entropy).println(),
end for
nl.
atom H = 0
 
integer n = length(counts)
% probabilities of each element/character in L
for i=1 to n do
entropy(L) = Entropy =>
atom ci = counts[i]/N
Len = L.length,
H -= ci*log2(ci)
Occ = new_map(), % # of occurrences
end for
foreach(E in L)
return H
Occ.put(E, Occ.get(E,0) + 1)
end function
end,
Entropy = -sum([P2*log2(P2) : _C=P in Occ, P2 = P/Len]).</syntaxhighlight>
 
?entropy("1223334444")</lang>
{{out}}
<pre>[1.846439344671016,3.646513010214172,5.169925001442309,3.251629167387823]</pre>
 
=={{header|PicoLisp}}==
PicoLisp only supports fixed point arithmetic, but it does have the ability to call libc transcendental functions (for log)
<syntaxhighlight lang="picolisp">
(scl 8)
(load "@lib/math.l")
 
(setq LN2 0.693147180559945309417)
 
(de tabulate-chars (Str)
(let Map NIL
(for Ch (chop Str)
(if (assoc Ch Map)
(con @ (inc (cdr @)))
(setq Map (cons (cons Ch 1) Map))))
Map))
 
(de entropy (Str)
(let (
Sz (length Str)
Hist (tabulate-chars Str)
)
(*/
(sum
'((Pair)
(let R (*/ (cdr Pair) 1. Sz)
(- (*/ R (log R) 1.))))
Hist)
1. LN2)))
 
</syntaxhighlight>
{{Out}}
<pre>
: (format (entropy "1223334444") *Scl)
1.846439345
-> "1.84643934"
</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source xref attributes or(!);
/*--------------------------------------------------------------------
* 08.08.2014 Walter Pachl translated from REXX version 1
Line 1,661 ⟶ 2,455:
End;
Put Edit('s='''!!s!!''' Entropy=',-e)(Skip,a,f(15,12));
End;</langsyntaxhighlight>
{{out}}
<pre>s='1223334444' Entropy= 1.846439344671</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function entropy ($string) {
$n = $string.Length
Line 1,676 ⟶ 2,470:
}
entropy "1223334444"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,683 ⟶ 2,477:
 
=={{header|Prolog}}==
 
{{works with|Swi-Prolog|7.3.3}}
 
This solution calculates the run-length encoding of the input string to get the relative frequencies of its characters.
<syntaxhighlight lang="prolog">:-module(shannon_entropy, [shannon_entropy/2]).
 
<lang Prolog>:-module(shannon_entropy, [shannon_entropy/2]).
 
%! shannon_entropy(+String, -Entropy) is det.
Line 1,819 ⟶ 2,610:
,must_be(number, B)
,Sum is A + B.
</syntaxhighlight>
</lang>
 
Example query:
Line 1,829 ⟶ 2,620:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#TESTSTR="1223334444"
NewMap uchar.i() : Define.d e
 
Line 1,850 ⟶ 2,641:
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()</langsyntaxhighlight>
{{out}}
<pre>Entropy of [1223334444] = 1.846439344671015</pre>
Line 1,856 ⟶ 2,647:
=={{header|Python}}==
===Python: Longer version===
<langsyntaxhighlight lang="python">from __future__ import division
import math
 
Line 1,889 ⟶ 2,680:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</langsyntaxhighlight>
 
{{out}}
Line 1,904 ⟶ 2,695:
 
===Python: More succinct version===
 
The <tt>Counter</tt> module is only available in Python >= 2.7.
<syntaxhighlight lang="python">from math import log2
from collections import Counter
 
def entropy(s):
<lang python>>>> import math
p, lns = Counter(s), float(len(s))
>>> from collections import Counter
return log2(lns) - sum(count * log2(count) for count in p.values()) / lns
>>>
 
>>> def entropy(s):
print(entropy("1223334444"))</syntaxhighlight>
... p, lns = Counter(s), float(len(s))
{{out}}
... return -sum( count/lns * math.log(count/lns, 2) for count in p.values())
<pre>1.8464393446710154</pre>
...
>>> entropy("1223334444")
1.8464393446710154
>>> </lang>
 
===Uses Python 2===
<langsyntaxhighlight lang="python">def Entropy(text):
import math
log2=lambda x:math.log(x)/math.log(2)
Line 1,937 ⟶ 2,726:
 
while True:
print Entropy(raw_input('>>>'))</langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="rsplus">
<lang r>entropy = function(s)
entropy <- function(str) {
{freq = prop.table(table(strsplit(s, '')[1]))
vec <-sum(freq * logstrsplit(freqstr, base = 2)"")}[[1]]
N <- length(vec)
p_xi <- table(vec) / N
-sum(p_xi * log(p_xi, 2))
}
</syntaxhighlight>
 
{{out}}
print(entropy("1223334444")) # 1.846439</lang>
<pre>
> entropy("1223334444")
[1] 1.846439
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require math)
(provide entropy hash-entropy list-entropy digital-entropy)
Line 1,969 ⟶ 2,768:
(check-= (entropy "xggooopppp") 1.8464393446710154 1E-8))
 
(module+ main (entropy "1223334444"))</langsyntaxhighlight>
{{out}}
<pre> 1.8464393446710154</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-09}}
<syntaxhighlight lang="raku" line>sub entropy(@a) {
[+] map -> \p { p * -log p }, bag(@a).values »/» +@a;
}
 
say log(2) R/ entropy '1223334444'.comb;</syntaxhighlight>
{{out}}
<pre>1.84643934467102</pre>
 
In case we would like to add this function to Raku's core, here is one way it could be done:
 
<syntaxhighlight lang="raku" line>use MONKEY-TYPING;
augment class Bag {
method entropy {
[+] map -> \p { - p * log p },
self.values »/» +self;
}
}
 
say '1223334444'.comb.Bag.entropy / log 2;</syntaxhighlight>
 
=={{header|REXX}}==
===version 1===
 
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 28.02.2013 Walter Pachl
* 12.03.2013 Walter Pachl typo in log corrected. thanx for testing
Line 2,075 ⟶ 2,897:
Numeric Digits (prec)
r=r+0
Return r </langsyntaxhighlight>
 
<!-- these types of comparisons are not part of this Rosetta Code task, and
<lang rexx>/* REXX ***************************************************************
since the results are identical, why post them?
 
 
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* Test program to compare Versions 1 and 2
* (the latter tweaked to be acceptable by my (oo)Rexx
Line 2,100 ⟶ 2,926:
Say ' '
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>Version 1: 1223334444 Entropy 1.846439344671
Line 2,115 ⟶ 2,941:
 
Version 1: 1234567890abcdefghijklmnopqrstuvwxyz Entropy 5.169925001442
Version 2: 1234567890abcdefghijklmnopqrstuvwxyz Entropy 5.169925001442</pre> !-->
 
===version 2===
Line 2,121 ⟶ 2,947:
 
The &nbsp; '''LOG2''' &nbsp; subroutine is only included here for functionality, not to document how to calculate &nbsp; LOG<sub>2</sub> &nbsp; using REXX.
<langsyntaxhighlight lang="rexx">/*REXX program calculates the information entropy for a givenspecified character string. */
numeric digits 50 length( e() ) % 2 - length(.) /*use 501/2 of the decimal digits forof precisionE. */
parse arg $; if $='' then $=1223334444 1223334444 /*obtain the optional input from the CL*/
#=0; @.= 0; L= length($) /*define handy-dandy REXX variables. */
$$= /*initialize the $$ list. */
do j=1 for L; _= substr($, j, 1) /*process each character in $ string.*/
if @._==0 then do; #= # + 1 /*Unique? Yes, bump character counter.*/
$$= $$ || _ /*add this character to the $$ list. */
end
@._= @._ + 1 /*keep track of this character's count.*/
end /*j*/
sum=0 0 /*calculate info entropy for each char.*/
do i=1 for #; _= substr($$, i, 1) /*obtain a character from unique list. */
sum= sum - @._/L * log2(@._/L) /*add (negatively) the char entropies. */
end /*i*/
 
say ' input string: ' $
say 'string length: ' L
say ' unique chars: ' # ; say
say 'the information entropy of the string ──► ' format(sum,,12) " bits."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
e: e= 2.718281828459045235360287471352662497757247093699959574966967627724076630; return e
log2: procedure; parse arg x 1 ox; ig= x>1.5; ii=0; is=1 - 2 * (ig\==1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
numeric digits digits()+5 /* [↓] precision of E must be≥digits()*/
log2: procedure; parse arg x 1 ox; ig= x>1.5; ii= 0; is= 1 - 2 * (ig\==1)
e=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759
numeric digits do while ig & ox>1.digits()+5; | \ig&ox<.5; call _=e; /*the precision of E domust j=-1; iz=oxbe≥digits(). * _ ** -is/
do while if j>=0 & (ig & iz<ox>1.5 | \ig&iz>ox<.5); then leave; _=_*_ e; izz do j=iz-1; end iz= ox /*j _ */* -is
if j>=0 & ox=izz;(ig & ii=ii+is*2**j;iz<1 | end\ig&iz>.5) /*while*/ then leave; x _=x *_ e** -ii -1_; z=0; izz= _=-1iz; end p=z/*j*/
ox=izz; do kii=1ii+is*2**j; end _=-_/*xwhile*/; zx=z+_/k; x * e** -ii -1; if z=p 0; then leave_= -1; p= z; end /*k*/
do rk=1; _= -_ * x; z= z+ii_/k; if arg()z==2p then returnleave; r p= z; returnend r/log2(2,.)<*k*/lang>
r= z + ii; if arg()==2 then return r; return r / log2(2, .)</syntaxhighlight>
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 1223334444 </tt>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 1223334444 </tt>}}
<pre>
input string: 1223334444
Line 2,159 ⟶ 2,985:
the information entropy of the string ──► 1.846439344671 bits.
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> Rosetta Code </tt>}}
<pre>
input string: Rosetta Code
Line 2,169 ⟶ 2,995:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(8)
entropy = 0
Line 2,202 ⟶ 3,028:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,214 ⟶ 3,040:
</pre>
 
=={{header|RubyRPL}}==
{{works with|RubyHalcyon Calc|14.92.7}}
{| class="wikitable"
<lang ruby>def entropy(s)
! Code
counts = Hash.new(0.0)
! Comments
s.each_char { |c| counts[c] += 1 }
|-
leng = s.length
|
DUP SIZE 2 LN → str len log2
≪ { 255 } 0 CON
1 len '''FOR''' j
str j DUP SUB
NUM DUP2 GET 1 + PUT
'''NEXT'''
0 1 255 '''FOR''' j
'''IF''' OVER j GET
'''THEN''' LAST len / DUP LN log2 / * + '''END'''
'''NEXT'''
NEG SWAP DROP
≫ ≫ '<span style="color:blue">NTROP</span>' STO
|
<span style="color:blue">NTROP</span> ''( "string" -- entropy )''
Initialize local variables
Initialize a vector with 255 counters
For each character in the string...
... increase the counter according to ASCII code
For each non-zero counter
calculate term
Change sign and forget the vector
|}
The following line of code delivers what is required:
"1223334444" <span style="color:blue">NTROP</span>
{{out}}
<pre>
1: 1.84643934467
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def entropy(s)
counts = s.chars.tally
leng = s.length.to_f
counts.values.reduce(0) do |entropy, count|
freq = count / leng
Line 2,227 ⟶ 3,093:
end
 
p entropy("1223334444")</langsyntaxhighlight>
{{out}}
<pre>
1.8464393446710154
</pre>
One-liner, same performance (or better):
<lang ruby>def entropy2(s)
s.each_char.group_by(&:to_s).values.map { |x| x.length / s.length.to_f }.reduce(0) { |e, x| e - x*Math.log2(x) }
end</lang>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">dim chrCnt( 255) ' possible ASCII chars
 
source$ = "1223334444"
Line 2,263 ⟶ 3,125:
print " Entropy of '"; source$; "' is "; entropy; " bits."
 
end</langsyntaxhighlight><pre>
Characters used and times used of each
'1' 1
Line 2,273 ⟶ 3,135:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn entropy(s: &[u8]) -> f32 {
let mut entropy: f32histogram = 0.0[0u64; 256];
let mut histogram = [0; 256];
 
for i&b in 0..s.len() {
histogram.get_mut(s[i]b as usize).map(|v| *v] += 1);
}
 
for i in 0..256 {
if histogram[i] > 0 {
.iter()
let ratio = (histogram[i] as f32 / s.len() as f32) as f32;
.cloned()
entropy -= (ratio * ratio.log2()) as f32;
}.filter(|&h| h != 0)
.map(|h| h as f32 / s.len() as f32)
}
.map(|ratio| -ratio * ratio.log2())
entropy
.sum()
}
 
fn main() {
let arg = std::env::args().nth(1).expect("Need a string.");
println!("Entropy of {} is {}.", arg, entropy(&arg.bytes().collect::<Vec<_>>as_bytes()));
}</langsyntaxhighlight>
{{out}}
<pre>$ ./entropy 1223334444
Line 2,299 ⟶ 3,161:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.math._
 
def entropy( v:String ) = { v
Line 2,310 ⟶ 3,172:
 
// Confirm that "1223334444" has an entropy of about 1.84644
assert( math.round( entropy("1223334444") * 100000 ) * 0.00001 == 1.84644 )</langsyntaxhighlight>
 
=={{header|scheme}}==
A version capable of calculating multidimensional entropy.
<langsyntaxhighlight lang="scheme">
(define (entropy input)
(define (close? a b)
Line 2,357 ⟶ 3,219:
(entropy (list 1 2 2 3 3 3 4 4 4 4))
(entropy (list (list 1 1) (list 1.1 1.1) (list 1.2 1.2) (list 1.3 1.3) (list 1.5 1.5) (list 1.6 1.6)))
</langsyntaxhighlight>
 
{{out}}
Line 2,367 ⟶ 3,229:
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function E = entropy(d)
d=strsplit(d);
n=unique(string(d));
Line 2,386 ⟶ 3,248:
word ='1223334444';
E = entropy(word);
disp('The entropy of '+word+' is '+string(E)+'.');</langsyntaxhighlight>
 
{{out}}
Line 2,392 ⟶ 3,254:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 2,420 ⟶ 3,282:
begin
writeln(entropy("1223334444") digits 5);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,426 ⟶ 3,288:
1.84644
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program shannon_entropy;
print(entropy "1223334444");
 
op entropy(symbols);
hist := {};
loop for symbol in symbols do
hist(symbol) +:= 1;
end loop;
h := 0.0;
loop for count = hist(symbol) do
f := count / #symbols;
h -:= f * log f / log 2;
end loop;
return h;
end op;
end program; </syntaxhighlight>
{{out}}
<pre>1.84643934467102</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func entropy(s) {
var counts = Hash.new;
s.each { |c| counts{c} := 0 ++ };
Line 2,437 ⟶ 3,319:
}
 
say entropy("1223334444");</langsyntaxhighlight>
{{out}}
<pre>1.846439344671015493434197746305045223237</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">val Entropy = fn input =>
let
val N = Real.fromInt (String.size input) ;
val term = fn a => Math.ln (a/N) * a / ( Math.ln 2.0 * N ) ;
val v0 = Vector.tabulate (255,fn i=>0) ;
val freq = Vector.map Real.fromInt (* List.foldr: count occurrences *)
(List.foldr (fn (i,v) => Vector.update( v, ord i, Vector.sub(v,ord i) + 1) ) v0 (explode input) )
in
~ (Vector.foldr (fn (a,s) => if a > 0.0 then term a + s else s) 0.0 freq )
 
end ;</syntaxhighlight>
Entropy "1223334444" ;
val it = 1.846439345: real
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func entropy(of x: String) -> Double {
return x
.reduce(into: [String: Int](), {cur, char in
cur[String(char), default: 0] += 1
})
.values
.map({i in Double(i) / Double(x.count) } as (Int) -> Double)
.map({p in -p * log2(p) } as (Double) -> Double)
.reduce(0.0, +)
}
 
print(entropy(of: "1223334444"))</syntaxhighlight>
 
{{out}}
<pre>1.8464393446710154</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc entropy {str} {
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 2,451 ⟶ 3,369:
}
return $entropy
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl">puts [format "entropy = %.5f" [entropy "1223334444"]]
puts [format "entropy = %.5f" [entropy "Rosetta Code"]]</langsyntaxhighlight>
{{out}}
<pre>
entropy = 1.84644
entropy = 3.08496
</pre>
 
=={{header|V (Vlang)}}==
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
import arrays
 
fn hist(source string) map[string]int {
mut hist := map[string]int{}
for e in source.split('') {
if e !in hist {
hist[e] = 0
}
hist[e]+=1
}
return hist
}
 
fn entropy(hist map[string]int, l int) f64 {
mut elist := []f64{}
for _,v in hist {
c := f64(v) / f64(l)
elist << -c * math.log2(c)
}
return arrays.sum<f64>(elist) or {-1}
}
 
fn main(){
input := "1223334444"
h := hist(input)
e := entropy(h, input.len)
println(e)
}</syntaxhighlight>
{{out}}
<pre>
1.8464393446710152
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">var s = "1223334444"
var m = {}
for (c in s) {
var d = m[c]
m[c] = (d) ? d + 1 : 1
}
var hm = 0
for (k in m.keys) {
var c = m[k]
hm = hm + c * c.log2
}
var l = s.count
System.print(l.log2 - hm/l)</syntaxhighlight>
 
{{out}}
<pre>
1.846439344671
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code real RlOut=48, Ln=54; \intrinsic routines
string 0; \use zero-terminated strings
 
Line 2,488 ⟶ 3,463:
];
 
RlOut(0, Entropy("1223334444"))</langsyntaxhighlight>
{{out}}
<pre>
1.84644
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math;
 
pub fn main() !void {
const stdout = std.io.getStdOut().outStream();
try stdout.print("{d:.12}\n", .{H("1223334444")});
}
 
fn H(s: []const u8) f64 {
var counts = [_]u16{0} ** 256;
for (s) |ch|
counts[ch] += 1;
 
var h: f64 = 0;
for (counts) |c|
if (c != 0) {
const p = @intToFloat(f64, c) / @intToFloat(f64, s.len);
h -= p * math.log2(p);
};
 
return h;
}
</syntaxhighlight>
{{Out}}
<pre>
1.846439344671
</pre>
 
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">fcn entropy(text){
text.pump(Void,fcn(c,freq){ c=c.toAsc(); freq[c]+=1; freq }
.fp1( (0).pump(256,List,0.0).copy() )) // array[256] of 0.0
Line 2,505 ⟶ 3,510:
}
 
entropy("1223334444").println(" bits");</langsyntaxhighlight>
{{out}}
<pre>
Line 2,513 ⟶ 3,518:
=={{header|ZX Spectrum Basic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="zxbasic">10 LET s$="1223334444": LET base=2: LET entropy=0
20 LET sourcelen=LEN s$
30 DIM t(255)
Line 2,524 ⟶ 3,529:
100 IF t(i)<>0 THEN PRINT CHR$ i;TAB (6);t(i): LET prop=t(i)/sourcelen: LET entropy=entropy-(prop*(LN prop)/(LN base))
110 NEXT i
120 PRINT '"The Entropy of """;s$;""" is ";entropy</langsyntaxhighlight>
Anonymous user