-
Notifications
You must be signed in to change notification settings - Fork 24
Unitful compatibility #60
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
Conversation
- Add separate type parameter for the angle - Promote types for combinations of AbstractFloat and Integer
andyferris
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just drop eltype from Polar and friends. eltype is part of the iteration API and unlike Cartesian coordinates (in AbstractVectors) you can't iterate them.
There might be one or two places where you need some type (for zero(T)) but looking at that, that logic might need cleaning up in any case (these days I tend to use zero(x) where x is a value).
|
The |
Do you mean to say that I should add Regarding the julia> x=SVector{3}(1,0,0);
julia> CylindricalFromCartesian()(x)
ERROR: MethodError: no method matching Cylindrical(::Float64, ::Float64, ::Int64)
Closest candidates are:
Cylindrical(::T, ::T, ::T) where T at C:\Users\sebastian\.julia\packages\CoordinateTransformations\HeCSs\src\coordinatesystems.jl:84
Stacktrace:
[1] (::CylindricalFromCartesian)(::SArray{Tuple{3},Int64,1,3}) at C:\Users\sebastian\.julia\packages\CoordinateTransformations\HeCSs\src\coordinatesystems.jl:153
[2] top-level scope at none:0with this PR julia> x=SVector{3}(1,0,0);
julia> CylindricalFromCartesian()(x)
Cylindrical(r=1.0, θ=0.0 rad, z=0.0)Regarding the conversions, there is a slight discrepancy between the unitless and unitful cases for things like julia> Polar(1.0,2)
Polar(r=1.0, θ=2.0 rad)
julia> Polar(1.0u"m",2)
Polar(r=1.0 m, θ=2 rad)This is because the number type is wrapped by I was thinking that I could do something similar, but first I wanted to ask whether the logic for changing the type should be in the constructor or in the Also I'm not sure what happens when you combine Dual numbers and Unitful or other types, but if you have ideas for some tests I can include them. |
|
The constructor is the right place to deal with promotion. Note that these packages all define For function Polar(r, theta)
(r2, theta2) = promote(r, theta)
return Polar{typeof(r2), typeof(theta)}(r2, theta)
endand let people use the inner constructor to override. |
- Note, due to JuliaPhysics/Unitful.jl#332 the tests using isapprox with Unitful are marked broken
|
Thanks for the suggestion, this is much better than my previous implementation. |
|
I think |
Those tests once marked broken (#60) seem to pass now.
I did not consider
<:Numberbecause I didn't know how to do it without getting a stack overflow and also because Unitful subtypesNumberand promoting to a common type is exactly the opposite of what this PR is trying to do.I tried to cover the most common cases with the tests. Let me know what you think of this.
This closes #58