Entropy: Difference between revisions

20,452 bytes added ,  3 months ago
m
imported>Thebeez
 
(43 intermediate revisions by 25 users not shown)
Line 39:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F entropy(source)
DefaultDict[Char, Int] hist
L(c) source
Line 49:
R r
 
print(entropy(‘1223334444’))</langsyntaxhighlight>
{{out}}
<pre>
Line 57:
=={{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 84:
Put(Result, Fore => 1, Aft => 5, Exp => 0);
end;
end Count_Entropy;</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer c;
real h, v;
index x;
Line 102:
}
 
o_form("/d6/\n", h);</langsyntaxhighlight>
Examples:
<pre>$ aime -a tmp/entr 1223334444
Line 112:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# calculate the shannon entropy of a string #
PROC shannon entropy = ( STRING s )REAL:
Line 142:
print( ( shannon entropy( "1223334444" ), newline ) )
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 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 203:
write( shannon_entropy( "1223334444", 10 ) )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 210:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
ENTROPY←{-+/R×2⍟R←(+⌿⍵∘.=∪⍵)÷⍴⍵}
 
Line 235:
-+/RATIO×2⍟RATIO
1.846439345
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 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 260 ⟶ 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.
<langsyntaxhighlight lang="basic">10 DEF FN L(X)=LOG(X)/LOG(2)
20 S$="1223334444"
30 U$=""
Line 308 ⟶ 325:
210 E=E-R(I)
220 NEXT I
230 PRINT E</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="basic"> 10 LET X$="1223334444"
20 LET U$=""
30 FOR I=1 TO LEN X$
Line 335 ⟶ 381:
200 LET E=E-R(I)
210 NEXT I
220 PRINT E</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="bbcbasic">REM >entropy
PRINT FNentropy("1223334444")
END
Line 364 ⟶ 477:
:
DEF FNlogtwo(n)
= LN n / LN 2</langsyntaxhighlight>
{{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 396 ⟶ 515:
return histlen;
}
 
double entropy(int *hist,int histlen,int len){
int i;
Line 406 ⟶ 525:
return H;
}
 
int main(void){
unsigned char S[MAXLEN];
int len,*hist,histlen;
double H;
Line 419 ⟶ 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#}}==
Translation of C++.
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 470 ⟶ 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 516 ⟶ 635:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
#include <map>
#include <iostream>
Line 544 ⟶ 663:
<< " is " << infocontent << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>(entropy "1223334444")
Line 550 ⟶ 669:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn entropy [s]
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
Line 556 ⟶ 675:
(let [rf (/ v len)]
(-> (Math/log rf) (/ log-2) (* rf) Math/abs))))
(reduce +))))</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight Clojurelang="clojure">(entropy "1223334444")
1.8464393446710154</langsyntaxhighlight>
 
=={{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 574 ⟶ 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 591 ⟶ 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 602 ⟶ 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 620 ⟶ 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 644 ⟶ 921:
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
;; by increasing entropy
 
Line 660 ⟶ 937:
(H (for/list ((i 1000_000)) (random 1000_000))) → 19.104028424596976
 
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import system'math;
import system'collections;
import system'routines;
Line 682 ⟶ 959:
var table := Dictionary.new();
input.forEach::(ch)
{
var n := table[ch];
Line 696 ⟶ 973:
var freq := 0;
table.forEach::(letter)
{
freq := letter.toInt().realDiv(input.Length);
Line 706 ⟶ 983:
console.printLine("The Entropy of ", input, " is ", infoC)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 715 ⟶ 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 728 ⟶ 1,005:
end
 
IO.inspect RC.entropy("1223334444")</langsyntaxhighlight>
 
{{out}}
Line 736 ⟶ 1,013:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">(defun shannon-entropy (input)
(let ((freq-table (make-hash-table))
(entropy 0)
Line 750 ⟶ 1,027:
(log (/ v length) 2)))))
freq-table)
(- entropy)))</langsyntaxhighlight>
 
{{out}}
Line 757 ⟶ 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 781 ⟶ 1,058:
Frequency = How_many / String_length,
{String_length, Acc - (Frequency * math:log(Frequency))}.
</syntaxhighlight>
</lang>
 
