Jump to content

Percolation/Mean cluster density: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 25:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom()
:seed = 1664525 * :seed + 1013904223
Line 81:
sum += clusterDensity(n, pp)
V sim = sum / tt
print(‘t = #. p = #.2 n = #4 sim = #.5’.format(tt, pp, n, sim))</langsyntaxhighlight>
 
{{out}}
Line 111:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 187:
free(map);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 219:
=={{header|D}}==
{{trans|python}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.random, std.math, std.array,
std.range, std.ascii;
 
Line 308:
nIters, prob, side, density);
}
}</langsyntaxhighlight>
{{out}}
<pre>Found 26 clusters in this 15 by 15 grid:
Line 340:
=={{header|EchoLisp}}==
We use the canvas bit-map as 2D-matrix. For extra-extra credit, a 800x800 nice cluster tapestry image is shown here : http://www.echolalie.org/echolisp/images/rosetta-clusters-800.png.
<langsyntaxhighlight lang="scheme">
(define-constant BLACK (rgb 0 0 0.6))
(define-constant WHITE -1)
Line 378:
(writeln 'n n 'Cn Cn 'density (// Cn (* n n) 5) )
(vector->pixels C)) ;; to screen
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 389:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators formatting generalizations kernel math
math.matrices random sequences ;
IN: rosetta-code.mean-cluster-density
Line 430:
] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 442:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 539:
fmt.Printf("t=%3d p=%4.2f n=%5d sim=%7.5f\n", t, p, n, sim)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 569:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">{-# language FlexibleContexts #-}
import Data.List
import Data.Maybe
Line 631:
main = newStdGen >>= mapM_ (uncurry (printf "%d\t%.5f\n")) . res
where
res = mapM task [10,50,100,500]</langsyntaxhighlight>
 
<pre>λ> newStdGen >>= putStrLn . showClusters . randomMatrix 15
Line 666:
Once we have this, we can identify clusters by propagating information in a single direction through the matrix using this operation, rotating the matrix 90 degrees, and then repeating this combination of operations four times. And, finally, by keeping at this until there's nothing more to be done.
 
<langsyntaxhighlight Jlang="j">congeal=: |.@|:@((*@[*>.)/\.)^:4^:_</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight Jlang="j"> M=:0.4>?6 6$0
M
1 0 0 0 0 0
Line 691:
71 71 0 0 0 0
0 0 0 149 0 113
131 131 0 149 149 0</langsyntaxhighlight>
 
We did not have to use primes there - any mechanism for assigning distinct positive integers to the 1s would work. And, in fact, it might be nice if - once we found our clusters - we assigned the smallest distinct positive integers to the clusters. This would allow us to use simple indexing to map the array to characters.
 
<langsyntaxhighlight Jlang="j">idclust=: $ $ [: (~. i.])&.(0&,)@,@congeal ] * 1 + i.@$</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> idclust M
1 0 0 0 0 0
0 0 0 2 0 0
Line 712:
CC....
...D.E
FF.DD.</langsyntaxhighlight>
 
Now we just need a measure of cluster density. Formally cluster density seems to be defined as the number of clusters divided by the total number of elements of the matrix. Thus:
 
