Anonymous user
Entropy: Difference between revisions
m
→{{header|uBasic/4tH}}
(Update to make the program compile with Nim 1.4: added missing type to parameter.) |
imported>Thebeez |
||
(40 intermediate revisions by 23 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 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}}==
<
Entropy(n)
Line 260 ⟶ 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 308 ⟶ 325:
210 E=E-R(I)
220 NEXT I
230 PRINT E</
{{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.
<
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</
{{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 364 ⟶ 477:
:
DEF FNlogtwo(n)
= LN n / LN 2</
{{out}}
<pre>1.84643934</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">H ← -∘(+´⊢×2⋆⁼⊢)∘((+˝⊢=⌜⍷)÷≠)
H "1223334444"</syntaxhighlight>
{{out}}
<pre>1.8464393446710154</pre>
=={{header|Burlesque}}==
<
1.8464393446710157</
=={{header|C}}==
<syntaxhighlight 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;
}</
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 470 ⟶ 589:
}
}
</syntaxhighlight>
{{out}}
<pre>The Entropy of 1223334444 is 1.84643934467102</pre>
Without using Hashtables or Dictionaries:
<
namespace Entropy
{
Line 516 ⟶ 635:
}
}
}</
=={{header|C++}}==
<
#include <map>
#include <iostream>
Line 544 ⟶ 663:
<< " is " << infocontent << std::endl ;
return 0 ;
}</
{{out}}
<pre>(entropy "1223334444")
Line 550 ⟶ 669:
=={{header|Clojure}}==
<
(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 +))))</
{{out}}
<
1.8464393446710154</
=={{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}}==
<
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'}"</
{{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 591 ⟶ 741:
(log (/ v (length input-string)) 2))))
table)
entropy))</
More like Common Lisp version:
<
(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))))))</
=={{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}}==
<
double entropy(T)(T[] s)
Line 620 ⟶ 812:
void main() {
"1223334444"d.dup.entropy.writeln;
}</
{{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}}==
<
(lib 'hash)
;; counter: hash-table[key]++
Line 644 ⟶ 921:
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
</syntaxhighlight>
{{out}}
<
;; by increasing entropy
Line 660 ⟶ 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 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)
}</
{{out}}
<pre>
Line 715 ⟶ 992:
{{works with|Erlang/OTP|18}}
<code>:math.log2</code> was added in OTP 18.
<
def entropy(str) do
leng = String.length(str)
Line 728 ⟶ 1,005:
end
IO.inspect RC.entropy("1223334444")</
{{out}}
Line 736 ⟶ 1,013:
=={{header|Emacs Lisp}}==
<
(let ((freq-table (make-hash-table))
(entropy 0)
Line 750 ⟶ 1,027:
(log (/ v length) 2)))))
freq-table)
(- entropy)))</
{{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).
<
1.8464393446710154</
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( entropy ).
Line 781 ⟶ 1,058:
Frequency = How_many / String_length,
{String_length, Acc - (Frequency * math:log(Frequency))}.
</syntaxhighlight>
{{out}}
Line 790 ⟶ 1,067:
=={{header|Euler Math Toolbox}}==
<
$ v=strtochar(s);
$ m=getmultiplicities(unique(v),v);
Line 797 ⟶ 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 810 ⟶ 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 825 ⟶ 1,133:
"1223334444" shannon-entropy .
"Factor is my favorite programming language." shannon-entropy .</
{{out}}
<pre>
Line 833 ⟶ 1,141:
=={{header|Forth}}==
<
create freq 256 cells allot
Line 854 ⟶ 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 918 ⟶ 1,225:
end program shannonEntropy
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 954 ⟶ 1,261:
Print : Print "hit any key to end program"
Sleep
End</
{{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).
<
for arg in $argv
set name count_$arg
Line 987 ⟶ 1,294:
echo "$entropy / l(2)" | bc -l
end
entropy (echo 1223334444 | fold -w1)</
{{out}}
<pre>1.84643934467101549345</pre>
Line 993 ⟶ 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,013 ⟶ 1,330:
}
// for ASCII strings
func H(data string) (entropy float64) {
if data == "" {
Line 1,024 ⟶ 1,342:
}
return entropy
}</
{{out}}
<pre>
Line 1,031 ⟶ 1,349:
===Go: Map version===
<
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)
}
fmt.Println(math.Log2(l) - hm/l)
}</
{{out}}
<pre>
Line 1,058 ⟶ 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,075 ⟶ 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,086 ⟶ 1,405:
=={{header|Haskell}}==
<
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</
Or, inlining with an applicative expression (turns out to be fractionally faster):
<
entropy
Line 1,109 ⟶ 1,428:
main :: IO ()
main = print $ entropy "1223334444"</
{{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.
<
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</
{{out}}
Line 1,143 ⟶ 1,461:
=={{header|J}}==
'''Solution''':<
{{out|Example}}
<
1.84644
entropy i.256
Line 1,154 ⟶ 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,162 ⟶ 1,480:
{{trans|REXX}}
{{works with|Java|7+}}
<
import java.util.Map;
import java.util.HashMap;
Line 1,212 ⟶ 1,530:
return;
}
}</
{{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.
<
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</
{{out}}
<pre>1.8464393446710154
Line 1,252 ⟶ 1,570:
2
3
4</pre>
;Another variant
<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);
};</
{{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.
<
# 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) ;</
{{out|Example}}
<
=={{header|Jsish}}==
From Javascript entry.
<
function values(obj:object):array {
Line 1,322 ⟶ 1,641:
; entropy('Rosetta Code');
; entropy('password');
}</
{{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))
@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,374 ⟶ 1,700:
println("------------------------------------ ------------------")
for (sample in samples) println("${sample.padEnd(36)} -> ${"%18.16f".format(shannon(sample))}")
}</
{{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}}==
<
{def entropy
Line 1,441 ⟶ 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,460 ⟶ 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,496 ⟶ 1,844:
logBase =log( x) /log( 2)
end function
</syntaxhighlight>
{{Out}}
<pre> Characters used and the number of occurrences of each
Line 1,507 ⟶ 1,855:
=={{header|Lua}}==
<
function entropy (X)
Line 1,525 ⟶ 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,544 ⟶ 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,563 ⟶ 1,911:
end function
print entropy("1223334444")</
{{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}}
<
options replace format comments java crossref savelog symbols
Line 1,643 ⟶ 2,031:
end report
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,659 ⟶ 2,047:
=={{header|Nim}}==
<
proc entropy(s: string): float =
Line 1,666 ⟶ 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,717 ⟶ 2,104:
};
}
}</
Output:
Line 1,731 ⟶ 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,761 ⟶ 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 1,776 ⟶ 2,175:
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
entropy("1223334444") .</
{{out}}
Line 1,783 ⟶ 2,182:
=={{header|ooRexx}}==
{{trans|REXX}}
<
Numeric Digits 16
Parse Arg s
Line 1,814 ⟶ 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 1,878 ⟶ 2,275:
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.
</syntaxhighlight>
{{out}}
Line 1,886 ⟶ 2,283:
=={{header|Perl}}==
<
my %count; $count{$_}++ for @_;
my $entropy = 0;
Line 1,896 ⟶ 2,293:
}
print entropy split //, "1223334444";</
=={{header|Phix}}==
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">symbols</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">,</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">symbols</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">,</span><span style="color: #000000;">si</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">H</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ci</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">N</span>
<span style="color: #000000;">H</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">ci</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">H</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1223334444"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,933 ⟶ 2,329:
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
function shannonEntropy($string) {
Line 1,960 ⟶ 2,355:
number_format(shannonEntropy($string), 6)
);
}</
{{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">
(scl 8)
(load "@lib/math.l")
Line 1,999 ⟶ 2,414:
1. LN2)))
</syntaxhighlight>
{{Out}}
<pre>
Line 2,007 ⟶ 2,422:
=={{header|PL/I}}==
<
/*--------------------------------------------------------------------
* 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;</
{{out}}
<pre>s='1223334444' Entropy= 1.846439344671</pre>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function entropy ($string) {
$n = $string.Length
Line 2,055 ⟶ 2,470:
}
entropy "1223334444"
</syntaxhighlight>
<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]).
%! shannon_entropy(+String, -Entropy) is det.
Line 2,198 ⟶ 2,610:
,must_be(number, B)
,Sum is A + B.
</syntaxhighlight>
Example query:
Line 2,208 ⟶ 2,620:
=={{header|PureBasic}}==
<
NewMap uchar.i() : Define.d e
Line 2,229 ⟶ 2,641:
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()</
{{out}}
<pre>Entropy of [1223334444] = 1.846439344671015</pre>
Line 2,235 ⟶ 2,647:
=={{header|Python}}==
===Python: Longer version===
<
import math
Line 2,268 ⟶ 2,680:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</
{{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):
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,316 ⟶ 2,726:
while True:
print Entropy(raw_input('>>>'))</
=={{header|R}}==
<syntaxhighlight lang="rsplus">
entropy <- function(str) {
vec <-
N <- length(vec)
p_xi <- table(vec) / N
-sum(p_xi * log(p_xi, 2))
}
</syntaxhighlight>
{{out}}
<pre>
> entropy("1223334444")
[1] 1.846439
</pre>
=={{header|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"))</
{{out}}
<pre> 1.8464393446710154</pre>
Line 2,355 ⟶ 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,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"
augment class Bag {
method entropy {
Line 2,373 ⟶ 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,477 ⟶ 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,483 ⟶ 2,903:
<
* 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>
{{out}}
<pre>Version 1: 1223334444 Entropy 1.846439344671
Line 2,527 ⟶ 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,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, .)</
{{out|output|text= when using the default input of: <tt> 1223334444 </tt>}}
<pre>
Line 2,575 ⟶ 2,995:
=={{header|Ring}}==
<
decimals(8)
entropy = 0
Line 2,608 ⟶ 3,028:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
Output:
<pre>
Line 2,620 ⟶ 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,633 ⟶ 3,093:
end
p entropy("1223334444")</
{{out}}
<pre>
1.8464393446710154
</pre>
=={{header|Run BASIC}}==
<
source$ = "1223334444"
Line 2,669 ⟶ 3,125:
print " Entropy of '"; source$; "' is "; entropy; " bits."
end</
Characters used and times used of each
'1' 1
Line 2,679 ⟶ 3,135:
=={{header|Rust}}==
<
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()));
}</
{{out}}
<pre>$ ./entropy 1223334444
Line 2,705 ⟶ 3,161:
=={{header|Scala}}==
<
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 )</
=={{header|scheme}}==
A version capable of calculating multidimensional entropy.
<
(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)))
</
{{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)+'.');</
{{out}}
Line 2,798 ⟶ 3,254:
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 2,826 ⟶ 3,282:
begin
writeln(entropy("1223334444") digits 5);
end func;</
{{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}}==
<
var counts = Hash.new;
s.each { |c| counts{c} := 0 ++ };
Line 2,843 ⟶ 3,319:
}
say entropy("1223334444");</
{{out}}
<pre>1.846439344671015493434197746305045223237</pre>
=={{header|Standard ML}}==
<
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 ;</
Entropy "1223334444" ;
val it = 1.846439345: real
=={{header|Swift}}==
<
func entropy(of x: String) -> Double {
Line 2,877 ⟶ 3,354:
}
print(entropy(of: "1223334444"))</
{{out}}
Line 2,883 ⟶ 3,360:
=={{header|Tcl}}==
<
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 2,892 ⟶ 3,369:
}
return $entropy
}</
Demonstration:
<
puts [format "entropy = %.5f" [entropy "Rosetta Code"]]</
{{out}}
<pre>
entropy = 1.84644
entropy = 3.08496
</pre>
=={{header|V (Vlang)}}==
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
import arrays
fn hist(source string) map[string]int {
mut hist := map[string]int{}
for e in source.split('') {
if e !in hist {
hist[e] = 0
}
hist[e]+=1
}
return hist
}
fn entropy(hist map[string]int, l int) f64 {
mut elist := []f64{}
for _,v in hist {
c := f64(v) / f64(l)
elist << -c * math.log2(c)
}
return arrays.sum<f64>(elist) or {-1}
}
fn main(){
input := "1223334444"
h := hist(input)
e := entropy(h, input.len)
println(e)
}</syntaxhighlight>
{{out}}
<pre>
1.8464393446710152
</pre>
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">var s = "1223334444"
var m = {}
for (c in s) {
Line 2,916 ⟶ 3,426:
for (k in m.keys) {
var c = m[k]
hm = hm + c *
}
var l = s.count
System.print(
{{out}}
Line 2,927 ⟶ 3,437:
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 2,953 ⟶ 3,463:
];
RlOut(0, Entropy("1223334444"))</
{{out}}
<pre>
Line 2,960 ⟶ 3,470:
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math;
Line 2,983 ⟶ 3,493:
return h;
}
</syntaxhighlight>
{{Out}}
<pre>
Line 2,991 ⟶ 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,000 ⟶ 3,510:
}
entropy("1223334444").println(" bits");</
{{out}}
<pre>
Line 3,008 ⟶ 3,518:
=={{header|ZX Spectrum Basic}}==
{{trans|FreeBASIC}}
<
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</
|