r-quantities / units

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

Improve arithmetic of logarithms #249

Closed ekatko1 closed 3 years ago

ekatko1 commented 4 years ago

Consider the following code:

a=set_units(5, 1)
b=set_units(2, 1)
log(a) / log(b)

This results in the following error:

Error: ‘`ln`(`re` * ^(1))’ is not a unit recognized by udunits or a user-defined unit
In addition: Warning message:
Could not parse expression: ‘`ln`(`re` * ^(1))’. Returning as a single symbolic unit() 

An equivalent expression,

log(a-b)

Returns 1.098612 [ln(re 1)]

I would expect both expressions to return the same result. I also see no reason to preserve the ln(re 1) notation for unitless quantities.

Kindly, Egor

Enchufa2 commented 4 years ago

Operations with logarithms can be certainly improved, but these expressions are not equivalent:

a=5
b=2
log(a) / log(b)
#> [1] 2.321928
log(a-b)
#> [1] 1.098612

Probably you meant

a=set_units(5, 1)
b=set_units(2, 1)
log(a) - log(b)
#> 0.9162907 [ln(re 1)]
log(a/b)
#> 0.9162907 [ln(re 1)]

which works fine.

edzer commented 4 years ago

I also noted that

> exp(log(set_units(1:10, 1)))
 [1]  1  2  3  4  5  6  7  8  9 10
Warning message:
In Math.units(log(set_units(1:10))) :
  Operation exp not meaningful for units

where we seem to miss the exception?

Enchufa2 commented 4 years ago

Yeap, good catch. We missed the exp case in the Math.units method.

ekatko1 commented 4 years ago

Apologies for the previous blunder with regards to the rules of exponentiation. Thank you for looking into it regardless.

Additionally, the base 10 analogy to edzer's comment does not compute at all:

> 10^log10(set_units(1:10, 1))
Error in UseMethod("units") : 
  no applicable method for 'units' applied to an object of class "c('double', 'numeric')"
billdenney commented 3 years ago

Thank you for fixing this. I recently encountered an issue where I wanted to do something like exp(log(x)) in my code, and I was going to give a bug report/feature request like this one.

So, thanks!