Skip to content

Commit

Permalink
Fix Base.one(::Angle2d) method and etc. (#195)
Browse files Browse the repository at this point in the history
* Base.one(r::Rotation) = one(typeof(r))

* update tests for one(::Rotation)
  • Loading branch information
hyrodium authored Nov 16, 2021
1 parent aad3188 commit f13326d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Base.zeros(::Type{R}, dims::Base.DimOrInd...) where {R<:Rotation} = zeros(typeof
Base.zeros(::Type{R}, dims::NTuple{N, Integer}) where {R<:Rotation, N} = zeros(typeof(zero(R)),dims)
Base.zeros(::Type{R}, dims::Tuple{}) where {R<:Rotation} = zeros(typeof(zero(R)),dims) # avoid ambiguity

# Generate identity rotation matrix
Base.one(r::Rotation) = one(typeof(r))

# Rotation angles and axes can be obtained by converting to the AngleAxis type
rotation_angle(r::Rotation{3}) = rotation_angle(AngleAxis(r))
rotation_axis(r::Rotation{3}) = rotation_axis(AngleAxis(r))
Expand Down
2 changes: 2 additions & 0 deletions test/2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ using Unitful
@test @inferred(size(R)) == (2,2)
@test @inferred(size(R{Float32})) == (2,2)
@test one(R)::R == I
@test one(one(R))::R == I
@test one(R{Float32})::R{Float32} == I32
@test one(one(R{Float32}))::R{Float32} == I32
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/rotation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ all_types = (RotMatrix{3}, AngleAxis, RotationVec,
@testset "$(R)" for R in all_types
# one(R) should always return something of type R (#114)
@test one(R)::R == I
@test one(one(R))::R == I
@test one(R{Float32})::R{Float32} == I32
@test one(one(R{Float32}))::R{Float32} == I32
end
end

Expand Down

0 comments on commit f13326d

Please sign in to comment.