Entropy: Difference between revisions

9,128 bytes added ,  3 months ago
m
No edit summary
imported>Thebeez
 
(25 intermediate revisions by 17 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 243:
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">entropy: function [s][
 
<lang rebol>entropy: function [s][
t: #[]
loop s 'c [
Line 257 ⟶ 256:
]
 
print entropy "1223334444"</langsyntaxhighlight>
 
{{out}}
Line 264 ⟶ 263:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % Entropy(1223334444)
 
Entropy(n)
Line 281 ⟶ 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 329 ⟶ 325:
210 E=E-R(I)
220 NEXT I
230 PRINT E</langsyntaxhighlight>
{{out}}
<pre>1.84643935</pre>
 
==={{header|QBasic}}===
<langsyntaxhighlight QBasiclang="qbasic">FUNCTION L (X)
L = LOG(X) / LOG(2)
END FUNCTION
Line 360 ⟶ 356:
NEXT I
PRINT E
END</langsyntaxhighlight>
 
==={{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 385 ⟶ 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 414 ⟶ 477:
:
DEF FNlogtwo(n)
= LN n / LN 2</langsyntaxhighlight>
{{out}}
<pre>1.84643934</pre>
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">H ← -∘(+´⊢×2⋆⁼⊢)∘((+˝⊢=⌜⍷)÷≠)
 
H "1223334444"</langsyntaxhighlight>
{{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>
Line 476 ⟶ 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 527 ⟶ 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 573 ⟶ 635:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
#include <map>
#include <iostream>
Line 601 ⟶ 663:
<< " is " << infocontent << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>(entropy "1223334444")
Line 607 ⟶ 669:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn entropy [s]
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
Line 613 ⟶ 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}}==
<langsyntaxhighlight lang="clu">% NOTE: when compiling with Portable CLU,
% this program needs to be merged with 'useful.lib' to get log()
%
Line 647 ⟶ 709:
po: stream := stream$primary_output()
stream$putl(po, f_form(shannon("1223334444"), 1, 6))
end start_up </langsyntaxhighlight>
{{out}}
<pre>1.846439</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">entropy = (s) ->
freq = (s) ->
result = {}
Line 664 ⟶ 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 681 ⟶ 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 692 ⟶ 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}}==
<langsyntaxhighlight lang="ruby"># Method to calculate sum of Float64 array
def sum(array : Array(Float64))
res = 0
Line 734 ⟶ 794:
puts ".[Results]."
puts "Length: " + l.to_s
puts "Entropy: " + (entropy h, l).to_s</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.math;
 
double entropy(T)(T[] s)
Line 752 ⟶ 812:
void main() {
"1223334444"d.dup.entropy.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>1.84644</pre>
 
=={{header|Delphi}}==
{{libheader| StrUtils}}
Line 760 ⟶ 821:
{{Trans|Pascal}}
Just fix Pascal code to run in Delphi.
<syntaxhighlight lang="delphi">
<lang Delphi>
program Entropytest;
 
Line 820 ⟶ 881:
Writeln('Entropy of "', strng, '" is ', entropy(strng): 2: 5, ' bits.');
readln;
end.</langsyntaxhighlight>
 
=={{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 841 ⟶ 921:
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
;; by increasing entropy
 
Line 857 ⟶ 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 880 ⟶ 959:
var table := Dictionary.new();
input.forEach::(ch)
{
var n := table[ch];
Line 894 ⟶ 973:
var freq := 0;
table.forEach::(letter)
{
freq := letter.toInt().realDiv(input.Length);
Line 904 ⟶ 983:
console.printLine("The Entropy of ", input, " is ", infoC)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 913 ⟶ 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 926 ⟶ 1,005:
end
 
IO.inspect RC.entropy("1223334444")</langsyntaxhighlight>
 
{{out}}
Line 934 ⟶ 1,013:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">(defun shannon-entropy (input)
(let ((freq-table (make-hash-table))
(entropy 0)
Line 948 ⟶ 1,027:
(log (/ v length) 2)))))
freq-table)
(- entropy)))</langsyntaxhighlight>
 
{{out}}
Line 955 ⟶ 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 979 ⟶ 1,058:
Frequency = How_many / String_length,
{String_length, Acc - (Frequency * math:log(Frequency))}.
</syntaxhighlight>
</lang>
 
{{out}}
Line 988 ⟶ 1,067:
 
=={{header|Euler Math Toolbox}}==
<langsyntaxhighlight EulerMathToolboxlang="eulermathtoolbox">>function entropy (s) ...
$ v=strtochar(s);
$ m=getmultiplicities(unique(v),v);
Line 995 ⟶ 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 1,008 ⟶ 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 1,023 ⟶ 1,133:
"1223334444" shannon-entropy .
"Factor is my favorite programming language." shannon-entropy .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,031 ⟶ 1,141:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: flog2 ( f -- f ) fln 2e fln f/ ;
 
create freq 256 cells allot
Line 1,052 ⟶ 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 1,116 ⟶ 1,225:
 
end program shannonEntropy
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 25-06-2015
' compile with: fbc -s console
 
Line 1,152 ⟶ 1,261:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Char count
Line 1,165 ⟶ 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 1,185 ⟶ 1,294:
echo "$entropy / l(2)" | bc -l
end
entropy (echo 1223334444 | fold -w1)</langsyntaxhighlight>
{{out}}
<pre>1.84643934467101549345</pre>
Line 1,191 ⟶ 1,300:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Entropy}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Entropy 01.png]]
In '''[https://formulae.org/?example=Entropy this]''' page you can see the program(s) related to this task and their results.
 
