Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have some functions for hypercube and n-sphere selection that generate dataframes with cartesian coordinates and guarantee a uniform distribution through the hypercube or n-sphere for an arbitrary amount of dimensions :</p> <pre><code>GenerateCubiclePoints &lt;- function(nrPoints,nrDim,center=rep(0,nrDim),l=1){ x &lt;- matrix(runif(nrPoints*nrDim,-1,1),ncol=nrDim) x &lt;- as.data.frame( t(apply(x*(l/2),1,'+',center)) ) names(x) &lt;- make.names(seq_len(nrDim)) x } </code></pre> <p>is in a cube/hypercube of <code>nrDim</code> dimensions with a <code>center</code> and <code>l</code> the length of one side. </p> <p>For an n-sphere with <code>nrDim</code> dimensions, you can do something similar, where <code>r</code> is the radius :</p> <pre><code>GenerateSpherePoints &lt;- function(nrPoints,nrDim,center=rep(0,nrDim),r=1){ #generate the polar coordinates! x &lt;- matrix(runif(nrPoints*nrDim,-pi,pi),ncol=nrDim) x[,nrDim] &lt;- x[,nrDim]/2 #recalculate them to cartesians sin.x &lt;- sin(x) cos.x &lt;- cos(x) cos.x[,nrDim] &lt;- 1 # see the formula for n.spheres y &lt;- sapply(1:nrDim, function(i){ if(i==1){ cos.x[,1] } else { cos.x[,i]*apply(sin.x[,1:(i-1),drop=F],1,prod) } })*sqrt(runif(nrPoints,0,r^2)) y &lt;- as.data.frame( t(apply(y,1,'+',center)) ) names(y) &lt;- make.names(seq_len(nrDim)) y } </code></pre> <p>in 2 dimensions, these give :</p> <p><img src="https://i.stack.imgur.com/2c2kY.png" alt="enter image description here"></p> <p>From code :</p> <pre><code> T1 &lt;- GenerateCubiclePoints(10000,2,c(4,3),5) T2 &lt;- GenerateSpherePoints(10000,2,c(-5,3),2) op &lt;- par(mfrow=c(1,2)) plot(T1) plot(T2) par(op) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload