Skip to content

Commit fa17430

Browse files
authored
Merge pull request #941 from thchr/float-real-satype
implement `float` and `real` for `StaticArray` (fix #935)
2 parents 559134c + f81e316 commit fa17430

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/StaticArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Statistics: mean
1313
using Random
1414
import Random: rand, randn, randexp, rand!, randn!, randexp!
1515
using Core.Compiler: return_type
16-
import Base: sqrt, exp, log
16+
import Base: sqrt, exp, log, float, real
1717
using LinearAlgebra
1818
import LinearAlgebra: transpose, adjoint, dot, eigvals, eigen, lyap, tr,
1919
kron, diag, norm, dot, diagm, lu, svd, svdvals,

src/convert.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ length_val(a::Type{T}) where {T<:StaticArrayLike} = length_val(Size(T))
4747
@inbounds return $(Expr(:tuple, exprs...))
4848
end
4949
end
50+
51+
# `float` and `real` of StaticArray types, analogously to application to scalars (issue 935)
52+
float(::Type{SA}) where SA<:StaticArray{_S,T,_N} where {_S,T,_N} = similar_type(SA, float(T))
53+
real(::Type{SA}) where SA<:StaticArray{_S,T,_N} where {_S,T,_N} = similar_type(SA, real(T))

test/convert.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,26 @@ end # testset
1919
@test Scalar{Int}[SVector{1,Int}(3), SVector{1,Float64}(2.0)] == [Scalar{Int}(3), Scalar{Int}(2)]
2020
@test Scalar[SVector{1,Int}(3), SVector{1,Float64}(2.0)] == [Scalar{Int}(3), Scalar{Float64}(2.0)]
2121
end
22+
23+
@testset "`real` and `float` of SArray/MArray" begin
24+
# Issue 935
25+
for SAT in (SArray, MArray)
26+
vInt = SAT(SA[1,2,3]) # S/MVector{3, Int}
27+
@test real(typeof(vInt)) === typeof(vInt)
28+
@test float(typeof(vInt)) === typeof(float.(vInt))
29+
30+
vCInt = vInt + 1im*vInt # S/MVector{3, Complex{Int}}
31+
@test real(typeof(vCInt)) === typeof(vInt)
32+
@test float(typeof(vCInt)) === typeof(float.(vCInt))
33+
34+
vvInt = SAT(SA[vInt, vInt]) # S/MVector{2, S/MVector{3, Int}}
35+
@test real(typeof(vvInt)) === SAT{Tuple{2}, SAT{Tuple{3}, Int, 1, 3}, 1, 2}
36+
@test float(typeof(vvInt)) === SAT{Tuple{2}, SAT{Tuple{3}, Float64, 1, 3}, 1, 2}
37+
38+
vvCInt = SAT(SA[vCInt, vCInt]) # S/MVector{2, S/MVector{3, Complex{Int}}}
39+
@test real(typeof(vvCInt)) === SAT{Tuple{2}, SAT{Tuple{3}, Int, 1, 3}, 1, 2}
40+
@test float(typeof(vvCInt)) === SAT{Tuple{2}, SAT{Tuple{3}, Complex{Float64}, 1, 3}, 1, 2}
41+
end
42+
mInt = SA[Int16(1) Int16(2) Int16(3); Int16(4) Int16(5) Int16(6)] # SMatrix{3,2,Int16}
43+
@test float(typeof(mInt)) === SMatrix{2, 3, float(Int16), 6}
44+
end

0 commit comments

Comments
 (0)