# R/mu2theta2.R In ahfoss/pkbd: Clustering on the Sphere with Poisson Kernel Based Distributions

#### Documented in mu2theta2

```#' Calculate the angle of a 2-dimensional vector
#'
#' Calculates the angle of a vector with respect to the vector (1,0).
#'
#' Returns the angle needed to rotate (1,0) counterclockwise to match the input vector.
#' atan(y/x) + {
#'   0   if x >= 0 and y >= 0 (Q1)
#'   pi  if x <  0 and y >  0 (Q2)
#'   pi  if x <  0 and y <= 0 (Q3)
#'   2pi if x >= 0 and y <  0 (Q4)
#' }
#'
#' @param mu a vector of length 2 or a N x 2 matrix of means in two dimensions
#' @export
mu2theta2 <- function(mu) {
if (is.vector(mu)) {
mu <- matrix(mu, nrow = 1)
}
if (ncol(mu) != 2) {
stop('Means must be of dimension 2')
}
# normalize to length 1
mu <- mu / sqrt(rowSums(mu^2))
at <- atan(mu[,2] / mu[,1])
quadrant2 <- pi * (mu[,1] < 0 & mu[,2] > 0)
quadrant3 <- pi * (mu[,1] < 0 & mu[,2] <= 0)
quadrant4 <- 2 * pi * (mu[,1] >= 0 & mu[,2] < 0)
return(quadrant2 + quadrant3 + quadrant4 + at)
}
```
ahfoss/pkbd documentation built on June 4, 2017, 3:20 a.m.