JuliaArrays / JuliaArrays/StaticArrays.jl

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

Open
#744 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing issue #740 and the referenced HybridArrays.jl src/broadcast.jl implementation, then inspect Julia's broadcast-style reduction for mixed StaticArray and Array inputs. Done means compatible broadcasts preserve an SArray when its dimensions determine the result, while cases requiring dynamic dimensions continue to produce an Array.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.