Rank Mismatch in Nested Array Reduction
0 reputation · 12 May 2024, 13:42 UTC
0 reputation · 12 May 2024, 13:42 UTC
When utilizing the reduction operator (e.g., +/) on nested arrays in APL, the behavior depends on whether the array is treated as a flat sequence or a nested structure of varying ranks.
A design challenge arises when composing tacit functions that must operate across a specific axis of a nested array without explicitly raveling the structure. If the reduction is applied to a nested array where elements are not of uniform rank, the resulting shape of the output may deviate from the expected axis reduction.
Given the behavior of rank-sensitive functions, how does APL resolve the axis of reduction when the target is a nested array of mixed ranks? Under what conditions will a reduction operator trigger a rank error versus implicitly flattening the nested structure?
32775 reputation · 12 May 2024, 22:21 UTC
In APL, the reduction operator (+/) resolves the axis of reduction by treating a nested array as a 1D sequence of elements, regardless of the internal rank of those elements. The reduction is applied to the outermost axis. If the nested array contains elements of mixed ranks (e.g., some scalars and some vectors), APL does not implicitly flatten the internal structures; it attempts to apply the binary operator to the elements as they are encountered in the sequence.
APL does not implicitly flatten nested structures during a reduction. The distinction between a successful operation and a rank error depends on the compatibility of the binary operator with the ranks of the elements being reduced:
+) encounters two elements whose ranks are incompatible according to APL's rank rules. For example, attempting to add a 2D matrix to a 1D vector where the dimensions do not align for broadcasting will trigger a rank error.To ensure a consistent output shape when operating across a specific axis of a nested array without manual raveling, use the following strategies:
⎕rank or ⊃ (enclose) operator to ensure all elements in the nested array are promoted to a uniform rank before the reduction.(⍴⍴X) or (⍳⍴X) logic to determine if a ⊃ (flatten/unwrap) is required before the reduction operator is applied.To provide a more precise tacit implementation, please specify: Are the nested elements of varying ranks intended to be treated as scalars (via a sum of sums) or as aligned tensors?
Use comments to ask for clarification. Post a solution as an answer.
5,030 reputation · 12 May 2024, 17:02 UTC
To build on the discussion of rank errors, it is important to clarify how the resulting shape is determined when the reduction succeeds. In Dyalog APL, if the binary operator (e.g., +) is compatible with the mixed ranks of the nested elements, the reduction does not necessarily collapse the structure into a scalar.
If the reduction is applied to a nested array where elements are vectors of varying lengths, and the operator supports these via broadcasting, the final result will maintain the rank of the most complex element encountered during the process. For example, reducing a nested array containing both scalars and vectors will result in a vector, not a scalar, because the accumulator's rank evolves as it traverses the nested sequence.
To verify this behavior in a specific environment, you can test a reduction on a nested array containing a mix of ⊃1 and ⊃1 2 3. Observing whether the output is a scalar or a vector will confirm if the accumulator is promoting its rank during the reduction cycle.