Entropy: Difference between revisions

→‎OCaml: add a more idiomatic variant
(→‎Ksh: add)
(→‎OCaml: add a more idiomatic variant)
Line 243:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">entropy: function [s][
t: #[]
Line 427 ⟶ 426:
 
=={{header|C}}==
 
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 666 ⟶ 664:
 
=={{header|Common Lisp}}==
 
Not very Common Lisp-y version:
 
<syntaxhighlight lang="lisp">(defun entropy (string)
(let ((table (make-hash-table :test 'equal))
Line 778 ⟶ 774:
{{out}}
<pre>1.84644</pre>
 
=={{header|Delphi}}==
{{libheader| StrUtils}}
Line 844 ⟶ 841:
readln;
end.</syntaxhighlight>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 881 ⟶ 879:
 
</syntaxhighlight>
 
 
=={{header|Elena}}==
Line 1,078 ⟶ 1,075:
 
=={{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">
Line 1,213 ⟶ 1,209:
 
=={{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.
 
Line 1,338 ⟶ 1,333:
 
=={{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,475 ⟶ 1,469:
2
3
4</pre> =={{header|JavaScript}}==
;Another variant
<syntaxhighlight lang="javascript">const entropy = (s) => {
const split = s.split('');
Line 1,555 ⟶ 1,550:
=={{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")
Line 1,613 ⟶ 1,607:
 
=={{header|Ksh}}==
{{works with|ksh93}}
<syntaxhighlight lang="ksh">function entropy {
typeset -i i len=${#1}
Line 1,852 ⟶ 1,847:
{{out}}
<pre> 1.8464394E+00</pre>
 
=={{header|NetRexx}}==
{{trans|REXX}}
Line 1,953 ⟶ 1,949:
 
=={{header|Objeck}}==
 
<syntaxhighlight lang="objeck">use Collection;
 
Line 2,015 ⟶ 2,010:
 
=={{header|OCaml}}==
;By using a map, purely functional
<syntaxhighlight lang="ocaml">(*module genericCharMap OCaml,= using a mutable Hashtbl *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 2,046 ⟶ 2,056:
-1.0 *. List.fold_left (fun b x -> b +. calc x) 0.0 relative_probs
</syntaxhighlight>
{{out}}
 
<pre>1.84643934467</pre>
'''output:'''
 
1.84643934467
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">: entropy(s) -- f
| freq sz |
Line 2,108 ⟶ 2,115:
 
=={{header|Pascal}}==
 
Free Pascal (http://freepascal.org).
 
<syntaxhighlight lang="pascal">
PROGRAM entropytest;
Line 2,216 ⟶ 2,221:
 
=={{header|PHP}}==
 
<syntaxhighlight lang="php"><?php
 
Line 2,365 ⟶ 2,369:
 
=={{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]).
 
Line 2,620 ⟶ 2,621:
 
=={{header|R}}==
 
<syntaxhighlight lang="rsplus">
entropy <- function(str) {
Line 2,933:
 
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">def entropy(s)
counts = s.chars.tally
Line 3,170 ⟶ 3,169:
Entropy "1223334444" ;
val it = 1.846439345: real
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
Line 3,210:
 
=={{header|V (Vlang)}}==
 
===Vlang: Map version===
<syntaxhighlight lang="v (vlang)">import math
559

edits