Fractional power of units of measures in F#
Asked Answered
C

5

9

Is it true to say that : there are no fractional power units in F#

Considering answered 4/7, 2012 at 17:8 Comment(6)
Yes, it is. So what? Could you describe a use case when you need fractional power?Atlas
yes. I have a matrix of type 'u. I need to to a QR decomposition. the type should be 'u^1/2Considering
I dont understand how one could do a unit system without fractional power. This is absolutely fundamental.Considering
@Atlas do you have code with units of measure in production ?Considering
In the decomposition wouldn't Q be dimensionless (no unit) and R have the same unit as the original matrix?Staford
@Staford actually your decomposition would make more sense indeed as Q is in most case a rotation matrix if i remember well. so let's say CholeskyConsidering
C
11

In addition to what has already been said, the best resource for information about (not just) F# units of measure is Andrew Kennedy's PhD thesis, who actually designed F# units. He mentions fractional units:

The most important decision is whether or not to allow fractional exponents of dimensions. The argument against them is philosophical: a quantity with a dimension such as M1/2 makes no sense physically, and if such a thing arose, it would suggest revision of the set of base dimensions rather than a re-evaluation of integral exponents. The argument in favour is pragmatic: sometimes it is easier to write program code which temporarily creates a value whose dimension has fractional exponents. In this dissertation the former view prevails, and fractional exponents are not considered. However, most of the theory would apply just the same; any potential differences are highlighted as they arise.

I think this is essentially the reason why F# does not have fractional units, because the F# design quite closely follows Andrew Kennedy's work to make sure it is sound.

Update: With F# 4.0, support for fractional exponents has been implemented

Cushiony answered 4/7, 2012 at 22:55 Comment(5)
it has no physical sense indeed. but when abstracting out algorithm, one is confronted to such cases. of course, since it has no physical sense, such fractional power unit will eventually be called with a unit that is raised to the corresponding power. so it really makes sense to have fractional unit if one intends to use them for general purposes, including general algorithmConsidering
I really wonder what would be the computational costs to the compiler. I imagine it would be negligible (so the question is really one of philosophy)Considering
that is very interesting link. I will argue with Mr Kennedy then – after reading his thesisConsidering
Actually, I already suggested him through his webpage (hope it went through) to add symbolic power to the UoM. That would have the immense advantage to handle both the (immense in my pov) case of matrix operation dimensions check and the fractional power issue.Considering
I stand by my previous remark that "the correct notion is that everything starts with physical units, and ends with physical units, but in the meantime, if multiplication is allowed, one has to close the ring, and add fractional units... ". until I grow some more neurons may be.Considering
M
7

Units with fractional exponents are quite common and there is nothing special about them. Probably everyone in technology has come across a voltage noise density, which is measured per sqrt(Hz). This makes a lot of sense physically, the noise power is proportional to the bandwidth, and the noise voltage is the sqrt of the power, no strange mathematics here.

To create a new base unit every time one comes across a fractional power exponent is not the right approach.

These units are not SI units and their use breaks library compatibility. If you define the sqrtHz as a new unit and I define the rootHz, our code can't work together. Anyway, I would need to introduce quite a big set of base units to have a complete set Hz^-2, Hz^3, Hz^-5,... Just to offer rational exponents seems to be the better choice, btw. Boost.units does so.

Mauro answered 22/8, 2012 at 16:9 Comment(1)
I'd even add symbolic powers, because I dont see why not. Practically it was too much effort for the reward, so I removed them from my code. I would have loved to add them unobstrusively, but this might require a deeper framework integration. Right now I see a practical usage for them in short range codebase, in simple cases. that is, where they dont quite shine.Considering
A
2

Absence of literally fractional power units of measure does not anyhow discounts F# unit facility as it allows presenting seemingly fractional exponent unit relationships the other way around having smallest fraction as a base dimension:

let takeSqrt (x: float<_>) = sqrt(x)

has inferred signature of float<'u ^ 2> -> float<'u> this way avoiding introduction of imaginary "naturally fractional" float<'u> -> float<'u^1/2>.

let moreComplicated (x: float<_>) (y: float<_>) =
    sqrt(x*x + y*y*y)

has inferred signature of float<'u ^ 3> -> float<'u ^ 2> -> float<'u ^ 3>, where all unit measure conversions stay valid relative to some derived implicit base dimension float<'u>.

The fact that the piece of code below

[<Measure>]type m
let c = sqrt(1.0<m>)

does not even compile with diagnostics The unit of measure 'm' does not match the unit of measure ''u ^ 2' can be considered as a blame, or a blessing, but is a clear indication that the unit measure checks are in place.

Antigua answered 4/7, 2012 at 18:50 Comment(8)
I agree with you, but note that this means that you can't call takeSqrt 1.0<s> because the return type wouldn't have an integral power. It's conceivable that in some applications having a fractional power of a concrete unit type would be useful.Isochronize
@kvb: Either it has physical meaning and deserves a derived UoM defined, or there's no such meaning and no sense for a variable/binding.Scent
@bytebuster - See e.g. en.wikipedia.org/wiki/Fracture_toughness for one example requiring m^1/2. Sure, you can create a derived unit, but the inability to express it in base units is a real limitation, even if it's one that's relatively rare in practice.Isochronize
@Isochronize I understand the scope, but this works fine for me (sorry for absent formatting): [<Measure>]type KIc [<Measure>]type Pa [<Measure>]type m = KIc^2/Pa^2 let pa:float<Pa> = 50.0<KIc> / sqrt 0.5<m>Scent
@bytebuster - that works if you're defining a new derived m unit, but doesn't work with the built-in SI.m, which would naturally be desirable.Isochronize
@bytebuster this argument about 'physical' things is specious from my pov. in that light, negative number have no physical meaning : it gets it by saying it is the quantity by which a positive number decrease when adding it. well ditto for fractional power unit : it is the unit that raised to an exponent, give a physical unit. the correct notion is that everything starts with physical units, and ends with physical units, but in the meantime, if multiplication is allowed, one has to close the ring, and add fractional units...Considering
@Gene your example does not work with a single class, say, matrix, that should work with different units. then you can not correctly assume that the base type is always a nth power of another.Considering
Gene your example does not work where a single class parameterized with unit, say, matrix<'u>, has function that yields with various fractional power. then you can not correctly assume that the base type 'u is always a nth power of the smallest common multiple of all operation of another type 'v. i mean you can, but your class will be more and more useless as you add functionality to itConsidering
T
0

EDIT: After reading OP's comment and the except from Andrew Kennedy's paper, it appears @nicolas is correct -- F# doesn't support units of measure with fractional exponents.

Teheran answered 9/7, 2012 at 1:16 Comment(1)
'u^1/2 units is not 'u^-2. I still might want to do a general matrix class of type 'u and have some common methods spitting out units 'u^1/2, yet no constraint my type to have 'u = 'v^2 for some 'vConsidering
S
-3

Shouldn't the answer be as easy as saying, yes, hertz are measured in s^-2 which is the same as s^(1/2)? There ya' go. Also, I like the philosophical idea of using , say m^(1/2) if it came up in calculations and perhaps one day understanding what that unit means in the literal sense.

Savoirvivre answered 18/11, 2016 at 4:44 Comment(1)
There is a mistake there - s^-2=1/(s*s) is not the same as s^(1/2)=sqrt(s)Citreous

© 2022 - 2024 — McMap. All rights reserved.