R/uniform_sphere_points.R

#' @title uniform_sphere_points
#' @description Function that generates n points uniformly within the unit sphere in d dimensions.#'
#' @param n number of points
#' @param d dimension
#' @return X = the matrix generated
#' @export
# Refer to: http://stackoverflow.com/questions/33350190/pointwise-multiplication-and-right-matrix-division
uniform_sphere_points <- function(n, d) {
  z <- matrix(rnorm(n * d), nrow = n, ncol = d)
  return(z * (runif(n) ^ (1 / d) / sqrt(rowSums(z ^ 2))))
}
jhoffjann/nmmso.R documentation built on May 19, 2019, 9:26 a.m.