diff --git a/.Rbuildignore b/.Rbuildignore index a6a8b1e..6da8a56 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,3 +7,5 @@ README.md ^quantities$ ^revdep$ +^\.positai$ +^\.claude$ diff --git a/.gitignore b/.gitignore index 28b2a66..0366d92 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ configure.scan # OSX-specific .DS_Store inst/doc +.positai diff --git a/DESCRIPTION b/DESCRIPTION index e977c5a..115332c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: units -Version: 1.0-1.3 +Version: 1.0-1.4 Title: Measurement Units for R Vectors Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de", comment = c(ORCID = "0000-0001-8049-7069")), person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"), diff --git a/NEWS.md b/NEWS.md index ef873ad..1dc2f5e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ * Several fixes in documentation files; #433 @tszberkowitz +* Simplify unitless powers automatically; #435 addressing #179, #434 + # version 1.0-1 * Add internal workaround for udunits2 bug with parsing of units that contain diff --git a/R/arith.R b/R/arith.R index 8d5cb79..0751200 100644 --- a/R/arith.R +++ b/R/arith.R @@ -128,12 +128,14 @@ Ops.units <- function(e1, e2) { return(.simplify_units(NextMethod(), .symbolic_units(numerator, denominator))) } else if (pw) { - if (e2_inherits_units) { - if (e1_inherits_units && identical(units(e1), units(as_units(1)))) { + if (e2_inherits_units && identical(units(e2), unitless)) { + e2 <- drop_units(e2) + if (!e1_inherits_units) + return(NextMethod()) + + } else if (e2_inherits_units) { + if (e1_inherits_units && identical(units(e1), unitless)) e1 <- drop_units(e1) - } else if (e1_inherits_units) { - stop("power operation only allowed with numeric power") - } # code to manage things like exp(log(...)) and 10^log10(...) follows # this is not supported in udunits2, so we are on our own @@ -179,7 +181,7 @@ Ops.units <- function(e1, e2) { # when the power is negative and we have a special case when it is zero where # units should be removed. if (e2 == 0) { - u <- units(as_units(1)) + u <- unitless } else { tbl_den <- tabulate(factor(units(e1)$denominator)) tbl_num <- tabulate(factor(units(e1)$numerator)) diff --git a/tests/testthat/test_arith.R b/tests/testthat/test_arith.R index 3105f67..e70db59 100644 --- a/tests/testthat/test_arith.R +++ b/tests/testthat/test_arith.R @@ -91,8 +91,8 @@ test_that("we can take powers of units", { expect_equal(as.numeric(ux ** 0), x ** 0) expect_equal(as.numeric(ux ^ 0), x ^ 0) - expect_identical(units(ux ** 0), units(as_units(1))) - expect_identical(units(ux ^ 0), units(as_units(1))) + expect_identical(units(ux ** 0), unitless) + expect_identical(units(ux ^ 0), unitless) }) test_that("we support unary +/-", { @@ -181,7 +181,7 @@ test_that("we can undo logatithms", { expect_equal(expm1(3^log(log1p(y), base=3)), set_units(y, m^2)) expect_error(exp(log10(x)), "wrong base in power operation") expect_error(exp(x), "only allowed with logarithmic unit") - expect_error(exp(set_units(1, 1)), "only allowed with logarithmic unit") + expect_equal(exp(x/y), exp(drop_units(x/y))) }) test_that("%/% and %% work", {