Skip to content

Commit ecc6595

Browse files
authored
Relax input type constraint to enable eltype promotion. (#230)
* Enable auto promote. * add test * Use `SArray{Tuple{N,N}}` to throw a `DimensionMismatch`
1 parent a12cb6c commit ecc6595

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/core_types.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@ RotMatrix(x::SMatrix{N,N,T,L}) where {N,T,L} = RotMatrix{N,T,L}(x)
7777
Base.zero(::Type{RotMatrix}) = error("The dimension of rotation is not specified.")
7878

7979
# These functions (plus size) are enough to satisfy the entire StaticArrays interface:
80-
@inline function RotMatrix(t::NTuple{L}) where L
81-
n = sqrt(L)
80+
@inline function RotMatrix(t::Tuple)
81+
n = sqrt(length(t))
8282
if !isinteger(n)
8383
throw(DimensionMismatch("The length of input tuple $(t) must be square number."))
8484
end
8585
N = Int(n)
8686
RotMatrix(SMatrix{N,N}(t))
8787
end
88-
@inline (::Type{RotMatrix{N}})(t::NTuple) where N = RotMatrix(SMatrix{N,N}(t))
89-
@inline RotMatrix{N,T}(t::NTuple) where {N,T} = RotMatrix(SMatrix{N,N,T}(t))
90-
@inline RotMatrix{N,T,L}(t::NTuple{L}) where {N,T,L} = RotMatrix(SMatrix{N,N,T}(t))
88+
@inline (::Type{RotMatrix{N}})(t::Tuple) where N = RotMatrix(SArray{Tuple{N,N}}(t))
89+
@inline RotMatrix{N,T}(t::Tuple) where {N,T} = RotMatrix(SArray{Tuple{N,N},T}(t))
90+
@inline RotMatrix{N,T,L}(t::Tuple) where {N,T,L} = RotMatrix(SArray{Tuple{N,N},T}(t))
9191

9292
# Create aliases RotMatrix2{T} = RotMatrix{2,T,4} and RotMatrix3{T} = RotMatrix{3,T,9}
9393
for N = 2:3
9494
L = N*N
9595
RotMatrixN = Symbol(:RotMatrix, N)
9696
@eval begin
9797
const $RotMatrixN{T} = RotMatrix{$N, T, $L}
98-
@inline $RotMatrixN(t::NTuple{$L}) = RotMatrix(SMatrix{$N,$N}(t))
98+
@inline $RotMatrixN(t::Tuple) = RotMatrix(SArray{Tuple{$N,$N}}(t))
9999
end
100100
end
101101

@@ -152,7 +152,7 @@ params(r::Angle2d) = SVector{1}(r.theta)
152152

153153
Angle2d(r::Rotation{2}) = Angle2d(rotation_angle(r))
154154
Angle2d{T}(r::Rotation{2}) where {T} = Angle2d{T}(rotation_angle(r))
155-
@inline (::Type{R})(t::NTuple{4}) where R<:Angle2d = convert(R, RotMatrix(t))
155+
@inline (::Type{R})(t::Tuple) where R<:Angle2d = convert(R, RotMatrix2(t))
156156

157157
Base.one(::Type{A}) where {A<: Angle2d} = A(0)
158158

test/2d.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ using Unitful
1111
@test RotMatrix((1,0,0,1)) == RotMatrix(@SMatrix [1 0;0 1]) == one(SMatrix{2,2})
1212
@test Angle2d((1,0,0,1)) == RotMatrix(@SMatrix [1 0;0 1]) == one(SMatrix{2,2})
1313
@test RotMatrix((1,0,0,1)) isa RotMatrix2{Int}
14+
@test RotMatrix((1.,0,0,1)) isa RotMatrix2{Float64}
15+
@test RotMatrix2((1f0,0,0,1)) isa RotMatrix2{Float32}
1416
@test Angle2d((1,0,0,1)) isa Angle2d{Float64}
17+
@test Angle2d((1f0,0,0,1)) isa Angle2d{Float32}
1518
@test RotMatrix{2,Float32}((1,0,0,1)) isa RotMatrix2{Float32}
1619
@test Angle2d{Float32}((1,0,0,1)) isa Angle2d{Float32}
1720
@test_throws DimensionMismatch RotMatrix((1,0,0,1,0))

0 commit comments

Comments
 (0)