| 1 |
|
| 2 |
# Inside the group generic functions we do have .Generic even if the diagnostics |
| 3 |
# think we do not. |
| 4 |
# !diagnostics suppress=.Generic |
| 5 |
|
| 6 |
#' S3 Ops Group Generic Functions for units objects |
| 7 |
#' |
| 8 |
#' Ops functions for units objects, including comparison, product and divide, |
| 9 |
#' add, subtract. |
| 10 |
#' |
| 11 |
#' Users are advised against performing arithmetical operations with |
| 12 |
#' temperatures in different units. The \pkg{units} package ensure that results |
| 13 |
#' 1) are arithmetically correct, and 2) satisfy dimensional analysis, but could |
| 14 |
#' never ensure that results are physically meaningful. Temperature units are |
| 15 |
#' special because there is an absolute unit, Kelvin, and relative ones, Celsius |
| 16 |
#' and Fahrenheit degrees. Arithmetic operations between them are meaningless |
| 17 |
#' from the physical standpoint. Users are thus advised to convert all |
| 18 |
#' temperatures to Kelvin before operating. |
| 19 |
#' |
| 20 |
#' @param e1 object of class \code{units}, |
| 21 |
#' or something that can be coerced to it by \code{as_units(e1)} |
| 22 |
#' @param e2 object of class \code{units}, |
| 23 |
#' or something that can be coerced to it by \code{as_units(e2)}, |
| 24 |
#' or in case of power a number (integer n or 1/n) |
| 25 |
#' |
| 26 |
#' @return object of class \code{units} |
| 27 |
#' @export |
| 28 |
#' |
| 29 |
#' @examples |
| 30 |
#' a <- set_units(1:3, m/s) |
| 31 |
#' b <- set_units(1:3, m/s) |
| 32 |
#' a + b |
| 33 |
#' a * b |
| 34 |
#' a / b |
| 35 |
#' a <- as_units("kg m-3") |
| 36 |
#' b <- set_units(1, kg/m/m/m) |
| 37 |
#' a + b |
| 38 |
#' a = set_units(1:5, m) |
| 39 |
#' a %/% a |
| 40 |
#' a %/% set_units(2) |
| 41 |
#' set_units(1:5, m^2) %/% set_units(2, m) |
| 42 |
#' a %% a |
| 43 |
#' a %% set_units(2) |
| 44 |
Ops.units <- function(e1, e2) { |
| 45 |
if (missing(e2)) |
| 46 |
return(NextMethod()) |
| 47 |
|
| 48 |
cmp <- .Generic %in% c("==", "!=", "<", ">", "<=", ">=") # comparison-type |
| 49 |
div <- .Generic %in% c("/", "%/%") # division-type |
| 50 |
mul <- .Generic %in% "*" # multiplication-only |
| 51 |
prd <- .Generic %in% c("*", "/", "%/%", "%%") # product-type |
| 52 |
pw <- .Generic %in% c("**", "^") # power-type |
| 53 |
mod <- .Generic %in% c("%/%", "%%") # modulo-type |
| 54 |
pm <- .Generic %in% c("+", "-") # addition-type |
| 55 |
|
| 56 |
if (! any(cmp, pm, prd, pw, mod)) |
| 57 |
stop(paste("operation", .Generic, "not allowed")) |
| 58 |
|
| 59 |
e1_inherits_units <- inherits(e1, "units") |
| 60 |
e2_inherits_units <- inherits(e2, "units") |
| 61 |
both_inherit_units <- e1_inherits_units && e2_inherits_units |
| 62 |
|
| 63 |
if ((pm || cmp) && !both_inherit_units) |
| 64 |
stop("both operands of the expression should be \"units\" objects") # nocov |
| 65 |
|
| 66 |
if (cmp) { |
| 67 |
if (!ud_are_convertible(units(e1), units(e2))) |
| 68 |
stop("cannot compare non-convertible units") |
| 69 |
|
| 70 |
if (length(e1) >= length(e2)) { |
| 71 |
e1 <- ud_compare(e1, e2, ud_char(units(e1)), ud_char(units(e2))) |
| 72 |
attr <- attributes(e2) |
| 73 |
e2 <- rep(0, length(e2)) |
| 74 |
attributes(e2) <- attr |
| 75 |
} else { |
| 76 |
e2 <- ud_compare(e2, e1, ud_char(units(e2)), ud_char(units(e1))) |
| 77 |
attr <- attributes(e1) |
| 78 |
e1 <- rep(0, length(e1)) |
| 79 |
attributes(e1) <- attr |
| 80 |
} |
| 81 |
return(NextMethod()) |
| 82 |
} |
| 83 |
|
| 84 |
if (!isFALSE(.units.simplify())) { # see #355 |
| 85 |
# Special cases for identical unit division and inverse unit multiplication |
| 86 |
# which may not be otherwise divisible by udunits (see #310) |
| 87 |
identical_units <- |
| 88 |
both_inherit_units && |
| 89 |
identical(units(e1), units(e2)) |
| 90 |
|
| 91 |
inverse_units <- |
| 92 |
both_inherit_units && |
| 93 |
identical(units(e1)$numerator, units(e2)$denominator) && |
| 94 |
identical(units(e1)$denominator, units(e2)$numerator) |
| 95 |
|
| 96 |
if ((div && identical_units) || (mul && inverse_units)) { |
| 97 |
e1 <- drop_units(e1) |
| 98 |
e2 <- drop_units(e2) |
| 99 |
return(set_units(NextMethod(), 1)) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (mod) { |
| 104 |
div <- e1 / e2 |
| 105 |
int <- floor(div) |
| 106 |
if (.Generic == "%/%") { |
| 107 |
return(int) |
| 108 |
} else { |
| 109 |
return(e1 - int*e2) |
| 110 |
} |
| 111 |
} else if (prd) { |
| 112 |
if (!e1_inherits_units) |
| 113 |
e1 <- set_units(e1, 1) # TODO: or warn? |
| 114 |
|
| 115 |
if (!e2_inherits_units) |
| 116 |
e2 <- set_units(e2, 1) # TODO: or warn? |
| 117 |
|
| 118 |
ve1 <- unclass(e1) ; ue1 <- units(e1) |
| 119 |
ve2 <- unclass(e2) ; ue2 <- units(e2) |
| 120 |
|
| 121 |
if (.Generic == "*") { |
| 122 |
numerator <- sort(c(ue1$numerator, ue2$numerator)) |
| 123 |
denominator <- sort(c(ue1$denominator, ue2$denominator)) |
| 124 |
} else { |
| 125 |
numerator <- sort(c(ue1$numerator, ue2$denominator)) |
| 126 |
denominator <- sort(c(ue1$denominator, ue2$numerator)) |
| 127 |
} |
| 128 |
return(.simplify_units(NextMethod(), .symbolic_units(numerator, denominator))) |
| 129 |
|
| 130 |
} else if (pw) { |
| 131 |
if (e2_inherits_units) { |
| 132 |
if (e1_inherits_units && identical(units(e1), units(as_units(1)))) { |
| 133 |
e1 <- drop_units(e1) |
| 134 |
} else if (e1_inherits_units) { |
| 135 |
stop("power operation only allowed with numeric power") |
| 136 |
} |
| 137 |
|
| 138 |
# code to manage things like exp(log(...)) and 10^log10(...) follows |
| 139 |
# this is not supported in udunits2, so we are on our own |
| 140 |
u <- units(e2) |
| 141 |
invalid_lengths <- length(u$numerator) > 1 || length(u$denominator) |
| 142 |
if (u == unitless || invalid_lengths || !grepl("\\(re", u$numerator)) |
| 143 |
stop("power operation only allowed with logarithmic unit") |
| 144 |
|
| 145 |
sp <- strsplit(u$numerator, "\\(re ")[[1]] |
| 146 |
|
| 147 |
# only works for the same base |
| 148 |
type <- strsplit(sp[1], " ")[[1]] |
| 149 |
fc <- if (length(type) == 1) 1 else as.numeric(type[1]) |
| 150 |
base <- switch(type[length(type)], lb=2, lg=10, ln=exp(1/fc)) |
| 151 |
if (!isTRUE(all.equal(e1, base))) |
| 152 |
stop("wrong base in power operation") |
| 153 |
|
| 154 |
# this is the next unit after "undoing" the outer logarithm |
| 155 |
nxt_u <- paste(sp[-1], collapse="(re ") # next unit |
| 156 |
nxt_u <- substr(nxt_u, 1, nchar(nxt_u)-1) # remove last parenthesis |
| 157 |
|
| 158 |
if (length(sp) > 2) { # another logarithm! |
| 159 |
mult <- 1 |
| 160 |
attr(e2, "units")$numerator <- sub("^1 ", "", nxt_u) |
| 161 |
} else { |
| 162 |
nxt_u <- strsplit(nxt_u, " ")[[1]] |
| 163 |
mult <- as.numeric(nxt_u[1]) |
| 164 |
nxt_u <- as_units(nxt_u[2]) |
| 165 |
attr(e2, "units") <- units(nxt_u) |
| 166 |
} |
| 167 |
return(mult * NextMethod()) |
| 168 |
} |
| 169 |
|
| 170 |
if (length(e2) > 1L) { |
| 171 |
if (length(unique(e2)) == 1) # all identical |
| 172 |
e2 = e2[1] |
| 173 |
else # return mixed units: |
| 174 |
return(.as.mixed_units(mapply("^", e1, e2, SIMPLIFY=FALSE))) |
| 175 |
} |
| 176 |
|
| 177 |
# we repeat each unit the number of times given by e2. They are already |
| 178 |
# sorted so they will remain sorted. We need to flip numerator and denominator |
| 179 |
# when the power is negative and we have a special case when it is zero where |
| 180 |
# units should be removed. |
| 181 |
if (e2 == 0) { |
| 182 |
u <- units(as_units(1)) |
| 183 |
} else { |
| 184 |
tbl_den <- tabulate(factor(units(e1)$denominator)) |
| 185 |
tbl_num <- tabulate(factor(units(e1)$numerator)) |
| 186 |
if (any((tbl_den*e2) %% 1 != 0) || any((tbl_num*e2) %% 1 != 0)) |
| 187 |
stop("powers not divisible") # work on wording |
| 188 |
u <- if (e2 > 0) .symbolic_units( |
| 189 |
rep(unique(units(e1)$numerator), tbl_num*e2), |
| 190 |
rep(unique(units(e1)$denominator), tbl_den*e2)) |
| 191 |
else .symbolic_units( |
| 192 |
rep(unique(units(e1)$denominator), tbl_den*abs(e2)), |
| 193 |
rep(unique(units(e1)$numerator), tbl_num*abs(e2))) |
| 194 |
} |
| 195 |
} else { # pm: |
| 196 |
units(e2) <- units(e1) |
| 197 |
u <- units(e1) |
| 198 |
} |
| 199 |
|
| 200 |
.as.units(NextMethod(), u) |
| 201 |
} |
| 202 |
|
| 203 |
#' S3 matrixOps Group Generic Functions for units objects |
| 204 |
#' |
| 205 |
#' matrixOps functions for units objects. |
| 206 |
#' |
| 207 |
#' @param x object of class \code{units}, |
| 208 |
#' or something that can be coerced to it by \code{as_units(x)} |
| 209 |
#' @param y object of class \code{units}, |
| 210 |
#' or something that can be coerced to it by \code{as_units(y)} |
| 211 |
#' |
| 212 |
#' @return object of class \code{units} |
| 213 |
#' |
| 214 |
#' @examples |
| 215 |
#' a = set_units(1:5, m) |
| 216 |
#' a %*% a |
| 217 |
#' a %*% t(a) |
| 218 |
#' a %*% 1:5 |
| 219 |
#' 1:5 %*% a |
| 220 |
matrixOps.units <- function(x, y) { |
| 221 |
mul <- .Generic %in% c("%*%") # multiplication-only |
| 222 |
|
| 223 |
if (! any(mul)) |
| 224 |
stop(paste("operation", .Generic, "not allowed")) |
| 225 |
|
| 226 |
u <- units(x[0] * y[0]) |
| 227 |
.as.units(NextMethod(), u) |
| 228 |
} |
| 229 |
# registered .onLoad |