Skip to content

Broadcast of SArray with Array gives Array but we'd like SArray #744

Open
@c42f

Description

@c42f

As noted in #740, we currently have the following annoyance that broadcast with static and normal arrays doesn't result in static arrays:

julia> SA[1,2,3] .+ [1,2,3]
3-element Array{Int64,1}:
 2
 4
 6

I think this can be fixed in principle but the big difficulty with broadcasting is that the output SArray Size may depend on the dynamic size of the Array, for example we cannot infer the size of the second dimension in

julia> SA[1,2,3] .+ [1 2 3]
3×3 Array{Int64,2}:
 2  3  4
 3  4  5
 4  5  6

However, there's many circumstances in which we could make this work: basically any time the StaticArray has all dimensions with Size greater than than 1, broadcast semantics require that the Array which is being broadcast together with it match those dimensions (provided the Array as number of dimensions less or equal to the StaticArray).

I'm not sure how to fix this, but one thought was to try stashing the Size trait within the BroadcastStyle and use that to infer the output shape as part of the recursive reduction of BroadCastStyle(a,b).

@mateuszbaran's HybridArrays.jl may also have some code to deal with this kind of thing (https://github.com/mateuszbaran/HybridArrays.jl/blob/master/src/broadcast.jl)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurefeatures and feature requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions