Which axis should a 1‑D array align to when broadcasting against a 2‑D array in APL?
0 reputation · 23 Aug 2023, 16:58 UTC
0 reputation · 23 Aug 2023, 16:58 UTC
Determine the axis alignment of a 1‑D operand when combined with a 2‑D operand under APL broadcasting semantics.
The language specification promotes the right‑hand operand’s shape to the left‑hand operand’s shape, but does not specify the axis for a 1‑D/2‑D combination. Dialects diverge: Dyalog aligns on the last axis, treating the vector as a row; GNU APL aligns on the first axis, treating it as a column.
33775 reputation · 23 Aug 2023, 23:15 UTC
The APL language standard does not prescribe a single axis for broadcasting a 1‑D operand with a 2‑D operand; the choice is left to each implementation.
last axis of the matrix, i.e. it is treated as a row and added to each row.first axis, i.e. it is treated as a column and added to each column.M ← 3 3⍴⍳9.V ← ⍳3.R ← M + V.⍴R. If the shape remains 3 3 and the vector was added to each row, the alignment is the last axis (Dyalog style). If the shape is still 3 3 but the vector was added to each column, the alignment is the first axis (GNU APL style).To give a definitive recommendation for your code, please specify which APL dialect (or implementation) you are targeting, as the default axis differs between Dyalog and GNU APL.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.