Skip to content
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

Weird interactions with Unitful #55

Closed
danielmatz opened this issue Apr 25, 2018 · 2 comments · Fixed by #141
Closed

Weird interactions with Unitful #55

danielmatz opened this issue Apr 25, 2018 · 2 comments · Fixed by #141

Comments

@danielmatz
Copy link
Contributor

I have been trying to use Rotations and Unitful together. At first, it seems like it works well:

julia> RotX(1.0u"°")
3×3 RotX{Unitful.Quantity{Float64,Unitful.Dimensions{()},Unitful.FreeUnits{(Unitful.Unit{:Degree,Unitful.Dimensions{()}}(0, 1//1),),Unitful.Dimensions{()}}}}(1.0°):
 1.0  0.0         0.0      
 0.0  0.999848   -0.0174524
 0.0  0.0174524   0.999848

But then I found that the units propagate incorrectly:

julia> RotX(1.0u"°") * [0, 0, 1]
3-element StaticArrays.SArray{Tuple{3},Unitful.Quantity{Float64,Unitful.Dimensions{()},Unitful.FreeUnits{(Unitful.Unit{:Degree,Unitful.Dimensions{()}}(0, 1//1),),Unitful.Dimensions{()}}},1,3}:
       0.0°
 -0.999949°
   57.2871°
@andyferris
Copy link
Contributor

I guess we are assuming sin(::T) -> T here. We'd need more type-parameters to handle this (one for the AbstractArray's eltype, and one for the stored quantity).

Actually this bug is more widespread than just Unitful integration:

julia> r = RotX(1)
3×3 RotX{Int64}(1):
 1.0  0.0        0.0     
 0.0  0.540302  -0.841471
 0.0  0.841471   0.540302

julia> eltype(r)
Int64

julia> r * [1,1,1]
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{Int64}, ::Float64) at ./float.jl:679
 [2] macro expansion at /home/ferris/.julia/v0.6/StaticArrays/src/util.jl:11 [inlined]
 [3] convert_ntuple at /home/ferris/.julia/v0.6/StaticArrays/src/util.jl:8 [inlined]
 [4] Type at /home/ferris/.julia/v0.6/StaticArrays/src/SArray.jl:28 [inlined]
 [5] macro expansion at /home/ferris/.julia/v0.6/StaticArrays/src/matrix_multiply.jl:69 [inlined]
 [6] _mul at /home/ferris/.julia/v0.6/StaticArrays/src/matrix_multiply.jl:57 [inlined]
 [7] *(::Rotations.RotX{Int64}, ::Array{Int64,1}) at /home/ferris/.julia/v0.6/StaticArrays/src/matrix_multiply.jl:38

This is lying about an AbstractArray interface function (eltype) and is leading to bad results. :(

@OliverEvans96
Copy link
Contributor

I'm finding that it works as expected for 3D rotations

julia> rx = RotX(38°)
3×3 RotX{Float64} with indices SOneTo(3)×SOneTo(3)(0.663225):
 1.0  0.0        0.0
 0.0  0.788011  -0.615661
 0.0  0.615661   0.788011

julia> rx * [1.0, 2.0, 3.0]
3-element StaticArrays.SVector{3, Float64} with indices SOneTo(3):
  1.0
 -0.27096291876353107
  3.595355211471482

julia> rz = RotZ(38°)
3×3 RotZ{Float64} with indices SOneTo(3)×SOneTo(3)(0.663225):
 0.788011  -0.615661  0.0
 0.615661   0.788011  0.0
 0.0        0.0       1.0

julia> rz * [1.0, 2.0, 3.0]
3-element StaticArrays.SVector{3, Float64} with indices SOneTo(3):
 -0.4433121970445947
  2.191682982539102
  3.0

But appends an extraneous unit in 2D

julia> r2 = Angle2d(38°)
2×2 Angle2d{Quantity{Int64, NoDims, FreeUnits{(°,), NoDims, nothing}}} with indices SOneTo(2)×SOneTo(2)(38°):
 0.788011  -0.615661
 0.615661   0.788011

julia> r2 * [1.0, 2.0]
2-element StaticArrays.SVector{2, Quantity{Float64, NoDims, Unitful.FreeUnits{(°,), NoDims, nothing}}} with indices SOneTo(2):
 -25.399917897327196°
  125.57418493013506°

This is with:

  • Julia v1.6.0-beta1
  • Rotations v1.0.2
  • Unitful v1.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants