Skip to content

Commit

Permalink
stub to generalize angle in dev/
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Dec 7, 2023
1 parent 0ab6524 commit 4ab118b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ docs/
pkgdown/
^\.Rproj\.user$
^CRAN-SUBMISSION$
dev/
5 changes: 5 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ formatNumbers <- function(x, fractions=FALSE, tol=sqrt(.Machine$double.eps)){
ret
}

# radians and degrees
r2d <-function(r) r/pi * 180

d2r <-function(d) pi*d / 180

rad2deg <- function(rad) (rad * 180) / pi

deg2rad <- function(deg) (deg * pi) / 180

Fractions <- function(x, ...){
opts <- getOption("fractions")
if (is.null(opts)){
Expand Down
40 changes: 40 additions & 0 deletions dev/angle.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generalize the angle function to allow angle(X, Y), where X, Y are matrices
# and we want to calculate the angles between the columns of X, Y.
# Or, angle(X), and want to calculate the angles between the columns of X

angle <- function(x, y, degree = TRUE) {
if(missing(y)) y <- x
if(is.vector(x) && is.atomic(x)) {
theta <- acos(x %*% y / (len(x) * len(y)))
}
else {
theta <- acos(t(x) %*% y / outer(len(x), len(y)))
}
if(degree) theta <- r2d(theta)
theta
}

if(FALSE) {
# one or two vectors
x <- c(-2,1)
y <- c(1,1)
angle(x, y) # degrees
angle(x, y, degree = FALSE) # radians
angle(x) # for one arg case

# matrices

X <- matrix(
c(1, 0, 1, 1, -2, 1),
ncol = 2,
byrow = TRUE)

Y <- matrix(
c(1, 1, -1, -1),
ncol = 2,
byrow = TRUE)

angle(X, Y)
angle(X)

}

0 comments on commit 4ab118b

Please sign in to comment.