You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
| scalar with anything | allowed; the scalar is broadcast |
146
146
| identical shapes | allowed |
147
147
| 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) |
149
149
| a `1x1` matrix in comparisons and `&` / `|` | treated as a one-row matrix; mismatched shapes are rejected, and R errors here too |
150
150
| anything else — unequal lengths, even when one divides the other |**error**: R-style recycling is not supported |
151
151
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
+
152
161
The last row is the important one. In R, `1:6 + c(10, 20)` recycles the
153
162
shorter vector. quickr never does this:
154
163
@@ -234,7 +243,10 @@ As described above: operand lengths must match exactly or one operand
234
243
must be a scalar (or a vector spanning a matrix's rows). R's general
235
244
recycling rule — including the case where one length divides the other,
236
245
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.
238
250
239
251
### Zero-length operands do not recycle to zero-length results
0 commit comments