Skip to content

reinterpret of Arrays of static arrays failed #501

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

Closed
Hong-Xiang opened this issue Sep 18, 2018 · 4 comments · Fixed by #619
Closed

reinterpret of Arrays of static arrays failed #501

Hong-Xiang opened this issue Sep 18, 2018 · 4 comments · Fixed by #619

Comments

@Hong-Xiang
Copy link

reinterpret Arrays of static arrays failed.

steps to reproduce:

using StaticArrays
function svectors(x::Matrix{Float64})
    @assert size(x,1) == 3
    reinterpret(SVector{3,Float64}, x, (size(x,2),))
end
m = zeros(3, 10)
svectors(m)

outputs:

ERROR: MethodError: no method matching reinterpret(::Type{SArray{Tuple{3},Float64,1,3}}, ::Array{Float64,2}, ::Tuple{Int64})
Closest candidates are:
  reinterpret(::Type{T}, ::Base.ReshapedArray, ::Tuple{Vararg{Int64,N}} where N) where T at reshapedarray.jl:191
  reinterpret(::Type{T}, ::A<:AbstractArray{S,N}) where {T, N, S, A<:AbstractArray{S,N}} at reinterpretarray.jl:14
  reinterpret(::Type{T}, ::Any) where T at essentials.jl:370
  ...
Stacktrace:
 [1] svectors(::Array{Float64,2}) at ./REPL[2]:3
 [2] top-level scope at none:0
@chethega
Copy link
Contributor

The reinterpret signature changed from 0.6 to 1.0. You can use the following:

julia> function svectormat(x::Matrix{Float64})
           @assert size(x,1) == 3
           reinterpret(SVector{3,Float64}, x)
       end
julia> function svectorvec(x::Matrix{Float64})
           @assert size(x,1) == 3
           reshape(reinterpret(SVector{3,Float64}, x), (size(x,2),))
       end
julia> svectormat(m)
1×10 reinterpret(SArray{Tuple{3},Float64,1,3}, ::Array{Float64,2}):
 [0.0, 0.0, 0.0]  [0.0, 0.0, 0.0]  …  [0.0, 0.0, 0.0]  [0.0, 0.0, 0.0]

julia> svectorvec(m)
10-element reshape(reinterpret(SArray{Tuple{3},Float64,1,3}, ::Array{Float64,2}), 10) with eltype SArray{Tuple{3},Float64,1,3}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

Also see #496 for a lengthy discussion on this topic. If you need this to be fast on 1.0 and don't mind relying on internals that will probably silently break your code in 1.1, you can also use the ccall-based unsafe variant from the PR. (there currently is a penalty for the reinterpret on every single access to the resulting object. If you don't rely on mutations and can afford the memory, then you should pull a copy of the reshape-reinterpret object).

@chethega
Copy link
Contributor

@andyferris It appears that this is a documentation bug. Do we need a docfix before merging some variant of #496?

@andyferris
Copy link
Member

Yes - perhaps clearly documenting the current behavior on Julia 1.0 would be the best first step.

@dmbates
Copy link

dmbates commented Jun 20, 2019

Bump. The old, failing, function svectors is still in the documentation as of v0.11.0

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