-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ docs/ | |
pkgdown/ | ||
^\.Rproj\.user$ | ||
^CRAN-SUBMISSION$ | ||
dev/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |