Skip to content

Commit b756bb5

Browse files
committed
Vignette: state the 1x1-matrix recycling rule by observable shape
The shapes-table row said a 1x1 matrix in arithmetic is "treated as a scalar" -- implementation language that reads as a shape change quickr never makes (scalar + 1x1 returns a 1x1 matrix, as in R). Say what the user sees instead: with a statically known length other than 1 the result is a plain vector, exactly R's (deprecated) answer; with a symbolic length the result shape would depend on the runtime value, so the compiled function guards on length 1, returns a 1x1 matrix, and errors where R would recycle (the 02-2 fix). The no-partial-recycling divergence section gets the matching sentence.
1 parent 472bfb0 commit b756bb5

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

vignettes/quickr-semantics.Rmd

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,19 @@ For elementwise operations (arithmetic, comparisons, `&`, `|`),
145145
| scalar with anything | allowed; the scalar is broadcast |
146146
| identical shapes | allowed |
147147
| vector with matrix, `length(vec) == nrow(mat)` | allowed; the vector is recycled column-wise, exactly as in R |
148-
| a `1x1` matrix in *arithmetic* | treated as a scalar (R allows this, with a deprecation warning) |
148+
| a `1x1` matrix in *arithmetic* with a vector of statically known length other than 1 | recycled; the result is a plain vector without dims, exactly as in R (which warns that this recycling is deprecated) |
149149
| a `1x1` matrix in comparisons and `&` / `|` | treated as a one-row matrix; mismatched shapes are rejected, and R errors here too |
150150
| anything else — unequal lengths, even when one divides the other | **error**: R-style recycling is not supported |
151151

152+
A `1x1` matrix meeting a vector whose length is only known at run time
153+
is the one place where R's *result shape* depends on a runtime value: R
154+
keeps the `1x1` dims when the vector has length 1 and drops them for
155+
any other length. A compiled function's result shape cannot depend on a
156+
value, so quickr treats the `1x1` as a one-row matrix there instead:
157+
the compiled function checks that the vector has length 1 and returns a
158+
`1x1` matrix — matching R exactly — and errors on a longer vector,
159+
where R would perform the deprecated recycling.
160+
152161
The last row is the important one. In R, `1:6 + c(10, 20)` recycles the
153162
shorter vector. quickr never does this:
154163

@@ -234,7 +243,10 @@ As described above: operand lengths must match exactly or one operand
234243
must be a scalar (or a vector spanning a matrix's rows). R's general
235244
recycling rule — including the case where one length divides the other,
236245
which R accepts without a warning — is a compile-time error or a runtime
237-
error instead.
246+
error instead. This also covers a `1x1` matrix in arithmetic with a
247+
vector whose length is only known at run time: the compiled function
248+
requires length 1 (returning a `1x1` matrix, as R does) and errors on a
249+
longer vector, where R would recycle with a deprecation warning.
238250

239251
### Zero-length operands do not recycle to zero-length results
240252

0 commit comments

Comments
 (0)