Scalar Functions and Scalar Extension¶
Scalar functions are the workhorses of APL. They operate element-by-element on arrays of any shape, and they handle mismatched sizes gracefully through scalar extension.
Scalar functions apply element-wise¶
When you add two vectors of the same length, the function is applied to each pair of corresponding elements:
This works for all scalar functions — arithmetic, comparison, boolean, and more:
Scalar extension¶
When one argument is a scalar and the other is an array, the scalar is extended — paired with every element:
This works with arrays of any rank:
Matching shapes¶
If both arguments are arrays (not scalars), they must have the same shape. Mismatched shapes cause a LENGTH ERROR:
Monadic scalar functions¶
Most scalar functions also have a monadic form (one argument, on the right). The function is applied to each element:
| Dyadic | Monadic | Meaning |
|---|---|---|
X + Y (add) |
+ Y (conjugate/identity) |
Returns Y unchanged for real numbers |
X - Y (subtract) |
- Y (negate) |
Flips sign |
X × Y (multiply) |
× Y (signum) |
Returns ¯1, 0, or 1 |
X ÷ Y (divide) |
÷ Y (reciprocal) |
1÷Y |
X * Y (power) |
* Y (exponential) |
e*Y |
X ⍟ Y (logarithm) |
⍟ Y (natural log) |
ln Y |
X ⌈ Y (maximum) |
⌈ Y (ceiling) |
Round up |
X ⌊ Y (minimum) |
⌊ Y (floor) |
Round down |
X \| Y (residue) |
\| Y (magnitude) |
Absolute value |
Negative numbers
APL uses ¯ (high minus) for negative numbers, not -. So negative three is ¯3, not -3. The - symbol is the subtract function.
The complete list¶
MARPLE implements these scalar functions. Each applies element-wise and supports scalar extension.
Arithmetic: + − × ÷ ⌈ ⌊ * ⍟ | ! ○
Comparison: < ≤ = ≥ > ≠
Boolean: ∧ ∨ ⍲ ⍱ ~
Comparison functions return 1 (true) or 0 (false). Boolean functions operate on 0s and 1s.
See the Primitive Functions Reference for full details on each one.
Key points¶
- Scalar functions work element-by-element on arrays of any shape
- If both arguments are arrays, their shapes must match
- If one argument is a scalar, it's extended to match the other
- Most scalar functions have both monadic and dyadic forms
- Comparison functions return 0 or 1
Next: Reduce and Scan