Skip to content

Pair up map arguments in memory order, like Base - #885

Merged
maleadt merged 1 commit into
mainfrom
tb/map-linear
Dec 3, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/map-linear

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

map/map! with differently shaped arguments broadcast them and truncated the result, which only agrees with Base when broadcasting happens to pair the same elements. map(+, a_1x4, b_4) added a[1,1] to all of b. map(min, a_2x2, b_2x1) returned two elements where Base throws DimensionMismatch. Mapping a matrix with a range of the same length, or into a destination of another shape, threw.

map! now pairs the n-th element of every container, up to the shortest one, so indices that don't start at 1 work too. map uses zip's shape rules to choose between Base's DimensionMismatch and a vector of the shortest length. Arguments of equal size still go through broadcast.

Fixes #882

`map` and `map!` on GPU arrays with differently shaped arguments broadcast
them and kept the first results. That only agrees with Base when broadcasting
happens to pair the same elements: `map(+, a_1x4, b_4)` added `a[1,1]` to every
element of `b`, `map(min, a_2x2, b_2x1)` returned two elements where Base
throws a `DimensionMismatch`, and mapping over a matrix and a range of the
same length, or into a destination of another shape, threw.

`map!` now indexes every container by its n-th linear index, stopping at the
shortest one, and `map` uses `zip`'s shape rules to decide between Base's
`DimensionMismatch` and a vector of the shortest length. Arguments of equal
size still go through `broadcast`.

Fixes #882
@maleadt
maleadt merged commit e0140b5 into main Dec 3, 2026
16 of 17 checks passed
@maleadt
maleadt deleted the tb/map-linear branch December 3, 2026 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

map on differently shaped inputs produces different values or dimension errors on oneArray and Array

1 participant