r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

Unusual units with log(x) that cannot be divided #310

Closed billdenney closed 1 year ago

billdenney commented 2 years ago

This may be related to #249 or #292.

I'm working with fitting data on the log scale that simplifies to the below evaluation. There are three underlying issues here that may be better treated separately. (If you'd prefer them opened as separate issues, I'm happy to do so.)

Issue 1 is that it seems odd that units would create a unit that it cannot recognize. I recognize that more is probably happening here, but if it were possible to simplify the units with log transforms as indicated by setting the units to "ln(g)" in the example below, it would work.

Issue 2 is that it can add the units but it cannot divide them. It would seem that even if units could not be recognized, if they are identical, you could end up with a unitless value, regardless.

Issue 3 is that when dividing log units by themselves, they do not cancel to be unitless.

x1 <- log(units::set_units(2, "g"))
x1 + x1
#> 1.386294 [ln(re 1 g)]
x1/x1
#> Warning: Could not parse expression: '`ln`(`re` * ^(1) * `g`)'. Returning as a
#> single symbolic unit()
#> Error: '`ln`(`re` * ^(1) * `g`)' is not a unit recognized by udunits or a user-defined unit
x2 <- x1
attr(x2, "units")$numerator <- "ln(g)"
x2 + x2
#> 1.386294 [ln(g)]
x2/x2
#> 1 [ln(g)/ln(g)]

Created on 2022-03-09 by the reprex package (v2.0.1)

Enchufa2 commented 2 years ago

Issue 1: The created unit is correct:

units:::R_ut_parse("ln(re 1 g)")
#> <pointer: 0x55d5ff551810>

It's not that nice, but it's what udunits2 uses and recognises.

Issue 2: Our parser should be improved to support this logarithm notation, because the error message is unfortunate, but:

units:::R_ut_parse("ln(re 1 g) / ln(re 1 g)")
#> Error in units:::R_ut_parse("ln(re 1 g) / ln(re 1 g)"): logRaise(): Can't raise logarithmic-unit to non-zero power

udunits2 does not support multiplying / dividing logarithmic units.

Issue 3: This is a non-issue. ln(g) is simply a non-valid unit.

units:::R_ut_parse("ln(g)")
#> Error in units:::R_ut_parse("ln(g)"): syntax error, cannot parse 'ln(g)'
billdenney commented 2 years ago

Thanks for the quick response.

Specifically for division of units with a log (and generally any division), could a special case be used where if the units are identical for the numerator and denominator, then it will automatically become unitless?