| 1 |
# Inside the group generic functions we do have .Generic even if the diagnostics |
| 2 |
# think we do not. |
| 3 |
# !diagnostics suppress=.Generic |
| 4 |
|
| 5 |
#' Mathematical operations for units objects |
| 6 |
#' |
| 7 |
#' @param x object of class units |
| 8 |
#' @param ... parameters passed on to the Math functions |
| 9 |
#' |
| 10 |
#' @details Logarithms receive a special treatment by the underlying \pkg{udunits2} |
| 11 |
#' library. If a natural logarithm is applied to some \code{unit}, the result is |
| 12 |
#' \code{ln(re 1 unit)}, which means \emph{natural logarithm referenced to |
| 13 |
#' \code{1 unit}}. For base 2 and base 10 logarithms, the output \code{lb(...)} |
| 14 |
#' and \code{lg(...)} respectively instead of \code{ln(...)}. |
| 15 |
#' |
| 16 |
#' This is particularly important for some units that are typically expressed in |
| 17 |
#' a logarithmic scale (i.e., \emph{bels}, or, more commonly, \emph{decibels}), |
| 18 |
#' such as Watts or Volts. For some of these units, the default \pkg{udunits2} |
| 19 |
#' database contains aliases: e.g., \code{BW} (bel-Watts) is an alias of |
| 20 |
#' \code{lg(re 1 W)}; \code{Bm} (bel-milliWatts) is an alias of |
| 21 |
#' \code{lg(re 0.001 W)}; \code{BV} is an alias of \code{lg(re 1 V)} (bel-Volts), |
| 22 |
#' and so on and so forth (see the output of \code{valid_udunits()} for further |
| 23 |
#' reference). |
| 24 |
#' |
| 25 |
#' Additionally, the \pkg{units} package defines \code{B}, the \emph{bel}, by |
| 26 |
#' default (because it is not defined by \pkg{udunits2}) as an alias of |
| 27 |
#' \code{lg(re 1)}, unless a user-provided XML database already contains a |
| 28 |
#' definition of \code{B}, or the \code{define_bel} option is set to \code{FALSE} |
| 29 |
#' (see \code{help(units_options)}). |
| 30 |
#' |
| 31 |
#' @export |
| 32 |
#' |
| 33 |
#' @examples |
| 34 |
#' # roundings, cummulative functions |
| 35 |
#' x <- set_units(sqrt(1:10), m/s) |
| 36 |
#' signif(x, 2) |
| 37 |
#' cumsum(x) |
| 38 |
#' |
| 39 |
#' # trigonometry |
| 40 |
#' sin(x) # not meaningful |
| 41 |
#' x <- set_units(sqrt(1:10), rad) |
| 42 |
#' sin(x) |
| 43 |
#' cos(x) |
| 44 |
#' x <- set_units(seq(0, 1, 0.1), 1) |
| 45 |
#' asin(x) |
| 46 |
#' acos(x) |
| 47 |
#' |
| 48 |
#' # logarithms |
| 49 |
#' x <- set_units(sqrt(1:10), W) |
| 50 |
#' log(x) # base exp(1) |
| 51 |
#' log(x, base = 3) |
| 52 |
#' log2(x) |
| 53 |
#' log10(x) |
| 54 |
#' set_units(x, dBW) # decibel-watts |
| 55 |
#' set_units(x, dBm) # decibel-milliwatts |
| 56 |
Math.units = function(x, ...) { |
| 57 |
if (.Generic == "sqrt") |
| 58 |
return(x^0.5) |
| 59 |
|
| 60 |
if (.Generic == "exp") |
| 61 |
return(exp(1)^x) |
| 62 |
|
| 63 |
if (.Generic == "expm1") { |
| 64 |
out <- exp(1)^x |
| 65 |
mult <- drop_units(out) / exp(1)^drop_units(x) |
| 66 |
return(out - set_units(mult, units(out), mode="standard")) |
| 67 |
} |
| 68 |
|
| 69 |
if (.Generic == "sign") |
| 70 |
return(as.numeric(NextMethod())) |
| 71 |
|
| 72 |
OK <- switch(.Generic, "abs" = , "sign" = , "floor" = , "ceiling" = , "log" = , |
| 73 |
"log1p" =, "trunc" = , "round" = , "signif" = , "cumsum" = , |
| 74 |
"cummax" = , "cummin" = TRUE, FALSE) |
| 75 |
|
| 76 |
rad = units(as_units("rad")) |
| 77 |
deg = units(as_units("degree")) |
| 78 |
if (!OK && (units(x) == rad || units(x) == deg || units(x) == unitless)) { |
| 79 |
OK <- switch(.Generic, sin =, cos =, tan =, sinpi =, cospi =, tanpi = TRUE, FALSE) |
| 80 |
if (OK) { |
| 81 |
units(x) <- "rad" # convert deg -> rad |
| 82 |
x <- set_units(x) # result has unit 1 |
| 83 |
} |
| 84 |
} |
| 85 |
if (!OK && units(x) == unitless) { |
| 86 |
OK <- switch(.Generic, asin =, acos =, atan = TRUE, FALSE) |
| 87 |
if (OK) |
| 88 |
units(x) <- "rad" # unit of the answer (also unitless) |
| 89 |
} |
| 90 |
|
| 91 |
if (!OK) { |
| 92 |
warning(paste("Operation", .Generic, "not meaningful for units")) |
| 93 |
x <- drop_units(x) |
| 94 |
NextMethod() |
| 95 |
} else { |
| 96 |
if (.Generic %in% c("log", "log1p")) { |
| 97 |
base <- if (missing(...)) exp(1) else c(...)[1] |
| 98 |
uptr <- R_ut_parse(ud_char(units(x))) |
| 99 |
u <- R_ut_format(R_ut_log(uptr, base), ascii=TRUE) |
| 100 |
.as.units(NextMethod(), units(symbolic_unit(u))) |
| 101 |
} else |
| 102 |
.as.units(NextMethod(), units(x)) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
#' @export |
| 107 |
#' @method log10 units |
| 108 |
log10.units <- function(x) log(x, 10) |
| 109 |
|
| 110 |
#' @export |
| 111 |
#' @method log2 units |
| 112 |
log2.units <- function(x) log(x, 2) |