Anonymous user
Entropy: Difference between revisions
m
→{{header|uBasic/4tH}}
imported>Thebeez |
|||
(22 intermediate revisions by 14 users not shown) | |||
Line 39:
=={{header|11l}}==
<
DefaultDict[Char, Int] hist
L(c) source
Line 49:
R r
print(entropy(‘1223334444’))</
{{out}}
<pre>
Line 57:
=={{header|Ada}}==
Uses Ada 2012.
<
procedure Count_Entropy is
Line 84:
Put(Result, Fore => 1, Aft => 5, Exp => 0);
end;
end Count_Entropy;</
=={{header|Aime}}==
<
real h, v;
index x;
Line 102:
}
o_form("/d6/\n", h);</
Examples:
<pre>$ aime -a tmp/entr 1223334444
Line 112:
=={{header|ALGOL 68}}==
<
# calculate the shannon entropy of a string #
PROC shannon entropy = ( STRING s )REAL:
Line 142:
print( ( shannon entropy( "1223334444" ), newline ) )
END</
{{out}}
<pre>
Line 150:
=={{header|ALGOL W}}==
{{trans|ALGOL 68}}
<
% 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.</
{{out}}
<pre>
Line 210:
=={{header|APL}}==
<
ENTROPY←{-+/R×2⍟R←(+⌿⍵∘.=∪⍵)÷⍴⍵}
Line 235:
-+/RATIO×2⍟RATIO
1.846439345
</syntaxhighlight>
{{out}}
<pre>
Line 243:
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">entropy: function [s][
t: #[]
loop s 'c [
Line 257 ⟶ 256:
]
print entropy "1223334444"</
{{out}}
Line 264 ⟶ 263:
=={{header|AutoHotkey}}==
<
Entropy(n)
Line 281 ⟶ 280:
}
return, e
}</
{{out}}
<pre>1.846440</pre>
=={{header|AWK}}==
<syntaxhighlight lang
{
N = length
for (i = 1; i <= N; ++i)
++H[substr($0, i, 1)]
}
END {
}</syntaxhighlight>
{{out|Usage}}
<
1.84644
=={{header|BASIC}}==
Works with older (unstructured) Microsoft-style BASIC.
<
20 S$="1223334444"
30 U$=""
Line 329 ⟶ 325:
210 E=E-R(I)
220 NEXT I
230 PRINT E</
{{out}}
<pre>1.84643935</pre>
==={{header|QBasic}}===
<
L = LOG(X) / LOG(2)
END FUNCTION
Line 360 ⟶ 356:
NEXT I
PRINT E
END</
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
<
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</
{{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}}
<
PRINT FNentropy("1223334444")
END
Line 414 ⟶ 477:
:
DEF FNlogtwo(n)
= LN n / LN 2</
{{out}}
<pre>1.84643934</pre>
=={{header|BQN}}==
<
H "1223334444"</
{{out}}
<pre>1.8464393446710154</pre>
=={{header|Burlesque}}==
<
1.8464393446710157</
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Line 476 ⟶ 538:
printf("%lf\n",H);
return 0;
}</
Examples:
<syntaxhighlight lang="text">$ ./entropy
1223334444
1.846439
$ ./entropy
Rosetta Code is the best site in the world!
3.646513</
=={{header|C sharp|C#}}==
Translation of C++.
<
using System;
using System.Collections.Generic;
Line 527 ⟶ 589:
}
}
</syntaxhighlight>
{{out}}
<pre>The Entropy of 1223334444 is 1.84643934467102</pre>
Without using Hashtables or Dictionaries:
<
namespace Entropy
{
Line 573 ⟶ 635:
}
}
}</
=={{header|C++}}==
<
#include <map>
#include <iostream>
Line 601 ⟶ 663:
<< " is " << infocontent << std::endl ;
return 0 ;
}</
{{out}}
<pre>(entropy "1223334444")
Line 607 ⟶ 669:
=={{header|Clojure}}==
<
(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 +))))</
{{out}}
<
1.8464393446710154</
=={{header|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 </
{{out}}
<pre>1.846439</pre>
=={{header|CoffeeScript}}==
<
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'}"</
{{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)
(let ((table (make-hash-table :test 'equal))
(entropy 0))
Line 681 ⟶ 741:
(log (/ v (length input-string)) 2))))
table)
entropy))</
More like Common Lisp version:
<
(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))))))</
=={{header|Crystal}}==
<
def sum(array : Array(Float64))
res = 0
Line 734 ⟶ 794:
puts ".[Results]."
puts "Length: " + l.to_s
puts "Entropy: " + (entropy h, l).to_s</
=={{header|D}}==
<
double entropy(T)(T[] s)
Line 752 ⟶ 812:
void main() {
"1223334444"d.dup.entropy.writeln;
}</
{{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">
program Entropytest;
Line 820 ⟶ 881:
Writeln('Entropy of "', strng, '" is ', entropy(strng): 2: 5, ' bits.');
readln;
end.</
=={{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}}==
<
(lib 'hash)
;; counter: hash-table[key]++
Line 841 ⟶ 921:
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
</syntaxhighlight>
{{out}}
<
;; by increasing entropy
Line 857 ⟶ 937:
(H (for/list ((i 1000_000)) (random 1000_000))) → 19.104028424596976
</syntaxhighlight>
=={{header|Elena}}==
{{trans|C#}}
ELENA
<
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)
}</
{{out}}
<pre>
Line 913 ⟶ 992:
{{works with|Erlang/OTP|18}}
<code>:math.log2</code> was added in OTP 18.
<
def entropy(str) do
leng = String.length(str)
Line 926 ⟶ 1,005:
end
IO.inspect RC.entropy("1223334444")</
{{out}}
Line 934 ⟶ 1,013:
=={{header|Emacs Lisp}}==
<
(let ((freq-table (make-hash-table))
(entropy 0)
Line 948 ⟶ 1,027:
(log (/ v length) 2)))))
freq-table)
(- entropy)))</
{{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).
<
1.8464393446710154</
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( entropy ).
Line 979 ⟶ 1,058:
Frequency = How_many / String_length,
{String_length, Acc - (Frequency * math:log(Frequency))}.
</syntaxhighlight>
{{out}}
Line 988 ⟶ 1,067:
=={{header|Euler Math Toolbox}}==
<
$ v=strtochar(s);
$ m=getmultiplicities(unique(v),v);
Line 995 ⟶ 1,074:
$endfunction
>entropy("1223334444")
1.84643934467</
=={{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#}}==
<
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")</
{{out}}
<pre>1.846439</pre>
=={{header|Factor}}==
<
prettyprint sequences ;
IN: rosetta-code.entropy
Line 1,023 ⟶ 1,133:
"1223334444" shannon-entropy .
"Factor is my favorite programming language." shannon-entropy .</
{{out}}
<pre>
Line 1,031 ⟶ 1,141:
=={{header|Forth}}==
<
create freq 256 cells allot
Line 1,052 ⟶ 1,162:
s" 1223334444" entropy f. \ 1.84643934467102 ok
</syntaxhighlight>
=={{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">
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Tue May 21 21:43:12
Line 1,116 ⟶ 1,225:
end program shannonEntropy
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,152 ⟶ 1,261:
Print : Print "hit any key to end program"
Sleep
End</
{{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).
<
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)</
{{out}}
<pre>1.84643934467101549345</pre>
Line 1,191 ⟶ 1,300:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Entropy}}
'''Solution'''
[[File:Fōrmulæ - Entropy 01.png]]
'''Test case'''
[[File:Fōrmulæ - Entropy 02.png]]
[[File:Fōrmulæ - Entropy 03.png]]
[[File:Fōrmulæ - Entropy 04.png]]
[[File:Fōrmulæ - Entropy 05.png]]
=={{header|Go}}==
===Go: Slice version===
<
import (
Line 1,211 ⟶ 1,330:
}
// for ASCII strings
func H(data string) (entropy float64) {
if data == "" {
Line 1,222 ⟶ 1,342:
}
return entropy
}</
{{out}}
<pre>
Line 1,229 ⟶ 1,349:
===Go: Map version===
<
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)
}
fmt.Println(math.Log2(l) - hm/l)
}</
{{out}}
<pre>
Line 1,256 ⟶ 1,377:
=={{header|Groovy}}==
<
-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)
}
}</
Testing
<
'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
}</
{{out}}
<pre>Checking 1223334444 has a shannon entrophy of 1.846439344671
Line 1,284 ⟶ 1,405:
=={{header|Haskell}}==
<
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</
Or, inlining with an applicative expression (turns out to be fractionally faster):
<
entropy
Line 1,307 ⟶ 1,428:
main :: IO ()
main = print $ entropy "1223334444"</
{{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.
<
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</
{{out}}
Line 1,341 ⟶ 1,461:
=={{header|J}}==
'''Solution''':<
{{out|Example}}
<
1.84644
entropy i.256
Line 1,352 ⟶ 1,472:
1
entropy 256$0 1 2 3
2</
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+}}
<
import java.util.Map;
import java.util.HashMap;
Line 1,410 ⟶ 1,530:
return;
}
}</
{{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.
<
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</
{{out}}
<pre>1.8464393446710154
Line 1,450 ⟶ 1,570:
2
3
4</pre>
;Another variant
<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);
};</
{{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.
<
# 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) ;</
{{out|Example}}
<
=={{header|Jsish}}==
From Javascript entry.
<
function values(obj:object):array {
Line 1,520 ⟶ 1,641:
; entropy('Rosetta Code');
; entropy('password');
}</
{{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))
@show entropy("1223334444")
@show entropy([1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 4, 5])</
{{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}}==
<
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))}")
}</
{{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}}==
<
{def entropy
Line 1,639 ⟶ 1,789:
-> 4.70043971814109
</syntaxhighlight>
=={{header|Lang5}}==
<
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</
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
dim countOfChar( 255) ' all possible one-byte ASCII chars
Line 1,694 ⟶ 1,844:
logBase =log( x) /log( 2)
end function
</syntaxhighlight>
{{Out}}
<pre> Characters used and the number of occurrences of each
Line 1,705 ⟶ 1,855:
=={{header|Lua}}==
<
function entropy (X)
Line 1,723 ⟶ 1,873:
end
print(entropy("1223334444"))</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Length[#]) &[Characters[s]])</
{{out|Example}}<
1.84644
shE["Rosetta Code"]
3.08496</
=={{header|MATLAB}} / {{header|Octave}}==
This version allows for any input vectors,
including strings, floats, negative integers, etc.
<
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; </
{{out|Usage}}
<
ans = 1.8464</
=={{header|MiniScript}}==
<
count = {}
for c in s
Line 1,761 ⟶ 1,911:
end function
print entropy("1223334444")</
{{out}}
Line 1,767 ⟶ 1,917:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
Line 1,802 ⟶ 1,952:
WriteReal(entropy("1223334444"), 14);
WriteLn;
END Entropy.</
{{out}}
<pre> 1.8464394E+00</pre>
=={{header|NetRexx}}==
{{trans|REXX}}
<
options replace format comments java crossref savelog symbols
Line 1,880 ⟶ 2,031:
end report
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,896 ⟶ 2,047:
=={{header|Nim}}==
<
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")</
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Collection;
class Entropy {
Line 1,954 ⟶ 2,104:
};
}
}</
Output:
Line 1,968 ⟶ 2,118:
=={{header|OCaml}}==
;By using a map, purely functional
<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>
{{out}}
<pre>1.84643934467</pre>
=={{header|Oforth}}==
<syntaxhighlight 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") .</
{{out}}
Line 2,020 ⟶ 2,182:
=={{header|ooRexx}}==
{{trans|REXX}}
<
Numeric Digits 16
Parse Arg s
Line 2,051 ⟶ 2,213:
Exit
::requires 'rxmath' LIBRARY </
{{out}}
<pre>1223334444 Entropy 1.846439344671</pre>
=={{header|PARI/GP}}==
<
<pre>>entropy("1223334444")
%1 = 1.8464393446710154934341977463050452232</pre>
=={{header|Pascal}}==
Free Pascal (http://freepascal.org).
<syntaxhighlight lang="pascal">
PROGRAM entropytest;
Line 2,115 ⟶ 2,275:
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.
</syntaxhighlight>
{{out}}
Line 2,123 ⟶ 2,283:
=={{header|Perl}}==
<
my %count; $count{$_}++ for @_;
my $entropy = 0;
Line 2,133 ⟶ 2,293:
}
print entropy split //, "1223334444";</
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,169 ⟶ 2,329:
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
function shannonEntropy($string) {
Line 2,196 ⟶ 2,355:
number_format(shannonEntropy($string), 6)
);
}</
{{out}}
Line 2,207 ⟶ 2,366:
=={{header|Picat}}==
<
["1223334444",
"Rosetta Code is the best site in the world!",
Line 2,221 ⟶ 2,380:
Occ.put(E, Occ.get(E,0) + 1)
end,
Entropy = -sum([P2*log2(P2) : _C=P in Occ, P2 = P/Len]).</
{{out}}
Line 2,228 ⟶ 2,387:
=={{header|PicoLisp}}==
PicoLisp only supports fixed point arithmetic, but it does have the ability to call libc transcendental functions (for log)
<syntaxhighlight lang="picolisp">
(scl 8)
(load "@lib/math.l")
Line 2,255 ⟶ 2,414:
1. LN2)))
</syntaxhighlight>
{{Out}}
<pre>
Line 2,263 ⟶ 2,422:
=={{header|PL/I}}==
<
/*--------------------------------------------------------------------
* 08.08.2014 Walter Pachl translated from REXX version 1
Line 2,296 ⟶ 2,455:
End;
Put Edit('s='''!!s!!''' Entropy=',-e)(Skip,a,f(15,12));
End;</
{{out}}
<pre>s='1223334444' Entropy= 1.846439344671</pre>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function entropy ($string) {
$n = $string.Length
Line 2,311 ⟶ 2,470:
}
entropy "1223334444"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,318 ⟶ 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]).
%! shannon_entropy(+String, -Entropy) is det.
Line 2,454 ⟶ 2,610:
,must_be(number, B)
,Sum is A + B.
</syntaxhighlight>
Example query:
Line 2,464 ⟶ 2,620:
=={{header|PureBasic}}==
<
NewMap uchar.i() : Define.d e
Line 2,485 ⟶ 2,641:
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()</
{{out}}
<pre>Entropy of [1223334444] = 1.846439344671015</pre>
Line 2,491 ⟶ 2,647:
=={{header|Python}}==
===Python: Longer version===
<
import math
Line 2,524 ⟶ 2,680:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</
{{out}}
Line 2,539 ⟶ 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):
p, lns = Counter(s), float(len(s))
return log2(lns) - sum(count * log2(count) for count in p.values()) / lns
print(entropy("1223334444"))</syntaxhighlight>
{{out}}
<pre>1.8464393446710154</pre>
===Uses Python 2===
<
import math
log2=lambda x:math.log(x)/math.log(2)
Line 2,572 ⟶ 2,726:
while True:
print Entropy(raw_input('>>>'))</
=={{header|R}}==
<syntaxhighlight lang="rsplus">
entropy <- function(str) {
vec <- strsplit(str, "")[[1]]
Line 2,584 ⟶ 2,737:
-sum(p_xi * log(p_xi, 2))
}
</syntaxhighlight>
{{out}}
Line 2,593 ⟶ 2,746:
=={{header|Racket}}==
<
(require math)
(provide entropy hash-entropy list-entropy digital-entropy)
Line 2,615 ⟶ 2,768:
(check-= (entropy "xggooopppp") 1.8464393446710154 1E-8))
(module+ main (entropy "1223334444"))</
{{out}}
<pre> 1.8464393446710154</pre>
Line 2,622 ⟶ 2,775:
(formerly Perl 6)
{{works with|rakudo|2015-09-09}}
<syntaxhighlight lang="raku"
[+] map -> \p { p * -log p }, bag(@a).values »/» +@a;
}
say log(2) R/ entropy '1223334444'.comb;</
{{out}}
<pre>1.84643934467102</pre>
Line 2,632 ⟶ 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"
augment class Bag {
method entropy {
Line 2,640 ⟶ 2,793:
}
say '1223334444'.comb.Bag.entropy / log 2;</
=={{header|REXX}}==
===version 1===
<
* 28.02.2013 Walter Pachl
* 12.03.2013 Walter Pachl typo in log corrected. thanx for testing
Line 2,744 ⟶ 2,897:
Numeric Digits (prec)
r=r+0
Return r </
<!-- these types of comparisons are not part of this Rosetta Code task, and
Line 2,750 ⟶ 2,903:
<
* Test program to compare Versions 1 and 2
* (the latter tweaked to be acceptable by my (oo)Rexx
Line 2,773 ⟶ 2,926:
Say ' '
Return
</syntaxhighlight>
{{out}}
<pre>Version 1: 1223334444 Entropy 1.846439344671
Line 2,794 ⟶ 2,947:
The '''LOG2''' subroutine is only included here for functionality, not to document how to calculate LOG<sub>2</sub> using REXX.
<
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,823 ⟶ 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, .)</
{{out|output|text= when using the default input of: <tt> 1223334444 </tt>}}
<pre>
Line 2,842 ⟶ 2,995:
=={{header|Ring}}==
<
decimals(8)
entropy = 0
Line 2,875 ⟶ 3,028:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
Output:
<pre>
Line 2,887 ⟶ 3,040:
</pre>
=={{header|
{{works with|
{| class="wikitable"
! Code
! Comments
|-
|
≪
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,900 ⟶ 3,093:
end
p entropy("1223334444")</
{{out}}
<pre>
1.8464393446710154
</pre>
=={{header|Run BASIC}}==
<
source$ = "1223334444"
Line 2,936 ⟶ 3,125:
print " Entropy of '"; source$; "' is "; entropy; " bits."
end</
Characters used and times used of each
'1' 1
Line 2,946 ⟶ 3,135:
=={{header|Rust}}==
<
let mut histogram = [0u64; 256];
Line 2,965 ⟶ 3,154:
let arg = std::env::args().nth(1).expect("Need a string.");
println!("Entropy of {} is {}.", arg, entropy(arg.as_bytes()));
}</
{{out}}
<pre>$ ./entropy 1223334444
Line 2,972 ⟶ 3,161:
=={{header|Scala}}==
<
def entropy( v:String ) = { v
Line 2,983 ⟶ 3,172:
// Confirm that "1223334444" has an entropy of about 1.84644
assert( math.round( entropy("1223334444") * 100000 ) * 0.00001 == 1.84644 )</
=={{header|scheme}}==
A version capable of calculating multidimensional entropy.
<
(define (entropy input)
(define (close? a b)
Line 3,030 ⟶ 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)))
</
{{out}}
Line 3,040 ⟶ 3,229:
=={{header|Scilab}}==
<syntaxhighlight lang="text">function E = entropy(d)
d=strsplit(d);
n=unique(string(d));
Line 3,059 ⟶ 3,248:
word ='1223334444';
E = entropy(word);
disp('The entropy of '+word+' is '+string(E)+'.');</
{{out}}
Line 3,065 ⟶ 3,254:
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 3,093 ⟶ 3,282:
begin
writeln(entropy("1223334444") digits 5);
end func;</
{{out}}
Line 3,099 ⟶ 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}}==
<
var counts = Hash.new;
s.each { |c| counts{c} := 0 ++ };
Line 3,110 ⟶ 3,319:
}
say entropy("1223334444");</
{{out}}
<pre>1.846439344671015493434197746305045223237</pre>
=={{header|Standard ML}}==
<
let
val N = Real.fromInt (String.size input) ;
Line 3,126 ⟶ 3,335:
~ (Vector.foldr (fn (a,s) => if a > 0.0 then term a + s else s) 0.0 freq )
end ;</
Entropy "1223334444" ;
val it = 1.846439345: real
=={{header|Swift}}==
<
func entropy(of x: String) -> Double {
Line 3,144 ⟶ 3,354:
}
print(entropy(of: "1223334444"))</
{{out}}
Line 3,150 ⟶ 3,360:
=={{header|Tcl}}==
<
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 3,159 ⟶ 3,369:
}
return $entropy
}</
Demonstration:
<
puts [format "entropy = %.5f" [entropy "Rosetta Code"]]</
{{out}}
<pre>
Line 3,169 ⟶ 3,379:
</pre>
=={{header|V (Vlang)}}==
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
import arrays
Line 3,200 ⟶ 3,409:
e := entropy(h, input.len)
println(e)
}</
{{out}}
<pre>
Line 3,208 ⟶ 3,417:
=={{header|Wren}}==
{{trans|Go}}
<
var m = {}
for (c in s) {
Line 3,220 ⟶ 3,429:
}
var l = s.count
System.print(l.log2 - hm/l)</
{{out}}
Line 3,228 ⟶ 3,437:
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 3,254 ⟶ 3,463:
];
RlOut(0, Entropy("1223334444"))</
{{out}}
<pre>
Line 3,261 ⟶ 3,470:
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math;
Line 3,284 ⟶ 3,493:
return h;
}
</syntaxhighlight>
{{Out}}
<pre>
Line 3,292 ⟶ 3,501:
=={{header|zkl}}==
{{trans|D}}
<
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,301 ⟶ 3,510:
}
entropy("1223334444").println(" bits");</
{{out}}
<pre>
Line 3,309 ⟶ 3,518:
=={{header|ZX Spectrum Basic}}==
{{trans|FreeBASIC}}
<
20 LET sourcelen=LEN s$
30 DIM t(255)
Line 3,320 ⟶ 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</
|