SimonDanisch / FixedSizeArrays.jl

Julia's generic FixedSizeArray implementation. Can be used for all kinds of Vector constructs
Other
35 stars 20 forks source link

FSA broadcasting #115

Open c42f opened 8 years ago

c42f commented 8 years ago

While implementing #105 the issue of map vs broadcast for FSA operations came up. The FSA map already does some broadcast with scalars, and we should probably be extending it into a full broadcast implementation for consistency with Array. I didn't want to start on broadcast and further delay fixing a lot of bugs, so here's a new issue for it. To copy the summary from https://github.com/SimonDanisch/FixedSizeArrays.jl/pull/105#issuecomment-220810574:

Deducing the output type is the major problem with implementing broadcast if we want to allow mixing FixedArray with AbstractArray in a nice way.

For broadcast(), mixing the two must result in a normal Array, since the output size will only be known at runtime so we can't know the output FSA type. (The output size of a broadcast may also be really large in which case using an FSA would be pretty terrible anyway.)

Say you want to broadcast .+. There's only really one set of rules which makes sense:

Number .+ FSA -> FSA
FSA .+ FSA -> FSA
FSA .+ AbstractArray -> AbstractArray

For map() it's natural to assume that mixing the two should result in a FixedArray, because you've already got a fixed array of the same size, and you can have the following set of rules:

Number + FSA -> FSA
FSA + FSA -> FSA
FSA + AbstractArray -> FSA

So it's a sticky situation. On the one hand, we should follow the usual broadcast semantics from AbstractArray. On the other hand, these play havoc with the ability to make FixedArray and AbstractArray interoperate efficiently, with a minimum of intrusive syntax.

SimonDanisch commented 8 years ago

I think this is a pretty satisfactory set of rules, considering the trade offs! ;)