StaticArray broadcast error

broadcast

#1

Hello,
On julia 0.5.0, Win64, StaticArrays 0.2.1:
I have 2 types defined as

using StaticArrays
immutable Node <: FieldVector{Float64}
    x::Float64
    y::Float64
    z::Float64
end
immutable Point3 <: FieldVector{Float64}
    x::Float64
    y::Float64
    z::Float64
end

and I’d like to do a vectorized math operation

p = Point3(1.0, 2.0, 3.0); n=Node(2.0, 4.0, 6.0)
d = n.-p
MethodError: no method matching similar_type(::Type{CST_Fields.Node}, ::Type{Float64})
Closest candidates are:
  similar_type{SA<:StaticArrays.StaticArray{T,N},N,T}(::Union{SA<:StaticArrays.StaticArray{T,N},Type{SA<:StaticArrays.StaticArray}}, ::Type{T}, !Matched::Tuple{Vararg{Int64,N}}) at c:\Users\phlavenk\Documents\programs\juliadata\v0.5\StaticArrays\src\abstractarray.jl:58
...
 in macro expansion at mapreduce.jl:24 [inlined]
 in map at mapreduce.jl:17 [inlined]
 in macro expansion at mapreduce.jl:246 [inlined]
 in broadcast at mapreduce.jl:243 [inlined]
 in ./(::CST_Fields.Node, ::CST_Fields.Point3) at arraymath.jl:13
...

Does anyone know, what to do for my types to behave well in the broadcast machinery?


#2

That’s not one I’ve seen before.

Just this week I’ve been working on cleaning up similar_type - it’s been completely overhauled. Could you please try to checkout the latest prerelease version by typing Pkg.checkout("StaticArrays", "ajf/nopure")? And let me know if the problem is fixed.

(You can revert to the standard version of StaticArrays with Pkg.free("StaticArrays"))


#3

PS - in general you can control the output type of operations via creating your own definitions for the similar_type function. There are brief instructions included with the new branch. Let me know if you need some more specifics.


#4

OK, thank you for help. Hopefuly I understand.
So in my case it is not clear what should be the result type of the math operation. Now, I can get the desired behavior by my own definition

similar_type{SA <: Union{Point3, Vec3, Node}, ElType<:AbstractFloat}(::Type{SA}, s::Type{ElType}) = SVector{Size(SA), ElType)

I think there should be a fallback function that errors with better error message telling what to do.
Because there soon will be huge amount of people relying on this great package.
Thank you.
Petr


#5

Hopefully the improvements in the above-mentioned branch will be available via Pkg.update() sometime next week.