-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# hrep 0.6.0 | ||
|
||
- Added a `NEWS.md` file to track changes to the package. | ||
- Added `milne_pc_spec_dist` for computing spectral distances between sonorities. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#' Cosine similarity | ||
#' | ||
#' Computes the cosine similarity between two numeric vectors. | ||
#' @param x Numeric vector 1. | ||
#' @param y Numeric vector 2. | ||
#' @return Cosine similarity, as a numeric scalar. | ||
#' @export | ||
cosine_similarity <- function(x, y) { | ||
numerator <- sum(x * y) | ||
denominator <- | ||
sqrt(sum(x ^ 2)) * | ||
sqrt(sum(y ^ 2)) | ||
numerator / denominator | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' Milne's pitch-class spectral distance | ||
#' | ||
#' Computes Milne's pitch-class spectral distance measure between two sonorities. | ||
#' | ||
#' @param x First sonority, passed to \code{\link{milne_pc_spectrum}}. | ||
#' @param y Second sonority, passed to \code{\link{milne_pc_spectrum}}. | ||
#' @param ... Further arguments passed to \code{\link{milne_pc_spectrum}}. | ||
#' | ||
#' @return | ||
#' A numeric scalar where greater values indicate greater distance. | ||
#' 0 is the minimum possible distance. | ||
#' 1 corresponds to two uncorrelated spectra; | ||
#' values greater than 1 are possible if the spectra are inversely correlated. | ||
#' | ||
#' @details | ||
#' See \insertCite{Milne2016;textual}{hrep} for details. | ||
#' | ||
#' @export | ||
milne_pc_spec_dist <- function(x, y, ...) { | ||
1 - cosine_similarity( | ||
milne_pc_spectrum(x, ...), | ||
milne_pc_spectrum(y, ...) | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
context("test-milne_pc_spec_dist") | ||
|
||
test_that("examples", { | ||
approx_equal <- function(x, y, digits = 4) { | ||
all.equal( | ||
round(x, digits = digits), | ||
round(y, digits = digits) | ||
) | ||
} | ||
|
||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(0, 4, 7) | ||
), | ||
0 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(0, 3, 7) | ||
), | ||
0.2597 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(0, 2, 7) | ||
), | ||
0.2201 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(1, 5, 8) | ||
), | ||
0.8527 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(2, 7, 11) | ||
), | ||
0.4155 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(0, 4, 7), | ||
c(0, 5, 9) | ||
), | ||
0.4155 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(2, 7, 8), | ||
c(3, 4, 8, 9) | ||
), | ||
0.5619 | ||
) | ||
) | ||
expect_true( | ||
approx_equal( | ||
milne_pc_spec_dist( | ||
c(2, 6, 8, 9), | ||
c(1, 4) | ||
), | ||
0.5940 | ||
) | ||
) | ||
}) | ||
|
||
test_that("coercion", { | ||
expect_equal( | ||
milne_pc_spec_dist(c(0, 4, 7), c(0, 3, 7)), | ||
milne_pc_spec_dist(c(60, 64, 67), c(60, 63, 67)) | ||
) | ||
|
||
expect_equal( | ||
milne_pc_spec_dist(c(0, 4, 7), c(0, 3, 7)), | ||
milne_pc_spec_dist(pi_chord(c(60, 64, 67)), pi_chord(c(60, 63, 67))) | ||
) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
context("test-r-cosine-similarity") | ||
|
||
test_that("examples", { | ||
expect_equal(cosine_similarity(rep(1, times = 10), | ||
rep(2, times = 10)), | ||
1) | ||
expect_equal(cosine_similarity(rep(1, times = 10), | ||
rep(-2, times = 10)), | ||
-1) | ||
expect_equal( | ||
cosine_similarity( | ||
c(0, 3, 0, 0, 2, 0, 0, 2, 0, 5), | ||
c(1, 2, 0, 0, 1, 1, 0, 1, 0, 3) | ||
), | ||
0.94, | ||
tolerance = 1e-2 | ||
) | ||
expect_equal( | ||
cosine_similarity( | ||
c(0, 3, 0, 0, 2, 0, 0, 2, 0, 5), | ||
c(1, 2, 0, 0, 1, 1, 0, 1, 0, 3) | ||
), | ||
0.9356015 | ||
) | ||
}) |