utils/sphere_sample.R

sphere.samp = function(n,dims) {
  d = matrix(NA, nrow=n, ncol=dims)
  for(i in 1:n) {
    # from http://mathworld.wolfram.com/HyperspherePointPicking.html
    x = rnorm(dims)
    d[i,] = x / sqrt(sum(x*x))
  }
  d = data.frame(d)
  names(d) = stringr::str_c("x", 1:dims)
  d
}

upper.samp = function(n,dims) {
  d = matrix(NA, nrow=n, ncol=dims)
  for(i in 1:n) {
    # from http://mathworld.wolfram.com/HyperspherePointPicking.html
    x = rep(-1, dims)
    # keep sampling until everything is positive
    while(any(x < 0)) {
      x = rnorm(dims)
    }
    d[i,] = x / sqrt(sum(x*x))
  }
  d = data.frame(d)
  names(d) = stringr::str_c("x", 1:dims)
  d
}
gabysbrain/hypersliceplorer documentation built on Nov. 17, 2022, 1:42 p.m.