fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Microsoft.FSharp.Math functions should support units of measure

Open jackfoxy opened this issue 6 years ago • 8 comments

Is your feature request related to a problem? Please describe. Math functions require untagging UOMs

Describe the solution you'd like Math functions should respect UOMs

Describe alternatives you've considered I have to untag and retag UOM every time I use a Math function

jackfoxy avatar Sep 16 '19 17:09 jackfoxy

Math functions should respect UOMs

The relevant ones seem to be Abs, Max, Min, Sign. (Edit: not pown)

charlesroddie avatar Sep 16 '19 21:09 charlesroddie

Math functions should respect UOMs

The relevant ones seem to be Abs, Max, Min, Sign, and pown.

I'm not sure it makes sense for all of those to be applied to UOM. Say we have a velocity, x. Then Abs x is no longer a velocity (but rather a speed). Also, Sign x is not a velocity. Pown seems the most dubious of all: what is velocity^dollars, or even 3.14^velocity ? I don't see a problem with Max or Min, though.

nasosev avatar Sep 18 '19 02:09 nasosev

Abs:'num<'m>->'num<'m>
Sign:'num<'m>->int

(On phone. Sorry if the type syntax isn't right.) pown won't work on second thoughts as the measure of the output depends on the number of the integer exponent and that is not statically known.

charlesroddie avatar Sep 19 '19 15:09 charlesroddie

Abs:'num<'m>->'num<'m>
Sign:'num<'m>->int

(On phone. Sorry if the type syntax isn't right.) pown won't work on second thoughts as the measure of the output depends on the number of the integer exponent and that is not statically known.

In general, it only makes sense to use algebraic functions (versus transcendental functions) on UOM: see https://en.wikipedia.org/wiki/Transcendental_function#Dimensional_analysis .

Since Abs = sqrt(x^2), I guess that one does make sense.

nasosev avatar Sep 19 '19 16:09 nasosev

That's not right. Functions which are homogenous in each variable are compatible with units. This excludes algebraic functions like x^2+x.

abs is not quite homogenous but is homogenous when restricted to multiplication by positive quantities. So if units are considered positive (which is acceptable), then abs can work.

charlesroddie avatar Sep 19 '19 16:09 charlesroddie

That's not right. Functions which are homogenous in each variable are compatible with units. This includes non-algebraic functions like abs, and excludes algebraic functions like x^2+x.

Being homogeneous is a necessary condition, so is being algebraic. Abs x = sqrt(x^2) is an example of a homogeneous algebraic function.

nasosev avatar Sep 19 '19 16:09 nasosev

Please see above post as I was editing my (wrong) answer when you replied.

charlesroddie avatar Sep 19 '19 17:09 charlesroddie

I'm not sure why you want to assume units are positive. With that assumption there would be no reason to apply Abs in the first place?

nasosev avatar Sep 19 '19 17:09 nasosev

Min/Max/Abs functions do support units of measure ;; those are also the functions where it makes sense to keep the units. I will close the issue for now - please let us know if more support is needed via examples of expected consumption.

T-Gro avatar Dec 22 '22 16:12 T-Gro