Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request branch #2

Merged
merged 20 commits into from Aug 13, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+103 −42
Diff settings

Always

Just for now

Added tests for arithmetic

  • Loading branch information...
mailund committed Aug 13, 2016
commit b6886a8210b18032b4c9900970f2a5a5b04fd3db
Copy path View file
@@ -3,8 +3,6 @@
# think we do not.
# !diagnostics suppress=.Generic



#' Title
#'
#' @param e1 object of class \code{units},
@@ -49,43 +47,3 @@ Ops.units <- function(e1, e2) {
NextMethod(.Generic)
}

#' Mathematical operations for units objects
#'
#' @param x object of class units
#' @param ... parameters passed on to the Math functions
#'
#' @export
#'
#' @examples
#' a = sqrt(1:10)
#' units(a) = "m/s"
#' log(a)
#' log(a, base = 10)
#' cumsum(a)
#' signif(a, 2)
Math.units = function(x, ...) {
OK <- switch(.Generic, "abs" = , "sign" = , "floor" = , "ceiling" = , "log" = ,
"trunc" = , "round" = , "signif" = , "cumsum" = ,
"cummax" = , "cummin" = TRUE, FALSE)
if (!OK) {
warning(paste("Operation", .Generic, "not meaningful for units"))
x = unclass(x)
attr(x, "units") = NULL
NextMethod(.Generic)
} else {
if (.Generic == "log") {
dts = list(...)
if (is.null(dts$base) || dts$base == exp(1)) # missing or equal to default:
u = paste0("ln(",units(x),")")
else if (dts$base == 10)
u = paste0("lg(",units(x),")")
else if (dts$base == 2)
u = paste0("lb(",units(x),")")
else
stop(paste("log with base", dts$base, "not supported"))
.as.units(NextMethod(.Generic), u)
} else
.as.units(NextMethod(.Generic), units(x))
}
}

Copy path View file
@@ -0,0 +1,44 @@
# Inside the group generic functions we do have .Generic even if the diagnostics
# think we do not.
# !diagnostics suppress=.Generic

#' Mathematical operations for units objects
#'
#' @param x object of class units
#' @param ... parameters passed on to the Math functions
#'
#' @export
#'
#' @examples
#' a = sqrt(1:10)
#' units(a) = "m/s"
#' log(a)
#' log(a, base = 10)
#' cumsum(a)
#' signif(a, 2)
Math.units = function(x, ...) {
OK <- switch(.Generic, "abs" = , "sign" = , "floor" = , "ceiling" = , "log" = ,
"trunc" = , "round" = , "signif" = , "cumsum" = ,
"cummax" = , "cummin" = TRUE, FALSE)
if (!OK) {
warning(paste("Operation", .Generic, "not meaningful for units"))
x = unclass(x)
attr(x, "units") = NULL
NextMethod(.Generic)
} else {
if (.Generic == "log") {
dts = list(...)
if (is.null(dts$base) || dts$base == exp(1)) # missing or equal to default:
u = paste0("ln(",units(x),")")
else if (dts$base == 10)
u = paste0("lg(",units(x),")")
else if (dts$base == 2)
u = paste0("lb(",units(x),")")
else
stop(paste("log with base", dts$base, "not supported"))
.as.units(NextMethod(.Generic), u)
} else
.as.units(NextMethod(.Generic), units(x))
}
}

Copy path View file
@@ -0,0 +1,59 @@
context("Arithmetic")

test_that("we can compare vectors with equal units", {
x <- as.units(1:4, "m")
y <- as.units(1:4, "m")
z <- 2 * y

expect_true(all(x == y))
expect_true(all(x <= y))
expect_true(all(x >= y))

expect_true(all(x < z))
expect_true(all(x <= z))
expect_true(all(z > x))
expect_true(all(z >= x))

expect_false(any(x > y))
expect_false(any(x != y))
expect_true(all(x != z))
})

test_that("we can scale units with scalars", {
x <- 1:4
ux <- as.units(x, "m")

expect_equal(as.numeric(10 * ux), 10 * x)
expect_equal(as.numeric(ux / 10), x / 10)
})

test_that("we can multiply and divide units", {
x <- 1:4 ; y <- 5:8
m <- as.units(x, "m")
s <- as.units(y, "s")

expect_equal(as.numeric(m * s), x * y)
expect_equal(as.numeric(m / s), x / y)

# FIXME: There ought to be a test that the expressions get the right units
# but I am not entirely sure how that should be wrapped. Just checking string
# equality would give problems if units are equivalent but needs to be converted
# first...
})

test_that("we can take powers of units", {
x <- 1:4
ux <- as.units(x, "m")

expect_equal(as.numeric(ux ** 2), x ** 2)
expect_equal(as.numeric(ux ^ 2), x ^ 2)

expect_error(ux ^ ux)
expect_error(ux ^ x)

# FIXME: There ought to be a test that the expressions get the right units
# but I am not entirely sure how that should be wrapped. Just checking string
# equality would give problems if units are equivalent but needs to be converted
# first...
})

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.