name for this. Broadcasting. Tried and true from the Python world (numpy) R can beneﬁt from this! More intuitive syntax Can be more memory efﬁcient Can be faster 6 / 13
x + 2 [,1] [1,] 3 [2,] 4 [3,] 5 "Common" dimensions (3, 1) 3 rows, 1 cols (1, ) 1 rows, no cols ------ (3, 1) The 2 is broadcasted from (1, ) to (3, 1) so that it can be added to x using standard R addition rules (matching dimension). Broadcasting 7 / 13
x + 2 [,1] [1,] 3 [2,] 4 [3,] 5 "Common" dimensions (3, 1) 3 rows, 1 cols (1, ) 1 rows, no cols ------ (3, 1) The 2 is broadcasted from (1, ) to (3, 1) so that it can be added to x using standard R addition rules (matching dimension). x + matrix(c(2, 2, 2)) [,1] [1,] 3 [2,] 4 [3,] 5 Broadcasting 7 / 13
x + 2 [,1] [1,] 3 [2,] 4 [3,] 5 "Common" dimensions (3, 1) 3 rows, 1 cols (1, ) 1 rows, no cols ------ (3, 1) The 2 is broadcasted from (1, ) to (3, 1) so that it can be added to x using standard R addition rules (matching dimension). x + matrix(c(2, 2, 2)) [,1] [1,] 3 [2,] 4 [3,] 5 So, in theory, this should work: x + matrix(2) Error in x + matrix(2): non conformable arrays (3, 1) 3 rows, 1 cols (1, 1) 1 rows, 1 cols ------ (3, 1) The dimensions of (1, 1) should be broadcasted up to (3, 1) (concretely, the ﬁrst row should be repeated 3 times). Broadcasting 7 / 13
always recycled up to the other dimension you are comparing to. 2) Implicit dimensions are always appended to the right hand side, and are assumed to be 1’s. 3) If two dimensions do not match, and neither are 1, an error is thrown. 8 / 13
always recycled up to the other dimension you are comparing to. 2) Implicit dimensions are always appended to the right hand side, and are assumed to be 1’s. 3) If two dimensions do not match, and neither are 1, an error is thrown. These rules explain: (3, 1) (1, ) ------ (3, 1) 3 row VS 1 row = Rule 1 = The 1 row object is broadcasted up to 3. 1 col VS no col = Rule 2 = Implicit dimension of 1, matches the other 1. 8 / 13
always recycled up to the other dimension you are comparing to. 2) Implicit dimensions are always appended to the right hand side, and are assumed to be 1’s. 3) If two dimensions do not match, and neither are 1, an error is thrown. These rules explain: (3, 1) (1, ) ------ (3, 1) 3 row VS 1 row = Rule 1 = The 1 row object is broadcasted up to 3. 1 col VS no col = Rule 2 = Implicit dimension of 1, matches the other 1. rray is a new package that enforces this set of rules, and uses the xtensor C++ library for the underlying operations. 8 / 13
<vctrs_rray<integer>[,3][3]> [,1] [,2] [,3] [1,] 1 2 3 What happens if these are added together? x_rray + t(x_rray) <vctrs_rray<integer>[,3][9]> [,1] [,2] [,3] [1,] 2 3 4 [2,] 3 4 5 [3,] 4 5 6 (3, 1) (1, 3) ------ (3, 3) 3 row VS 1 row = Broadcast 1 up to 3 1 col VS 3 col = Broadcast 1 up to 3 Why is this powerful? 10 / 13