'''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,211 ⟶ 1,330:
}
 
// for ASCII strings
func H(data string) (entropy float64) {
if data == "" {
Line 1,222 ⟶ 1,342:
}
return entropy
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,229 ⟶ 1,349:
 
===Go: Map version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,239 ⟶ 1,359:
const s = "1223334444"
 
l := float64(0)
m := map[rune]float64{}
for _, r := range s {
m[r]++
l++
}
var hm float64
Line 1,247 ⟶ 1,369:
hm += c * math.Log2(c)
}
const l = float64(len(s))
fmt.Println(math.Log2(l) - hm/l)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,256 ⟶ 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,273 ⟶ 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,284 ⟶ 1,405:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
 
main = print $ entropy "1223334444"
Line 1,291 ⟶ 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,307 ⟶ 1,428:
 
main :: IO ()
main = print $ entropy "1223334444"</langsyntaxhighlight>
 
{{out}}
Line 1,313 ⟶ 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,321 ⟶ 1,441:
description.
 
<langsyntaxhighlight lang="unicon">procedure main(a)
s := !a | "1223334444"
write(H(s))
Line 1,331 ⟶ 1,451:
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
return h
end</langsyntaxhighlight>
 
{{out}}
Line 1,341 ⟶ 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,352 ⟶ 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,360 ⟶ 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,410 ⟶ 1,530:
return;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,425 ⟶ 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,443 ⟶ 1,563:
console.log(entropy('0123')) // 2
console.log(entropy('01234567')) // 3
console.log(entropy('0123456789abcdef')) // 4</langsyntaxhighlight>
{{out}}
<pre>1.8464393446710154
Line 1,450 ⟶ 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,465 ⟶ 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,475 ⟶ 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,484 ⟶ 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,520 ⟶ 1,641:
; entropy('Rosetta Code');
; entropy('password');
}</langsyntaxhighlight>
 
{{out}}
Line 1,530 ⟶ 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,572 ⟶ 1,700:
println("------------------------------------ ------------------")
for (sample in samples) println("${sample.padEnd(36)} -> ${"%18.16f".format(shannon(sample))}")
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 1,586 ⟶ 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,639 ⟶ 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,658 ⟶ 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,694 ⟶ 1,844:
logBase =log( x) /log( 2)
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre> Characters used and the number of occurrences of each
Line 1,705 ⟶ 1,855:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function log2 (x) return math.log(x) / math.log(2) end
 
function entropy (X)
Line 1,723 ⟶ 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,742 ⟶ 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,761 ⟶ 1,911:
end function
 
print entropy("1223334444")</langsyntaxhighlight>
 
{{out}}
Line 1,767 ⟶ 1,917:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Entropy;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
Line 1,802 ⟶ 1,952:
WriteReal(entropy("1223334444"), 14);
WriteLn;
END Entropy.</langsyntaxhighlight>
{{out}}
<pre> 1.8464394E+00</pre>
 
=={{header|NetRexx}}==
{{trans|REXX}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols
 
Line 1,880 ⟶ 2,031:
end report
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,896 ⟶ 2,047:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import tables, math
 
proc entropy(s: string): float =
Line 1,903 ⟶ 2,054:
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,954 ⟶ 2,104:
};
}
}</langsyntaxhighlight>
 
Output:
Line 1,968 ⟶ 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,998 ⟶ 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 2,013 ⟶ 2,175:
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
entropy("1223334444") .</langsyntaxhighlight>
 
{{out}}
Line 2,020 ⟶ 2,182:
=={{header|ooRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">/* REXX */
Numeric Digits 16
Parse Arg s
Line 2,051 ⟶ 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 2,115 ⟶ 2,275:
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,123 ⟶ 2,283:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub entropy {
my %count; $count{$_}++ for @_;
my $entropy = 0;
Line 2,133 ⟶ 2,293:
}
print entropy split //, "1223334444";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 2,162 ⟶ 2,322:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,169 ⟶ 2,329:
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
 
<lang PHP><?php
 
function shannonEntropy($string) {
Line 2,196 ⟶ 2,355:
number_format(shannonEntropy($string), 6)
);
}</langsyntaxhighlight>
 
{{out}}
Line 2,207 ⟶ 2,366:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
["1223334444",
"Rosetta Code is the best site in the world!",
Line 2,213 ⟶ 2,372:
"Picat is fun"].map(entropy).println(),
nl.
 
 
% probabilities of each element/character in L
Line 2,222 ⟶ 2,380:
Occ.put(E, Occ.get(E,0) + 1)
end,
Entropy = -sum([P2*log2(P2) : _C=P in Occ, P2 = P/Len]).</langsyntaxhighlight>
 
{{out}}
Output:
<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 2,257 ⟶ 2,414:
1. LN2)))
 
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,265 ⟶ 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,298 ⟶ 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,313 ⟶ 2,470:
}
entropy "1223334444"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,320 ⟶ 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,456 ⟶ 2,610:
,must_be(number, B)
,Sum is A + B.
</syntaxhighlight>
</lang>
 
Example query:
Line 2,466 ⟶ 2,620:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#TESTSTR="1223334444"
NewMap uchar.i() : Define.d e
 
Line 2,487 ⟶ 2,641:
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()</langsyntaxhighlight>
{{out}}
<pre>Entropy of [1223334444] = 1.846439344671015</pre>
Line 2,493 ⟶ 2,647:
=={{header|Python}}==
===Python: Longer version===
<langsyntaxhighlight lang="python">from __future__ import division
import math
 
Line 2,526 ⟶ 2,680:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</langsyntaxhighlight>
 
{{out}}
Line 2,541 ⟶ 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,574 ⟶ 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,606 ⟶ 2,768:
(check-= (entropy "xggooopppp") 1.8464393446710154 1E-8))
 
(module+ main (entropy "1223334444"))</langsyntaxhighlight>
{{out}}
<pre> 1.8464393446710154</pre>
Line 2,613 ⟶ 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,623 ⟶ 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,631 ⟶ 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,735 ⟶ 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,741 ⟶ 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,764 ⟶ 2,926:
Say ' '
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>Version 1: 1223334444 Entropy 1.846439344671
Line 2,785 ⟶ 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 specified 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*/
Line 2,814 ⟶ 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,833 ⟶ 2,995:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(8)
entropy = 0
Line 2,866 ⟶ 3,028:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,878 ⟶ 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,891 ⟶ 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,927 ⟶ 3,125:
print " Entropy of '"; source$; "' is "; entropy; " bits."
 
end</langsyntaxhighlight><pre>
Characters used and times used of each
'1' 1
Line 2,937 ⟶ 3,135:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn entropy(s: &[u8]) -> f32 {
let mut histogram = [0u64; 256];
 
Line 2,956 ⟶ 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,963 ⟶ 3,161:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.math._
 
def entropy( v:String ) = { v
Line 2,974 ⟶ 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 3,021 ⟶ 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 3,031 ⟶ 3,229:
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function E = entropy(d)
d=strsplit(d);
n=unique(string(d));
Line 3,050 ⟶ 3,248:
word ='1223334444';
E = entropy(word);
disp('The entropy of '+word+' is '+string(E)+'.');</langsyntaxhighlight>
 
{{out}}
Line 3,056 ⟶ 3,254:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 3,084 ⟶ 3,282:
begin
writeln(entropy("1223334444") digits 5);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,090 ⟶ 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 3,101 ⟶ 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 3,117 ⟶ 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 3,135 ⟶ 3,354:
}
 
print(entropy(of: "1223334444"))</langsyntaxhighlight>
 
{{out}}
Line 3,141 ⟶ 3,360:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc entropy {str} {
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 3,150 ⟶ 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>
Line 3,160 ⟶ 3,379:
</pre>
 
=={{header|GoV (Vlang)}}==
 
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
import arrays
 
Line 3,191 ⟶ 3,409:
e := entropy(h, input.len)
println(e)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,199 ⟶ 3,417:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var s = "1223334444"
var m = {}
for (c in s) {
Line 3,211 ⟶ 3,429:
}
var l = s.count
System.print(l.log2 - hm/l)</langsyntaxhighlight>
 
{{out}}
Line 3,219 ⟶ 3,437:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code real RlOut=48, Ln=54; \intrinsic routines
string 0; \use zero-terminated strings
 
Line 3,245 ⟶ 3,463:
];
 
RlOut(0, Entropy("1223334444"))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,252 ⟶ 3,470:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
const math = std.math;
Line 3,275 ⟶ 3,493:
return h;
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,283 ⟶ 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,292 ⟶ 3,510:
}
 
entropy("1223334444").println(" bits");</langsyntaxhighlight>
{{out}}
<pre>
Line 3,300 ⟶ 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,311 ⟶ 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