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

support conversion to AbstractArray #747

Merged
merged 4 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
@inline convert(::Type{SA}, sa::SA) where {SA<:StaticArray} = sa
@inline convert(::Type{SA}, x::Tuple) where {SA<:StaticArray} = SA(x) # convert -> constructor. Hopefully no loops...

# support conversion to AbstractArray
AbstractArray{T}(sa::StaticArray{S,T}) where {S,T} = sa
AbstractArray{T,N}(sa::StaticArray{S,T,N}) where {S,T,N} = sa
AbstractArray{T}(sa::StaticArray{S,U}) where {S,T,U} = similar_type(typeof(sa),T,Size(sa))(sa)
AbstractArray{T,N}(sa::StaticArray{S,U,N}) where {S,T,U,N} = similar_type(typeof(sa),T,Size(sa))(sa)

# Constructing a Tuple from a StaticArray
@inline Tuple(a::StaticArray) = unroll_tuple(a, Length(a))

Expand Down
50 changes: 49 additions & 1 deletion test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,55 @@ using StaticArrays, Test, LinearAlgebra
m = MVector(1, 2, 3)
@test @inferred(reverse(m))::typeof(m) == MVector(3, 2, 1)
end

@testset "Conversion to AbstractArray" begin
# Issue #746
# conversion to AbstractArray changes the eltype from Int to Float64
sv = SVector(1,2)
@test @inferred(convert(AbstractArray{Float64}, sv)) isa SVector{2,Float64}
@test @inferred(convert(AbstractVector{Float64}, sv)) isa SVector{2,Float64}
@test convert(AbstractArray{Float64}, sv) == sv
@test convert(AbstractArray{Int}, sv) === sv
sm = SMatrix{2,2}(1,2,3,4)
@test @inferred(convert(AbstractArray{Float64,2}, sm)) isa SMatrix{2,2,Float64}
@test convert(AbstractArray{Float64,2}, sm) == sm
@test convert(AbstractArray{Int,2}, sm) === sm
mv = MVector(1, 2, 3)
@test @inferred(convert(AbstractArray{Float64}, mv)) isa MVector{3,Float64}
@test @inferred(convert(AbstractVector{Float64}, mv)) isa MVector{3,Float64}
@test convert(AbstractArray{Float64}, mv) == mv
@test convert(AbstractArray{Int}, mv) === mv
mm = MMatrix{2, 2}(1, 2, 3, 4)
@test @inferred(convert(AbstractArray{Float64,2}, mm)) isa MMatrix{2,2,Float64}
@test convert(AbstractArray{Float64,2}, mm) == mm
@test convert(AbstractArray{Int,2}, mm) === mm

# Test some of the types in StaticMatrixLike
sym = Symmetric(SA[1 2; 2 3])
@test @inferred(convert(AbstractArray{Float64}, sym)) isa Symmetric{Float64,SMatrix{2,2,Float64,4}}
@test @inferred(convert(AbstractArray{Float64,2}, sym)) isa Symmetric{Float64,SMatrix{2,2,Float64,4}}
@test convert(AbstractArray{Float64}, sym) == sym
her = Hermitian(SA[1 2+im; 2-im 3])
@test @inferred(convert(AbstractArray{ComplexF64}, her)) isa Hermitian{ComplexF64,SMatrix{2,2,ComplexF64,4}}
@test convert(AbstractArray{ComplexF64}, her) == her
diag = Diagonal(SVector(1,2))
@test @inferred(convert(AbstractArray{Float64}, diag)) isa Diagonal{Float64,SVector{2,Float64}}
@test convert(AbstractArray{Float64}, diag) == diag
# The following cases currently convert the SMatrix into an MMatrix, because
# the constructor in Base invokes `similar`, rather than `convert`, on the static array
# trans = Transpose(SVector(1,2))
# @test @inferred(convert(AbstractArray{Float64}, trans)) isa Transpose{Float64,SVector{2,Float64}}
# adj = Adjoint(SVector(1,2))
# @test @inferred(convert(AbstractArray{Float64}, adj)) isa Adjoint{Float64,SVector{2,Float64}}
# uptri = UpperTriangular(SA[1 2; 0 3])
# @test @inferred(convert(AbstractArray{Float64}, uptri)) isa UpperTriangular{Float64,SMatrix{2,2,Float64,4}}
# lotri = LowerTriangular(SA[1 0; 2 3])
# @test @inferred(convert(AbstractArray{Float64}, lotri)) isa LowerTriangular{Float64,SMatrix{2,2,Float64,4}}
# unituptri = UnitUpperTriangular(SA[1 2; 0 1])
# @test @inferred(convert(AbstractArray{Float64}, unituptri)) isa UnitUpperTriangular{Float64,SMatrix{2,2,Float64,4}}
# unitlotri = UnitLowerTriangular(SA[1 0; 2 1])
# @test @inferred(convert(AbstractArray{Float64}, unitlotri)) isa UnitLowerTriangular{Float64,SMatrix{2,2,Float64,4}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can we work around this by adding new constructors, eg, Transpose(m::StaticMatrix) = ...?

Either that or uncomment these and leave them as @test_broken for now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The construction of the matrices is fine, fortunately, with all these types, it is just the conversion. I think that, in order to fix it here, we would have to intercept functions like AbstractMatrix{T}(A::UpperTriangular{T,<:SArray}). That seems a bit intrusive, so I've added @test_broken for now like you suggest here. In the meantime I did file an issue with Julia (JuliaLang/julia#34995) to hopefully fix it upstream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining AbstractMatrix{T}(A::UpperTriangular{T,<:SArray}) seems somewhat ok to me — we already do these kinds of things elsewhere in the package.

Having said that, it's definitely not ideal — thanks for submitting the upstream issue.

end
end

@testset "vcat() and hcat()" begin
Expand Down Expand Up @@ -191,4 +240,3 @@ end
@test @inferred(vcat(A, B)) === SMatrix{4, 2}([Matrix(A); Matrix(B)])
end
end