{{out}}
Line 790 ⟶ 1,067:
 
=={{header|Euler Math Toolbox}}==
<langsyntaxhighlight EulerMathToolboxlang="eulermathtoolbox">>function entropy (s) ...
$ v=strtochar(s);
$ m=getmultiplicities(unique(v),v);
Line 797 ⟶ 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 810 ⟶ 1,118:
|> Seq.fold (fun e p -> e - p * ld p) 0.
 
printfn "%f" (entropy "1223334444")</langsyntaxhighlight>
{{out}}
<pre>1.846439</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs kernel math math.functions math.statistics
prettyprint sequences ;
IN: rosetta-code.entropy
Line 825 ⟶ 1,133:
"1223334444" shannon-entropy .
"Factor is my favorite programming language." shannon-entropy .</langsyntaxhighlight>
{{out}}
<pre>
Line 833 ⟶ 1,141:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: flog2 ( f -- f ) fln 2e fln f/ ;
 
create freq 256 cells allot
Line 854 ⟶ 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 918 ⟶ 1,225:
 
end program shannonEntropy
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 25-06-2015
' compile with: fbc -s console
 
Line 954 ⟶ 1,261:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Char count
Line 967 ⟶ 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 987 ⟶ 1,294:
echo "$entropy / l(2)" | bc -l
end
entropy (echo 1223334444 | fold -w1)</langsyntaxhighlight>
{{out}}
<pre>1.84643934467101549345</pre>
Line 993 ⟶ 1,300:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Entropy}}
In [http://wiki.formulae.org/Entropy this] page you can see the solution of this task.
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
[[File:Fōrmulæ - Entropy 01.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
'''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 1,013 ⟶ 1,330:
}
 
// for ASCII strings
func H(data string) (entropy float64) {
if data == "" {
Line 1,024 ⟶ 1,342:
}
return entropy
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,031 ⟶ 1,349:
 
===Go: Map version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,041 ⟶ 1,359:
const s = "1223334444"
 
l := float64(0)
m := map[rune]float64{}
for _, r := range s {
m[r]++
l++
}
var hm float64
Line 1,049 ⟶ 1,369:
hm += c * math.Log2(c)
}
const l = float64(len(s))
fmt.Println(math.Log2(l) - hm/l)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,058 ⟶ 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 1,075 ⟶ 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 1,086 ⟶ 1,405:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
 
main = print $ entropy "1223334444"
Line 1,093 ⟶ 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):
 
<langsyntaxhighlight lang="haskell">import Data.List (genericLength, group, sort)
 
entropy
Line 1,109 ⟶ 1,428:
 
main :: IO ()
main = print $ entropy "1223334444"</langsyntaxhighlight>
 
{{out}}
Line 1,115 ⟶ 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 1,123 ⟶ 1,441:
description.
 
<langsyntaxhighlight lang="unicon">procedure main(a)
s := !a | "1223334444"
write(H(s))
Line 1,133 ⟶ 1,451:
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
return h
end</langsyntaxhighlight>
 
{{out}}
Line 1,143 ⟶ 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,154 ⟶ 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,162 ⟶ 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,212 ⟶ 1,530:
return;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,227 ⟶ 1,545:
{{works with|ECMAScript 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.
<langsyntaxhighlight JavaScriptlang="javascript">// Shannon entropy in bits per symbol.
function entropy(str) {
const len = str.length
Line 1,245 ⟶ 1,563:
console.log(entropy('0123')) // 2
console.log(entropy('01234567')) // 3
console.log(entropy('0123456789abcdef')) // 4</langsyntaxhighlight>
{{out}}
<pre>1.8464393446710154
Line 1,252 ⟶ 1,570:
2
3
4</pre> =={{header|JavaScript}}==
;Another variant
<lang JavaScript>const entropy = (s) => {
<syntaxhighlight lang="javascript">const entropy = (s) => {
const split = s.split('');
const counter = {};
Line 1,267 ⟶ 1,586:
.map(count => count / lengthf * Math.log2(count / lengthf))
.reduce((a, b) => a + b);
};</langsyntaxhighlight>
{{out}}
<pre>console.log(entropy("1223334444")); // 1.8464393446710154</pre>
Line 1,277 ⟶ 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,286 ⟶ 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.
<langsyntaxhighlight lang="javascript">/* Shannon entropy, in Jsish */
 
function values(obj:object):array {
Line 1,322 ⟶ 1,641:
; entropy('Rosetta Code');
; entropy('password');
}</langsyntaxhighlight>
 
{{out}}
Line 1,332 ⟶ 1,651:
=={{header|Julia}}==
{{works with|Julia|0.6}}
<syntaxhighlight lang="julia">entropy(s) = -sum(x -> x * log(2, x), count(x -> x == c, s) / length(s) for c in unique(s))
 
<lang julia>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])</langsyntaxhighlight>
 
{{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,374 ⟶ 1,700:
println("------------------------------------ ------------------")
for (sample in samples) println("${sample.padEnd(36)} -> ${"%18.16f".format(shannon(sample))}")
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 1,388 ⟶ 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}}==
<langsyntaxhighlight lang="scheme">
{def entropy
 
Line 1,441 ⟶ 1,789:
-> 4.70043971814109
 
</syntaxhighlight>
</lang>
 
=={{header|Lang5}}==
<langsyntaxhighlight 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 ;
Line 1,460 ⟶ 1,808:
dup neg swap log * 2 log / sum ;
 
"1223334444" comb entropy . # 1.84643934467102</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim countOfChar( 255) ' all possible one-byte ASCII chars
 
Line 1,496 ⟶ 1,844:
logBase =log( x) /log( 2)
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre> Characters used and the number of occurrences of each
Line 1,507 ⟶ 1,855:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function log2 (x) return math.log(x) / math.log(2) end
 
function entropy (X)
Line 1,525 ⟶ 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,544 ⟶ 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}}==
<langsyntaxhighlight MiniScriptlang="miniscript">entropy = function(s)
count = {}
for c in s
Line 1,563 ⟶ 1,911:
end function
 
print entropy("1223334444")</langsyntaxhighlight>
 
{{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,643 ⟶ 2,031:
end report
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,659 ⟶ 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;
 
<lang objeck>use Collection;
 
class Entropy {
Line 1,717 ⟶ 2,104:
};
}
}</langsyntaxhighlight>
 
Output:
Line 1,731 ⟶ 2,118:
 
=={{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,761 ⟶ 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,776 ⟶ 2,175:
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
entropy("1223334444") .</langsyntaxhighlight>
 
{{out}}
Line 1,783 ⟶ 2,182:
=={{header|ooRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">/* REXX */
Numeric Digits 16
Parse Arg s
Line 1,814 ⟶ 2,213:
Exit
 
::requires 'rxmath' LIBRARY </langsyntaxhighlight>
{{out}}
<pre>1223334444 Entropy 1.846439344671</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight 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)</langsyntaxhighlight>
<pre>>entropy("1223334444")
%1 = 1.8464393446710154934341977463050452232</pre>
 
=={{header|Pascal}}==
 
Free Pascal (http://freepascal.org).
<syntaxhighlight lang="pascal">
 
<lang Pascal>
PROGRAM entropytest;
 
Line 1,878 ⟶ 2,275:
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,886 ⟶ 2,283:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub entropy {
my %count; $count{$_}++ for @_;
my $entropy = 0;
Line 1,896 ⟶ 2,293:
}
print entropy split //, "1223334444";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function log2(atom v)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return log(v)/log(2)
<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>
end function
<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>
function entropy(sequence s)
<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>
sequence symbols = {},
<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>
counts = {}
<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>
integer N = length(s)
<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>
for i=1 to N do
<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>
object si = s[i]
<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>
integer k = find(si,symbols)
<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>
if k=0 then
<span symbols style="color: append(symbols,si)#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>
counts = append(counts,1)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
counts[k] += 1
<span style="color: #004080;">atom</span> <span style="color: #000000;">H</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end if
<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>
end for
<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>
atom H = 0
<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>
integer n = length(counts)
<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>
for i=1 to n do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
atom ci = counts[i]/N
<span style="color: #008080;">return</span> <span style="color: #000000;">H</span>
H -= ci*log2(ci)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end for
return H
<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>
end function
<!--</syntaxhighlight>-->
 
?entropy("1223334444")</lang>
{{out}}
<pre>
Line 1,933 ⟶ 2,329:
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
 
<lang PHP><?php
 
function shannonEntropy($string) {
Line 1,960 ⟶ 2,355:
number_format(shannonEntropy($string), 6)
);
}</langsyntaxhighlight>
 
{{out}}
Line 1,969 ⟶ 2,364:
Rosetta Code : 3.084963
1234567890abcdefghijklmnopqrstuvwxyz : 5.169925</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
["1223334444",
"Rosetta Code is the best site in the world!",
"1234567890abcdefghijklmnopqrstuvwxyz",
"Picat is fun"].map(entropy).println(),
nl.
 
% probabilities of each element/character in L
entropy(L) = Entropy =>
Len = L.length,
Occ = new_map(), % # of occurrences
foreach(E in L)
Occ.put(E, Occ.get(E,0) + 1)
end,
Entropy = -sum([P2*log2(P2) : _C=P in Occ, P2 = P/Len]).</syntaxhighlight>
 
{{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">
<lang PicoLisp>
(scl 8)
(load "@lib/math.l")
Line 1,999 ⟶ 2,414:
1. LN2)))
 
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,007 ⟶ 2,422:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source xref attributes or(!);
/*--------------------------------------------------------------------
* 08.08.2014 Walter Pachl translated from REXX version 1
Line 2,040 ⟶ 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 2,055 ⟶ 2,470:
}
entropy "1223334444"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,062 ⟶ 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 2,198 ⟶ 2,610:
,must_be(number, B)
,Sum is A + B.
</syntaxhighlight>
</lang>
 
Example query:
Line 2,208 ⟶ 2,620:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#TESTSTR="1223334444"
NewMap uchar.i() : Define.d e
 
Line 2,229 ⟶ 2,641:
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()</langsyntaxhighlight>
{{out}}
<pre>Entropy of [1223334444] = 1.846439344671015</pre>
Line 2,235 ⟶ 2,647:
=={{header|Python}}==
===Python: Longer version===
<langsyntaxhighlight lang="python">from __future__ import division
import math
 
Line 2,268 ⟶ 2,680:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</langsyntaxhighlight>
 
{{out}}
Line 2,283 ⟶ 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 2,316 ⟶ 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 2,348 ⟶ 2,768:
(check-= (entropy "xggooopppp") 1.8464393446710154 1E-8))
 
(module+ main (entropy "1223334444"))</langsyntaxhighlight>
{{out}}
<pre> 1.8464393446710154</pre>
Line 2,355 ⟶ 2,775:
(formerly Perl 6)
{{works with|rakudo|2015-09-09}}
<syntaxhighlight lang="raku" perl6line>sub entropy(@a) {
[+] map -> \p { p * -log p }, bag(@a).values »/» +@a;
}
 
say log(2) R/ entropy '1223334444'.comb;</langsyntaxhighlight>
{{out}}
<pre>1.84643934467102</pre>
Line 2,365 ⟶ 2,785:
In case we would like to add this function to Raku's core, here is one way it could be done:
 
<syntaxhighlight lang="raku" perl6line>use MONKEY-TYPING;
augment class Bag {
method entropy {
Line 2,373 ⟶ 2,793:
}
 
say '1223334444'.comb.Bag.entropy / log 2;</langsyntaxhighlight>
 
=={{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,477 ⟶ 2,897:
Numeric Digits (prec)
r=r+0
Return r </langsyntaxhighlight>
 
<!-- these types of comparisons are not part of this Rosetta Code task, and
Line 2,483 ⟶ 2,903:
 
 
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* Test program to compare Versions 1 and 2
* (the latter tweaked to be acceptable by my (oo)Rexx
Line 2,506 ⟶ 2,926:
Say ' '
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>Version 1: 1223334444 Entropy 1.846439344671
Line 2,527 ⟶ 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 length( e() ) % 2 - length(.) /*use 1/2 of the decimal digits of E. */
parse arg $; if $='' then $= 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.*/
Line 2,544 ⟶ 2,964:
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. */
Line 2,556 ⟶ 2,976:
ox=izz; ii=ii+is*2**j; end /*while*/; x= x * e** -ii -1; z= 0; _= -1; p= z
do k=1; _= -_ * x; z= z+_/k; if z=p then leave; p= z; end /*k*/
r= z + ii; if arg()==2 then return r; return r / log2(2, .)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 1223334444 </tt>}}
<pre>
Line 2,565 ⟶ 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,575 ⟶ 2,995:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(8)
entropy = 0
Line 2,608 ⟶ 3,028:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,620 ⟶ 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,633 ⟶ 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,669 ⟶ 3,125:
print " Entropy of '"; source$; "' is "; entropy; " bits."
 
end</langsyntaxhighlight><pre>
Characters used and times used of each
'1' 1
Line 2,679 ⟶ 3,135:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn entropy(s: &[u8]) -> f32 {
let mut histogram = [0u64; 256];
 
Line 2,698 ⟶ 3,154:
let arg = std::env::args().nth(1).expect("Need a string.");
println!("Entropy of {} is {}.", arg, entropy(arg.as_bytes()));
}</langsyntaxhighlight>
{{out}}
<pre>$ ./entropy 1223334444
Line 2,705 ⟶ 3,161:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.math._
 
def entropy( v:String ) = { v
Line 2,716 ⟶ 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,763 ⟶ 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,773 ⟶ 3,229:
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function E = entropy(d)
d=strsplit(d);
n=unique(string(d));
Line 2,792 ⟶ 3,248:
word ='1223334444';
E = entropy(word);
disp('The entropy of '+word+' is '+string(E)+'.');</langsyntaxhighlight>
 
{{out}}
Line 2,798 ⟶ 3,254:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 2,826 ⟶ 3,282:
begin
writeln(entropy("1223334444") digits 5);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,832 ⟶ 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,843 ⟶ 3,319:
}
 
say entropy("1223334444");</langsyntaxhighlight>
{{out}}
<pre>1.846439344671015493434197746305045223237</pre>
 
=={{header|Standard ML}}==
<langsyntaxhighlight Standardlang="standard MLml">val Entropy = fn input =>
let
val N = Real.fromInt (String.size input) ;
Line 2,859 ⟶ 3,335:
~ (Vector.foldr (fn (a,s) => if a > 0.0 then term a + s else s) 0.0 freq )
 
end ;</langsyntaxhighlight>
Entropy "1223334444" ;
val it = 1.846439345: real
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func entropy(of x: String) -> Double {
Line 2,877 ⟶ 3,354:
}
 
print(entropy(of: "1223334444"))</langsyntaxhighlight>
 
{{out}}
Line 2,883 ⟶ 3,360:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc entropy {str} {
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 2,892 ⟶ 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"
{{libheader|Wren-math}}
<lang ecmascript>import "/math" for Math
 
var s = "1223334444"
var m = {}
for (c in s) {
Line 2,916 ⟶ 3,426:
for (k in m.keys) {
var c = m[k]
hm = hm + c *Math c.log2(c)
}
var l = s.count
System.print(Mathl.log2(l) - hm/l)</langsyntaxhighlight>
 
{{out}}
Line 2,927 ⟶ 3,437:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code real RlOut=48, Ln=54; \intrinsic routines
string 0; \use zero-terminated strings
 
Line 2,953 ⟶ 3,463:
];
 
RlOut(0, Entropy("1223334444"))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,960 ⟶ 3,470:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
const math = std.math;
Line 2,983 ⟶ 3,493:
return h;
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,991 ⟶ 3,501:
=={{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 3,000 ⟶ 3,510:
}
 
entropy("1223334444").println(" bits");</langsyntaxhighlight>
{{out}}
<pre>
Line 3,008 ⟶ 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 3,019 ⟶ 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