Scalar extension broadcasting and prototype type conformity
0 reputation · 17 Jul 2021, 21:19 UTC
0 reputation · 17 Jul 2021, 21:19 UTC
The precise goal is to determine how APL scalar extension broadcasting governs operand shape conformance when the scalar and array operands carry incompatible numeric prototypes. In dyadic operations such as scalar + vector, the language specification mandates that the singleton scalar be replicated along each axis to match the vector's length, but the treatment of prototype type promotion remains implementation-specific. This behavior directly affects portability across APL systems and the correctness of numeric code that relies on automatic type coercion.
A key constraint involves the mismatch between a scalar's prototype (e.g., 64-bit integer) and the array operand's prototype (e.g., 64-bit floating-point). Dyalog APL since version 13.0 applies scalar extension rules uniformly, yet may raise a DOMAIN ERROR if the prototypes are fundamentally incompatible. GNU APL and other interpreters may employ different type promotion strategies, and the interaction with nested array rank elevation is not uniformly documented. These variations introduce uncertainty when reasoning about cross-platform APL expressions.
Does scalar extension preserve the scalar's original prototype when broadcasting to a vector of a different numeric type? How do Dyalog APL 18.0 and GNU APL differ in their handling of prototype mismatch during dyadic operations? What are the rank and shape consequences when the scalar operand is a nested array rather than a simple numeric?
34775 reputation · 18 Jul 2021, 01:37 UTC
In dyadic APL operations, scalar extension does not preserve the scalar's original prototype if it differs from the array operand. Instead, the language applies type promotion rules to ensure both operands are compatible before the operation is executed. The scalar is effectively coerced to the promoted type, and the resulting array adopts this promoted prototype.
While both systems follow the fundamental rule that a rank-0 scalar is replicated to match the shape of the non-scalar operand, their handling of prototype mismatches varies:
DOMAIN ERROR.When the scalar operand is a nested array rather than a simple numeric, the following consequences occur:
Note: Whether the nested structure is flattened or preserved during specific dyadic operations can be implementation-specific and should be verified per environment.
To verify prototype behavior in your specific environment, execute the following scoped tests:
⍝ Test 1: Numeric Promotion (Integer Scalar + Float Vector)
res ← 1 + 1.1 2.1 3.1
⍝ Inspect result type to see if 1 was promoted to float
⍝ Test 2: Nested Scalar Extension
nested_scalar ← ⊂ 1 2 3
res_nested ← nested_scalar + 1 1 1
⍝ Verify if result is a vector of 3 nested arrays
Missing Diagnostic: To provide a more precise promotion mapping, please specify if you are using a specific Dyalog configuration (e.g., #C or #S) as this can influence how numeric types are handled.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.