Skip to content

Commit 87ac812

Browse files
author
Rustam Guliev
committed
Fix issue #58
1 parent 12a12db commit 87ac812

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

pkg/unmixR/R/simplex.R

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@
9090
})
9191
}
9292

93+
#' Faster replacement for \code{abs(det(E))} for simplex volume
94+
#'
95+
#' @param x matrix
96+
#'
97+
#' @return absolute value of determinant of x
98+
#'
99+
#' @include unmixR-package.R
100+
#' @rdname simplex
101+
.absdet <- function(x) {
102+
determinant(x, logarithm = FALSE)$modulus[1]
103+
}
104+
105+
.test(.absdet) <- function() {
106+
context("absdet")
107+
108+
test_that("correct determinant", {
109+
expect_equal(.absdet(matrix(c(1, 2, 3, 4), nrow = 2)), abs(1*4 - 2*3))
110+
})
111+
112+
test_that("random matrix", {
113+
set.seed(1)
114+
x <- matrix(rnorm(9), nrow = 3)
115+
expect_equal(.absdet(x), abs(det(x)))
116+
})
117+
}
118+
93119
#' Volume of a simplex
94120
#'
95121
#' @param data matrix with coordinates in rows
@@ -111,10 +137,10 @@
111137
simplex_volume <- function(data, indices = seq_len(nrow(data)), factorial = TRUE) {
112138
if (ncol(data) == length(indices) - 1) {
113139
E <- .simplex_E(data, indices, type="vector")
114-
volume <- abs(det(E))
140+
volume <- .absdet(E)
115141
} else {
116142
E <- .simplex_E(data, indices, type="gram")
117-
volume <- sqrt(abs(det(E)))
143+
volume <- sqrt(.absdet(E))
118144
}
119145

120146
if (factorial) {

0 commit comments

Comments
 (0)