<langsyntaxhighlight Jlang="j">K=: (%&#~ }.@~.)&,</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> K idclust M
0.1666667</langsyntaxhighlight>
 
So we can create a word that performs a simulation experiment, given a probability getting a 1 and the number of rows (and columns) of our square matrix M.
 
<langsyntaxhighlight Jlang="j">experiment=: K@ idclust@: > 0 ?@$~ ,~</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> 0.4 experiment 6
0.1666667
0.4 experiment 6
0.1944444</langsyntaxhighlight>
 
The task wants us to perform at least five trials for sizes up to 1000 by 1000 with probability of 1 being 0.5:
 
<langsyntaxhighlight Jlang="j">trials=: 0.5&experiment"0@#</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> 6 trials 3
0.1111111 0.1111111 0.2222222 0.1111111 0.1111111 0.3333333
6 trials 10
Line 751:
0.06563333 0.06663333 0.06713333 0.06727778 0.06658889 0.06664444
6 trials 1000
0.066079 0.066492 0.065847 0.065943 0.066318 0.065998</langsyntaxhighlight>
 
Now for averages (these are different trials from the above):
 
<langsyntaxhighlight Jlang="j">mean=: +/%#
mean 8 trials 3
0.1805556
Line 767:
0.06749861
mean 8 trials 1000
0.06616738</langsyntaxhighlight>
 
Finally, for the extra credit (thru taken from the [[Loops/Downward_for#J|Loops/Downward for]] task):
 
<langsyntaxhighlight Jlang="j">thru=: <./ + i.@(+*)@-~</langsyntaxhighlight>
 
<langsyntaxhighlight Jlang="j"> (idclust 0.5 > 0 ?@$~ 15 15) {'.', 'A' thru&.(a.&i.) 'Z'
A.......B..C...
AAAA...D..E.F..
Line 788:
..AA..A.A...AAA
.M.A.AA.AA..AA.
.MM..A.N..O..A.</langsyntaxhighlight>
 
'''Collected definitions'''
 
<langsyntaxhighlight Jlang="j">congeal=: |.@|:@((*@[*>.)/\.)^:4^:_
idclust=: $ $ [: (~. i.])&.(0&,)@,@congeal ] * 1 + i.@$
 
Line 802:
mean=:+/ % #
 
thru=: <./ + i.@(+*)@-~</langsyntaxhighlight>
 
'''Extra Credit'''
 
<langsyntaxhighlight Jlang="j"> M=: (* 1+i.@$)?15 15$2
M
0 2 3 4 0 6 0 8 0 10 11 12 0 0 15
Line 854:
16 16 16 0 0 17 0 15 15 15 15 15 0 15 15
16 16 16 0 17 17 17 0 0 15 0 15 0 0 0
16 16 16 0 0 0 17 17 0 15 15 0 0 18 0</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">using Printf, Distributions
 
newgrid(p::Float64, r::Int, c::Int=r) = rand(Bernoulli(p), r, c)
Line 909:
sim = mean(clusterdensity(p, n) for _ in 1:nrep)
@printf("nrep = %2i p = %.2f dim = %-13s sim = %.5f\n", nrep, p, "$n × $n", sim)
end</langsyntaxhighlight>
 
{{out}}
Line 938:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.util.Random
Line 1,017:
w = w shl 1
}
}</langsyntaxhighlight>
 
Sample output:
Line 1,055:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">(*Calculate C_n / n^2 for n=1000, 2000, ..., 10 000*)
In[1]:= Table[N[Max@MorphologicalComponents[
RandomVariate[BernoulliDistribution[.5], {n, n}],
Line 1,064:
 
(*Show a 15x15 matrix with each cluster given an incrementally higher number, Colorize instead of MatrixForm creates an image*)
In[3]:= MorphologicalComponents[RandomChoice[{0, 1}, {15, 15}], CornerNeighbors -> False] // MatrixForm</langsyntaxhighlight>
 
 
Line 1,088:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat
 
const
Line 1,157:
sum += clusterDensity(n, P)
let sim = sum / T
echo &"t = {T} p = {P:4.2f} n = {n:4} sim = {sim:7.5f}"</langsyntaxhighlight>
 
{{out}}
Line 1,186:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">$fill = 'x';
$D{$_} = $i++ for qw<DeadEnd Up Right Down Left>;
 
Line 1,257:
$total += perctest($N) for 1..$trials;
printf "𝘱 = 0.5, trials = $trials, 𝘕 = %4d, 𝘒 = %.4f\n", $N, $total / $trials;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 . . . . 2 2 2 . . . . .
Line 1,284:
=={{header|Phix}}==
{{trans|C}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">grid</span>
Line 1,352:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,382:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from __future__ import division
from random import random
import string
Line 1,444:
sim = fsum(cluster_density(n, p) for i in range(t)) / t
print('t=%3i p=%4.2f n=%5i sim=%7.5f'
% (t, p, n, sim))</langsyntaxhighlight>
 
{{out}}
Line 1,473:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require srfi/14) ; character sets
 
Line 1,569:
(define grd (build-random-grid 1/2 1000 1000))
(/ (for/sum ((g (in-fxvector grd)) #:when (zero? g)) 1) (fxvector-length grd))
(display-sample-clustering 1/2))</langsyntaxhighlight>
 
{{out}}
Line 1,607:
{{works with|Rakudo|2017.02}}
 
<syntaxhighlight lang="raku" perl6line>my @perc;
my $fill = 'x';
 
Line 1,673:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>. . 1 . 2 . . 3 . . . 4 . . .
Line 1,702:
Note that the queue (variables <code>q</code> and <code>k</code>) used to remember where to find cells when flood-filling the cluster is maintained as a list ''segment''; the front of the list is not trimmed for performance reasons. (This would matter with very long queues, in which case the queue could be shortened occasionally; ''frequent'' trimming is still slower though, because Tcl backs its “list” datatype with arrays and not linked lists.)
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc determineClusters {w h p} {
Line 1,756:
}
puts "n=$n, K(p)=[expr {$tot/5.0/$n**2}]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,788:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 1,869:
Fmt.print("$5d $9.6f", w, t)
w = w << 1
}</langsyntaxhighlight>
 
{{out}}
Line 1,908:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">const X=-1; // the sentinal that marks an untouched cell
var C,N,NN,P;
fcn createC(n,p){
Line 1,936:
foreach z in (n){ createC(N,p); k+=countClusters().toFloat()/NN; }
k/n
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">createC(15,0.5);
println("width=%d, p=%.1f, %d clusters:".fmt(N,P,countClusters()));
showCluster();
 
println("p=0.5, 5 iterations:");
w:=4; do(6){ println("%5d %9.6f".fmt(w,tests(w, 5, 0.5))); w*=4; }</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.