Entropy: Difference between revisions

Content added Content deleted
(→‎Ksh: add)
(→‎OCaml: add a more idiomatic variant)
Line 243: Line 243:


=={{header|Arturo}}==
=={{header|Arturo}}==

<syntaxhighlight lang="rebol">entropy: function [s][
<syntaxhighlight lang="rebol">entropy: function [s][
t: #[]
t: #[]
Line 427: Line 426:


=={{header|C}}==
=={{header|C}}==

<syntaxhighlight lang="c">#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 666: Line 664:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==

Not very Common Lisp-y version:
Not very Common Lisp-y version:

<syntaxhighlight lang="lisp">(defun entropy (string)
<syntaxhighlight lang="lisp">(defun entropy (string)
(let ((table (make-hash-table :test 'equal))
(let ((table (make-hash-table :test 'equal))
Line 778: Line 774:
{{out}}
{{out}}
<pre>1.84644</pre>
<pre>1.84644</pre>

=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| StrUtils}}
{{libheader| StrUtils}}
Line 844: Line 841:
readln;
readln;
end.</syntaxhighlight>
end.</syntaxhighlight>

=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
<syntaxhighlight lang="scheme">
Line 881: Line 879:


</syntaxhighlight>
</syntaxhighlight>



=={{header|Elena}}==
=={{header|Elena}}==
Line 1,078: Line 1,075:


=={{header|Fortran}}==
=={{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.
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">
<syntaxhighlight lang="fortran">
Line 1,213: Line 1,209:


=={{header|Fōrmulæ}}==
=={{header|Fōrmulæ}}==

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.
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.


Line 1,338: Line 1,333:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==

Hmmm, the 2nd equation sums across the length of the string (for the
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
example, that would be the sum of 10 terms). However, the answer cited
Line 1,475: Line 1,469:
2
2
3
3
4</pre> =={{header|JavaScript}}==
4</pre>
;Another variant
<syntaxhighlight lang="javascript">const entropy = (s) => {
<syntaxhighlight lang="javascript">const entropy = (s) => {
const split = s.split('');
const split = s.split('');
Line 1,555: Line 1,550:
=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{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))
<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("1223334444")
Line 1,613: Line 1,607:


=={{header|Ksh}}==
=={{header|Ksh}}==
{{works with|ksh93}}
<syntaxhighlight lang="ksh">function entropy {
<syntaxhighlight lang="ksh">function entropy {
typeset -i i len=${#1}
typeset -i i len=${#1}
Line 1,852: Line 1,847:
{{out}}
{{out}}
<pre> 1.8464394E+00</pre>
<pre> 1.8464394E+00</pre>

=={{header|NetRexx}}==
=={{header|NetRexx}}==
{{trans|REXX}}
{{trans|REXX}}
Line 1,953: Line 1,949:


=={{header|Objeck}}==
=={{header|Objeck}}==

<syntaxhighlight lang="objeck">use Collection;
<syntaxhighlight lang="objeck">use Collection;


Line 2,015: Line 2,010:


=={{header|OCaml}}==
=={{header|OCaml}}==
;By using a map, purely functional
<syntaxhighlight 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 *)
(* pre-bake & return an inner-loop function to bin & assemble a character frequency map *)
let get_fproc (m: (char, int) Hashtbl.t) :(char -> unit) =
let get_fproc (m: (char, int) Hashtbl.t) :(char -> unit) =
Line 2,046: Line 2,056:
-1.0 *. List.fold_left (fun b x -> b +. calc x) 0.0 relative_probs
-1.0 *. List.fold_left (fun b x -> b +. calc x) 0.0 relative_probs
</syntaxhighlight>
</syntaxhighlight>
{{out}}

<pre>1.84643934467</pre>
'''output:'''

1.84643934467


=={{header|Oforth}}==
=={{header|Oforth}}==

<syntaxhighlight lang="oforth">: entropy(s) -- f
<syntaxhighlight lang="oforth">: entropy(s) -- f
| freq sz |
| freq sz |
Line 2,108: Line 2,115:


=={{header|Pascal}}==
=={{header|Pascal}}==

Free Pascal (http://freepascal.org).
Free Pascal (http://freepascal.org).

<syntaxhighlight lang="pascal">
<syntaxhighlight lang="pascal">
PROGRAM entropytest;
PROGRAM entropytest;
Line 2,216: Line 2,221:


=={{header|PHP}}==
=={{header|PHP}}==

<syntaxhighlight lang="php"><?php
<syntaxhighlight lang="php"><?php


Line 2,365: Line 2,369:


=={{header|Prolog}}==
=={{header|Prolog}}==

{{works with|Swi-Prolog|7.3.3}}
{{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.
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]).
<syntaxhighlight lang="prolog">:-module(shannon_entropy, [shannon_entropy/2]).


Line 2,620: Line 2,621:


=={{header|R}}==
=={{header|R}}==

<syntaxhighlight lang="rsplus">
<syntaxhighlight lang="rsplus">
entropy <- function(str) {
entropy <- function(str) {
Line 2,933: Line 2,933:


=={{header|Ruby}}==
=={{header|Ruby}}==

<syntaxhighlight lang="ruby">def entropy(s)
<syntaxhighlight lang="ruby">def entropy(s)
counts = s.chars.tally
counts = s.chars.tally
Line 3,170: Line 3,169:
Entropy "1223334444" ;
Entropy "1223334444" ;
val it = 1.846439345: real
val it = 1.846439345: real

=={{header|Swift}}==
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
<syntaxhighlight lang="swift">import Foundation
Line 3,210: Line 3,210:


=={{header|V (Vlang)}}==
=={{header|V (Vlang)}}==

===Vlang: Map version===
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
<syntaxhighlight lang="v (vlang)">import math