From 5ebe41355bad16596025ebcc0f6e9f0190794dea Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Tue, 10 May 2022 12:01:21 -0600 Subject: [PATCH 1/2] Clarify the required semantics for broadcasting with setitem Fixes #424. --- spec/API_specification/broadcasting.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/API_specification/broadcasting.rst b/spec/API_specification/broadcasting.rst index ec72fb089..8deecaf35 100644 --- a/spec/API_specification/broadcasting.rst +++ b/spec/API_specification/broadcasting.rst @@ -112,4 +112,4 @@ The following examples demonstrate array shapes which do **not** broadcast. In-place Semantics ------------------ -As implied by the broadcasting algorithm, in-place element-wise operations must not change the shape of the in-place array as a result of broadcasting. +As implied by the broadcasting algorithm, in-place element-wise operations (including ``__setitem__``) must not change the shape of the in-place array as a result of broadcasting. Such operations should only be supported in the case where the right-hand operand can broadcast to the shape of the left-hand operand, after any indexing operations are performed. From 59d81697287b2e43324d7c42877f6154f702611f Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 3 Nov 2022 15:25:17 -0600 Subject: [PATCH 2/2] Add examples of when in-place operations are allowed to broadcast --- spec/API_specification/broadcasting.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/API_specification/broadcasting.rst b/spec/API_specification/broadcasting.rst index 8deecaf35..abb3ed222 100644 --- a/spec/API_specification/broadcasting.rst +++ b/spec/API_specification/broadcasting.rst @@ -113,3 +113,16 @@ In-place Semantics ------------------ As implied by the broadcasting algorithm, in-place element-wise operations (including ``__setitem__``) must not change the shape of the in-place array as a result of broadcasting. Such operations should only be supported in the case where the right-hand operand can broadcast to the shape of the left-hand operand, after any indexing operations are performed. + +For example: + +:: + + x = empty((2, 3, 4)) + a = empty((1, 3, 4)) + + # This is OK. The shape of a, (1, 3, 4), can broadcast to the shape of x[...], (2, 3, 4) + x[...] = a + + # This is not allowed. The shape of a, (1, 3, 4), can NOT broadcast to the shape of x[1, ...], (3, 4) + x[1, ...] = a