Match exact `axes`, something more strict than broadcasting or `map!`

Is there an operation that is strict in the sense that the destination has to exactly equal the axes of the source?

using OffsetArrays
s = 1
a5_10 = fill(0, 10:15)
a5_0 = fill(1, 0:5)
a4_0 = fill(2, 0:4)

a5_10 .= s # allowed, which I don't want
map!(identity, a5_10, a5_0) # allowed, which I don't want
map!(identity, a5_0, a4_0) # allowed, which I don't want
# e.g. something more like
map(+, a5_0, a4_0)  # not allowed, which I want
map(+, a5_0, a5_10)  # not allowed, which I want

I am not sure what _s is, but if you meant s, why not allow this? It is part of the broadcasting semantics.

I am confused why you don’t want to allow this; the axes match, it’s the same array. Sorry if I missed something about the spec.

In any case, you can always

@assert axes(a) == axes(b)

in your code — it will be either cheap, or be a no-op. Or write something like

function strict_map!(f, a1, a_rest...)
    axes_a1 = axes(a1)
    for a in a_rest
        @assert axes(a) == axes_a1
    end
    map!(f, a1, a_rest...)
end

Oof, sorry. I made a few mistakes in the example. I fixed it now.

Your strict_map! is exactly what I mean (well maybe with a DimensionMismatch instead of AssertionError), and my question is about whether there already exists an operation like that. In my own I already wrote a strict_map! (same name even).

1 Like