-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quantities should be <: Real? #680
Comments
I'm sympathetic to this proposal, I also think |
It goes both ways:
|
The alternative, of course, is to weaken the type-constraints on the functions you want to call. I don't really understand why StatsBase has so many type-constraints. |
Both methods I listed (from Base and StatsBase) are fundamentally right in their restriction to Reals: it's not clear what |
But we have lots of things that are duck-typed, why is that wrong? Often there is no decent way of specifying what types of arguments this should be expected to work on. |
Maybe the same behavior and reliability is possible without |
Was discussing this recently too here. Here's my very subjective take. Is it semantically correct of a physical quantity as a real number? One could indeed argue that 1.0 meters is a type of number... But is it a real? Let me give an example. Does the statement: feel semantically correct? This is sort of what To me it feels more natural to instead state: for some abstract space All of this is to say that it's probably best to keep it Otherwise it is a bit like creating a complex number type that is |
This may be true from the mathematical purity PoV, but pragmatically in Julia:
|
One other option is to basically have Unitful do what DynamicQuantities does with respect to the type hierarchy to have pseudo multiple-inheritance. After SymbolicML/DynamicQuantities.jl#85 merges, you will be able to have quantities which are either So the default is the (imo, safer) This works by defining all quantity methods on However it's a lot refactoring, and a lot of disambiguating methods. And start time seems to takes a hit (haven’t figured out how to “heal” method invalidations coming from within a It’s not so much the mathematical purity directly, it’s more the indirect effects that matters. If people are used to thinking of |
In my opinion, these examples are different from quantities and it makes more sense that they are
Dimensionful quantities on the other hand are not a part of the real number line and some things that you can do with real numbers are not possible with quantities: What should If we make |
That's one specific property - choose another and unitful numbers start looking closer to reals than those other types.
It's pretty hard to further reduce the number of properties given the wide range of types that are already Real :) Anyway, I see there's no consensus on unitful numbers being Real - unfortunately. |
What other properties do you have in mind here? I’m genuinely curious.
Do you have some examples for those types as well? I think DualNumbers and Infinities don’t really reduce the properties of real numbers that much. I can’t think of an operation that I can do with real numbers but not with dual numbers or infinity. |
In my opinion, if we want to make For example, we currently allow Footnotes
|
Unitful quantities can be subtracted and compared in a consistent way, unlike some of other
Oh where do I start... Even looking only at those that directly subtype julia> using Infinities
julia> RealInfinity() - RealInfinity()
ERROR: ArgumentError
julia> using ForwardDiff
julia> a = 0
julia> b = ForwardDiff.Dual(0, 1)
julia> a == b
true
julia> isinteger(b)
true
julia> Int(a)
0
# but:
julia> Int(b)
ERROR: InexactError
julia> using LogarithmicNumbers
julia> ULogarithmic(2) - ULogarithmic(3)
ERROR: DomainError
julia> using IntervalArithmetic
julia> a = 1..2
julia> b = 1..3
julia> isless(a, b)
false
julia> isless(b, a)
false
julia> isequal(a, b)
false And importantly, there is no fundamental difference between
Same here: this is also an argument against subtyping |
I'm going to chime in with a different perspective: I don't think The reason I'm putting this perspective forward is because of the way that arrays of unitful quantities are handled. Although the manual shows that you can construct arrays with elements of mixed dimension it's not some thing that you'd want to do in practice because the array elements are multiple types, and performance suffers. The design of the type system should discourage users from doing this, and this can be accomplished by allowing If I'm missing some important reason why this would be a bad idea, I'd love to know. |
I can imagine usecases when non-numeric quantities can be needed. And there's nothing wrong to have a type for that! Although, I don't see how Anyway, my original point was only about numeric quantities (the only kind supported now). To handle numbers, it's typically more straightforward and composable to have
And this would also work without any special handling – if |
I forgot to respond here, but now that I remembered:
I think the solution is not a
Correct, that would return a There is an answer: I would read There is a critical semantic distinction between the two scenarios that the package currently does not allow users to express: sometimes you work with arrays of separate quantities, and sometimes the array itself is a quantity. My proposal would allow that.
I've published a package, CliffordNumbers.jl providing a multivector (in package, referred to as |
Currently, Unitful quantities are <: Number but not <: Real. This hinders composability with other libraries.
Just a few examples for methods that accept Reals and don't work with units - but actually should and totally make sense:
Base.searchsorted(::AbstractRange{<:Real}, ::Real)
does division instead of performing binary search - so, it's faster than AbstractArray fallbackStatsBase.weights
requireAbstractVector{<:Real}
Surely there are much more.
Note that these methods are correctly restricted to reals: they don't make sense for complex numbers.
Why not make Quantity <: Real, and use Complex{Quantity} for complex unitful values?
The text was updated successfully, but these errors were encountered: