Entropy/Narcissist: Difference between revisions
→{{header|jq}}: def sum():
(→{{header|jq}}: def sum():) |
|||
(10 intermediate revisions by 9 users not shown) | |||
Line 1:
{{task}}
[[File:ENTROPY.JPG|
;Task:
Line 12:
=={{header|Ada}}==
<
with Ada.Command_Line;
with Ada.Numerics.Elementary_Functions;
Line 77:
Float_Io.Put (Entr);
New_Line;
end Entropy;</
{{out}}
<pre>Entropy of 'entropy.adb' is 4.559854</pre>
Line 86:
Note that the source here uses spaces, not tabs, hence the low entropy, replacing all runs of four spaces with a single space
results in an entropy of +4.64524532762062e +0.
<
# calculate the shannon entropy of a string #
PROC shannon entropy = ( STRING s )REAL:
Line 144:
print( ( shannon entropy( file contents ), newline ) )
FI
END</
{{out}}
<pre>
Line 152:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
MsgBox, % Entropy(var)
Line 167:
}
return, e
}</
{{Output}}
<pre>5.942956</pre>
Line 173:
=={{header|AWK}}==
The record separator RS is set to end of file. So getline reads the whole file in one line.
<syntaxhighlight lang="awk">
BEGIN{FS=""
RS="\x04"#EOF
Line 179:
for(i=1;i<=NF;i++)H[$i]++
for(i in H)E-=(h=H[i]/NF)*log(h)
print "bytes ",NF,"
exit}</
{{Output}}
<pre>bytes 158
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM Freq%(255)
FOR I%=PAGE TO LOMEM Freq%(?I%)+=1 NEXT
Size=LOMEM - PAGE
FOR I%=0 TO 255
IF Freq%(I%) Entropy+=Freq%(I%) / Size * LN(Freq%(I%) / Size) / LN(2)
NEXT
PRINT "My size is ";Size " bytes and my entropy is ";-Entropy "!"
END</syntaxhighlight>
{{out}}
<pre>My size is 224 bytes and my entropy is 5.11257089!</pre>
=={{header|C}}==
Line 189 ⟶ 202:
Assumes that the source file is stored in the working directory as "entropy.c".
<
#include <stdlib.h>
#include <stdbool.h>
Line 236 ⟶ 249:
printf("%lf\n",H);
return 0;
}</
{{out}}
<syntaxhighlight lang="text">5.195143</
=={{header|C++}}==
<
#include <fstream>
#include <cmath>
Line 281 ⟶ 294:
cout<<entropy(readFile("entropy.cpp"));
return 0;
}</
{{out}}
<pre>4.58688</pre>
Line 287 ⟶ 300:
=={{header|Crystal}}==
{{trans|Ruby}}
<
counts = s.chars.each_with_object(Hash(Char, Float64).new(0.0)) { |c, h| h[c] += 1 }
counts.values.sum do |count|
Line 295 ⟶ 308:
end
puts entropy File.read(__FILE__)</
{{out}}
<pre>
Line 302 ⟶ 315:
=={{header|D}}==
<
import std.stdio, std.algorithm, std.math, std.file;
Line 312 ⟶ 325:
.sum
.writeln;
}</
{{out}}
<pre>6.29803</pre>
=={{header|Elixir}}==
<
text = IO.read(file, :all)
leng = String.length(text)
Line 328 ⟶ 341:
end)
|> IO.puts
end)</
{{out}}
Line 336 ⟶ 349:
=={{header|Emacs Lisp}}==
<
(let ((freq-table (make-hash-table))
(entropy 0)
Line 355 ⟶ 368:
(shannon-entropy (with-temp-buffer
(insert-file-contents "U:/rosetta/narcissist.el")
(buffer-string))))</
{{out}}
<
4.5129548515535785</
=={{header|Erlang}}==
<
-define(LOG2E, 1.44269504088896340735992).
Line 385 ⟶ 398:
_ -> count(Data, I+1, Frq #{Chr => 1})
end.
</syntaxhighlight>
{{out}}
<pre>
Line 392 ⟶ 405:
=={{header|Factor}}==
<
math.functions math.statistics prettyprint sequences ;
IN: rosetta-code.entropy-narcissist
Line 403 ⟶ 416:
"entropy-narcissist.factor" utf8 [
contents entropy .
] with-file-reader</
{{out}}
<pre>
Line 410 ⟶ 423:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
' modified code from ENTROPY entry
Line 462 ⟶ 475:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Windows version
Line 475 ⟶ 488:
=={{header|Go}}==
<
import (
Line 509 ⟶ 522:
l := float64(len(d))
return math.Log2(l) - hm/l
}</
{{out}}
<pre>
Line 517 ⟶ 530:
=={{header|Haskell}}==
<
import Data.List
import System.Environment
Line 527 ⟶ 540:
entropy = sort >>> group >>> map genericLength >>> normalize >>> map lg >>> sum
where lg c = -c * logBase 2 c
normalize c = let sc = sum c in map (/ sc) c</
{{out}} In a shell
Line 544 ⟶ 557:
=={{header|J}}==
'''Solution''':<
1!:2&2 entropy 1!:1 (4!:4 <'entropy') { 4!:3''</
'''Example''':<
4.73307</
=={{header|Java}}==
<
import java.io.BufferedReader;
import java.io.File;
Line 592 ⟶ 605:
}
</syntaxhighlight>
{{out}}
<pre>Entropy of file "src/EntropyNarcissist.java" = 4.691381977073.</pre>
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
'''Works with jaq, the Rust implementation of jq'''
The program assumes it will be presented to itself using
an invocation of jq with the -sR options, along the lines of:
<pre>
jq -sR -f entropy-narcissist.jq < entropy-narcissist.jq
</pre>
If your jq supports `keys_unsorted`, feel free to use it instead of `keys`.
<syntaxhighlight lang="jq">
def chars: explode[] | [.] | implode;
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
def sum(s): reduce s as $x (0; .+$x);
length as $l
| bow(chars)
| sum(keys[] as $k | .[$k] as $c | $c * ($c|log2) )
| ($l|log2) - ./$l
</syntaxhighlight>
{{output}}
<pre>
{{output}}
<pre>
4.796499915496963
</pre>
=={{header|Julia}}==
<
entropy(s) = -sum(x -> x / length(s) * log2(x / length(s)), values(counter(s)))
println("self-entropy: ", entropy(read(Base.source_path(), String)))</
{{out}}
Line 607 ⟶ 654:
=={{header|Kotlin}}==
<
fun log2(d: Double) = Math.log(d) / Math.log(2.0)
Line 629 ⟶ 676:
val prog = java.io.File("entropy_narc.kt").readText()
println("This program's entropy is ${"%18.16f".format(shannon(prog))}")
}</
{{out}}
Line 638 ⟶ 685:
=={{header|Lua}}==
arg[0] gives the path of the script currently being executed
<
local inFile = io.open(filename, "r")
local fileContent = inFile:read("*all")
Line 663 ⟶ 710:
end
print(entropy(getFile(arg[0])))</
{{out}}
<pre>4.3591214356783</pre>
Line 670 ⟶ 717:
As we have compiled without specific options to change the way the executable is named, we can retrieve the source file name by adding the suffix “.nim” to the executable file name. We suppose also that the source file is in the same directory as the executable (which is true in our environment).
<
let execName = getAppFilename().splitPath().tail
Line 683 ⟶ 730:
echo "Source file entropy: ", srcName.readFile().entropy().formatFloat(ffDecimal, 5)
echo "Binary file entropy: ", execName.readFile().entropy().formatFloat(ffDecimal, 5)</
{{out}}
Line 690 ⟶ 737:
=={{header|PARI/GP}}==
<
entropy(Str(entropy))</
{{out}}
<pre>%1 = 4.54978213</pre>
=={{header|Perl}}==
<
use strict ;
use warnings ;
Line 722 ⟶ 769:
}
$infocontent *= -1 ;
say "The information content of the source file is $infocontent !" ;</
{{out}}
<pre>The information content of the source file is 4.6487923749222 !</pre>
Line 728 ⟶ 775:
=={{header|Phix}}==
Minor edit to the [[Entropy#Phix|Entropy]] answer, if compiled assumes source code is in the same directory.
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- command_line, file i/o</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 753 ⟶ 800:
<span style="color: #0000FF;">?</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #008000;">".exe"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">".exw"</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">"rb"</span><span style="color: #0000FF;">))</span>
<!--</
Output is eg 4.993666233, but that may vary with Windows/Linux line endings, tabs vs spaces, trailing returns, BOM headers, etc.
=={{header|PHP}}==
<
$h = 0;
$s = file_get_contents(__FILE__);
Line 766 ⟶ 813:
( $c / $l ) *
log( $c / $l, 2 );
echo $h;</
{{out}}
<pre>2.9339128173013</pre>
=={{header|Picat}}==
{{trans|Go}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
entropy(File) = E =>
Bytes = read_file_bytes(File),
F = [0: I in 1..256],
foreach (B in Bytes)
B1 := B + 1,
F[B1] := F[B1] + 1
end,
HM = 0,
foreach (C in F)
if (C > 0) then
HM := HM + C * log(2, C)
end
end,
L = Bytes.length,
E = log(2, L) - HM / L.
main(Args) =>
printf("Entropy: %f\n", entropy(Args[1])).
</syntaxhighlight>
{{out}}
<pre>
$ picat entropy.pi entropy.pi
Entropy: 4.384622
</pre>
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
(scl 8)
(load "@lib/math.l")
Line 803 ⟶ 879:
(bye)
</syntaxhighlight>
{{Out}}
<pre>
My entropy is 4.12169822
</pre>
=={{header|Python}}==
{{works with|Python 3.4}}
Line 813 ⟶ 890:
Minor edit to the [[Entropy#Python|Entropy]] answer.
<
from collections import Counter
Line 823 ⟶ 900:
b=f.read()
print(entropy(b))</
{{Output}}
<pre>4.575438063744619</pre>
Line 829 ⟶ 906:
=={{header|Racket}}==
The entropy of the program below is 4.512678555350348.
<
#lang racket
(require math)
Line 837 ⟶ 914:
(- (for/sum ([(d c) (in-hash (samples->hash ds))])
(* (/ c n) (log2 (/ c n)))))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.05}}
<syntaxhighlight lang="raku"
given slurp($*PROGRAM-NAME).comb</
Result should be in the neighborhood of 4.9
{{out}}
Line 850 ⟶ 927:
=={{header|REXX}}==
REXX doesn't have a BIF (built-in function) for '''log''' or '''ln''', so the subroutine (function) '''log2''' is included herein.
<
numeric digits length( e() ) % 2 - length(.) /*use 1/2 of the decimal digits of E. */
#= 0; @.= 0; $=; $$=; recs= sourceline() /*define some handy─dandy REXX vars. */
Line 880 ⟶ 957:
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 this REXX program as input:}}
<pre>
Line 891 ⟶ 968:
=={{header|Ruby}}==
<
counts = s.each_char.tally
size = s.size.to_f
Line 902 ⟶ 979:
s = File.read(__FILE__)
p entropy(s)
</syntaxhighlight>
{{out}}
<pre>4.653607496799478
Line 908 ⟶ 985:
=={{header|Rust}}==
<
use std::io::{Read, BufReader};
Line 933 ⟶ 1,010:
let file = BufReader::new(File::open(name).expect("Could not read file."));
println!("Entropy is {}.", entropy(file.bytes().flatten()));
}</
{{out}}
<pre>Entropy is 5.7108583.</pre>
=={{header|Sidef}}==
<
[0,
s.chars.freq.values.map {|c|
Line 947 ⟶ 1,024:
}
say entropy(File(__FILE__).open_r.slurp)</
{{out}}
<pre>
Line 955 ⟶ 1,032:
=={{header|Tcl}}==
Note that this code doesn't bother to close the open handle on the script; it is only suitable as a demonstration program.
<
set log2 [expr log(2)]
foreach char [split $str ""] {dict incr counts $char}
Line 966 ⟶ 1,043:
}
puts [format "entropy = %.5f" [entropy [read [open [info script]]]]]</
{{out}}
<pre>
entropy = 4.59099
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="go">import os
import math
fn main() {
println("Binary file entropy: ${entropy(os.args[0])?}")
}
fn entropy(file string) ?f64 {
d := os.read_bytes(file)?
mut f := [256]f64{}
for b in d {
f[b]++
}
mut hm := 0.0
for c in f {
if c > 0 {
hm += c * math.log2(c)
}
}
l := f64(d.len)
return math.log2(l) - hm/l
}</syntaxhighlight>
{{out}}
<pre>
Binary file entropy: 5.676177202237735
</pre>
=={{header|Wren}}==
Basically an amalgam of the code in the [[Print_itself#Wren]] and [[Entropy#Wren]] tasks.
<
import "io" for File
Line 990 ⟶ 1,097:
}
var l = s.count
System.print(l.log2 - hm/l)</
{{out}}
Line 996 ⟶ 1,103:
4.6302314663
</pre>
=={{header|XPL0}}==
To run: entropy < entropy.xpl
<syntaxhighlight lang "XPL0">int Count(128), I, Len, Ch;
real Sum, Prob;
[for I:= 0 to 127 do Count(I):= 0;
Len:= 0;
loop [Ch:= ChIn(1);
if Ch = $1A\EOF\ then quit;
Count(Ch):= Count(Ch)+1;
Len:= Len+1;
];
Sum:= 0.;
for I:= 0 to 127 do
if Count(I) then
[Prob:= float(Count(I)) / float(Len);
Sum:= Sum + Prob*Ln(Prob);
];
RlOut(0, -Sum/Ln(2.));
]</syntaxhighlight>
{{out}}
<pre>
4.63457</pre>
=={{header|zkl}}==
Minor edit to the [[Entropy#zkl|Entropy]] answer.
<
text.pump(Void,fcn(c,freq){ c=c.toAsc(); freq[c]=freq[c]+1; freq }
.fp1((0).pump(256,List,(0.0).create.fp(0)).copy()))
Line 1,008 ⟶ 1,138:
}
entropy(File("entropy.zkl").read().text).println();</
{{out}}
<pre>
|