Percolation/Mean cluster density: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25:
{{trans|Nim}}
<
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))</
{{out}}
Line 111:
=={{header|C}}==
<
#include <stdlib.h>
Line 187:
free(map);
return 0;
}</
{{out}}
<pre>
Line 219:
=={{header|D}}==
{{trans|python}}
<
std.range, std.ascii;
Line 308:
nIters, prob, side, density);
}
}</
{{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.
<
(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>
{{out}}
<pre>
Line 389:
=={{header|Factor}}==
<
math.matrices random sequences ;
IN: rosetta-code.mean-cluster-density
Line 430:
] each ;
MAIN: main</
{{out}}
<pre>
Line 442:
=={{header|Go}}==
{{trans|Python}}
<
import (
Line 539:
fmt.Printf("t=%3d p=%4.2f n=%5d sim=%7.5f\n", t, p, n, sim)
}
}</
{{out}}
<pre>
Line 569:
=={{header|Haskell}}==
<
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]</
<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.
<
Example:
<
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</
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.
<
Example use:
<
1 0 0 0 0 0
0 0 0 2 0 0
Line 712:
CC....
...D.E
FF.DD.</
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:
<
Example use:
<
0.1666667</
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.
<
Example use:
<
0.1666667
0.4 experiment 6
0.1944444</
The task wants us to perform at least five trials for sizes up to 1000 by 1000 with probability of 1 being 0.5:
<
Example use:
<
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</
Now for averages (these are different trials from the above):
<
mean 8 trials 3
0.1805556
Line 767:
0.06749861
mean 8 trials 1000
0.06616738</
Finally, for the extra credit (thru taken from the [[Loops/Downward_for#J|Loops/Downward for]] task):
<
<
A.......B..C...
AAAA...D..E.F..
Line 788:
..AA..A.A...AAA
.M.A.AA.AA..AA.
.MM..A.N..O..A.</
'''Collected definitions'''
<
idclust=: $ $ [: (~. i.])&.(0&,)@,@congeal ] * 1 + i.@$
Line 802:
mean=:+/ % #
thru=: <./ + i.@(+*)@-~</
'''Extra Credit'''
<
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</
=={{header|Julia}}==
{{trans|Python}}
<
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</
{{out}}
Line 938:
=={{header|Kotlin}}==
{{trans|C}}
<
import java.util.Random
Line 1,017:
w = w shl 1
}
}</
Sample output:
Line 1,055:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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</
Line 1,088:
=={{header|Nim}}==
{{trans|Go}}
<
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}"</
{{out}}
Line 1,186:
=={{header|Perl}}==
{{trans|Raku}}
<
$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;
}</
{{out}}
<pre> 1 1 1 . . . . 2 2 2 . . . . .
Line 1,284:
=={{header|Phix}}==
{{trans|C}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,382:
=={{header|Python}}==
<
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))</
{{out}}
Line 1,473:
=={{header|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))</
{{out}}
Line 1,607:
{{works with|Rakudo|2017.02}}
<syntaxhighlight lang="raku"
my $fill = 'x';
Line 1,673:
}
}
</syntaxhighlight>
{{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}}
<
proc determineClusters {w h p} {
Line 1,756:
}
puts "n=$n, K(p)=[expr {$tot/5.0/$n**2}]"
}</
{{out}}
<pre>
Line 1,788:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,869:
Fmt.print("$5d $9.6f", w, t)
w = w << 1
}</
{{out}}
Line 1,908:
=={{header|zkl}}==
{{trans|C}}
<
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
}</
<
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; }</
{{out}}
<pre>
|