Slide 1

Slide 1 text

Arrays in R Davis Vaughan https://speakerdeck.com/davisvaughan

Slide 2

Slide 2 text

x matrix(1:3) y matrix(4:6) x [,1] [1,] 1 [2,] 2 [3,] 3 y [,1] [1,] 4 [2,] 5 [3,] 6 x + y [,1] [1,] 5 [2,] 7 [3,] 9 Arithmetic with matrices 2 / 13

Slide 3

Slide 3 text

x matrix(1:3) y matrix(4:6) x [,1] [1,] 1 [2,] 2 [3,] 3 y [,1] [1,] 4 [2,] 5 [3,] 6 x + y [,1] [1,] 5 [2,] 7 [3,] 9 Arithmetic with matrices Well duh 2 / 13

Slide 4

Slide 4 text

# 2 is implicitely extended to 3 1 dimension x + 2 [,1] [1,] 3 [2,] 4 [3,] 5 matrix(2, nrow = 3) [,1] [1,] 2 [2,] 2 [3,] 2 x + matrix(2, nrow = 3) [,1] [1,] 3 [2,] 4 [3,] 5 Arithmetic with scalars 3 / 13

Slide 5

Slide 5 text

x [,1] [1,] 1 [2,] 2 [3,] 3 two matrix(2) two [,1] [1,] 2 # What do you expect? x + two Arithmetic with scalars 4 / 13

Slide 6

Slide 6 text

x [,1] [1,] 1 [2,] 2 [3,] 3 two matrix(2) two [,1] [1,] 2 # What do you expect? x + two Error in x + two : non conformable arrays Arithmetic with scalars 4 / 13

Slide 7

Slide 7 text

x [,1] [1,] 1 [2,] 2 [3,] 3 two matrix(2) two [,1] [1,] 2 # What do you expect? x + two Error in x + two : non conformable arrays Arithmetic with scalars 4 / 13

Slide 8

Slide 8 text

z matrix(1:6, ncol = 2) z [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 x [,1] [1,] 1 [2,] 2 [3,] 3 x + z Arithmetic with matrices (2) 5 / 13

Slide 9

Slide 9 text

z matrix(1:6, ncol = 2) z [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 x [,1] [1,] 1 [2,] 2 [3,] 3 x + z Error in x + z: non conformable arrays Arithmetic with matrices (2) 5 / 13

Slide 10

Slide 10 text

Proposal These are valid and intuitive operations. There is a name for this. Broadcasting. Tried and true from the Python world (numpy) R can benefit from this! More intuitive syntax Can be more memory efficient Can be faster 6 / 13

Slide 11

Slide 11 text

Why does this work? Dimensions of 2 are broadcasted automatically. x + 2 [,1] [1,] 3 [2,] 4 [3,] 5 Broadcasting 7 / 13

Slide 12

Slide 12 text

Why does this work? Dimensions of 2 are broadcasted automatically. 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

Slide 13

Slide 13 text

Why does this work? Dimensions of 2 are broadcasted automatically. 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

Slide 14

Slide 14 text

Why does this work? Dimensions of 2 are broadcasted automatically. 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 first row should be repeated 3 times). Broadcasting 7 / 13

Slide 15

Slide 15 text

Three rules of broadcasting 1) Dimensions of size 1 are 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

Slide 16

Slide 16 text

Three rules of broadcasting 1) Dimensions of size 1 are 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

Slide 17

Slide 17 text

Three rules of broadcasting 1) Dimensions of size 1 are 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

Slide 18

Slide 18 text

library(rray) x_rray as_rray(x) x_rray [,1][3]> [,1] [1,] 1 [2,] 2 [3,] 3 x_rray + matrix(2) [,1][3]> [,1] [1,] 3 [2,] 4 [3,] 5 z [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 x_rray + z [,2][6]> [,1] [,2] [1,] 2 5 [2,] 4 7 [3,] 6 9 rray 9 / 13

Slide 19

Slide 19 text

x_rray [,1][3]> [,1] [1,] 1 [2,] 2 [3,] 3 t(x_rray) [,3][3]> [,1] [,2] [,3] [1,] 1 2 3 What happens if these are added together? Why is this powerful? 10 / 13

Slide 20

Slide 20 text

x_rray [,1][3]> [,1] [1,] 1 [2,] 2 [3,] 3 t(x_rray) [,3][3]> [,1] [,2] [,3] [1,] 1 2 3 What happens if these are added together? x_rray + t(x_rray) [,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

Slide 21

Slide 21 text

arr array(1:8, c(1, 4, 2)) rray as_rray(arr) rray [,4,2][8]> , , 1 [,1] [,2] [,3] [,4] [1,] 1 2 3 4 , , 2 [,1] [,2] [,3] [,4] [1,] 5 6 7 8 # choke arr + matrix(1:2) Error in arr + matrix(1 2): non conformable arrays Moar dimensions 11 / 13

Slide 22

Slide 22 text

arr array(1:8, c(1, 4, 2)) rray as_rray(arr) rray [,4,2][8]> , , 1 [,1] [,2] [,3] [,4] [1,] 1 2 3 4 , , 2 [,1] [,2] [,3] [,4] [1,] 5 6 7 8 # choke arr + matrix(1:2) Error in arr + matrix(1 2): non conformable arrays rray + matrix(1:2) [,4,2][16]> , , 1 [,1] [,2] [,3] [,4] [1,] 2 3 4 5 [2,] 3 4 5 6 , , 2 [,1] [,2] [,3] [,4] [1,] 6 7 8 9 [2,] 7 8 9 10 (1, 4, 2) (2, 1, ) --------- (2, 4, 2) Moar dimensions 11 / 13

Slide 23

Slide 23 text

common_dim rray_dim_common(rray, matrix(1:2)) common_dim [1] 2 4 2 rray_broadcast(matrix(1:2), common_dim) , , 1 [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 , , 2 [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 Find common dimension Broadcast individually to the common dim Add Moar dimensions 12 / 13

Slide 24

Slide 24 text

common_dim rray_dim_common(rray, matrix(1:2)) common_dim [1] 2 4 2 rray_broadcast(matrix(1:2), common_dim) , , 1 [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 , , 2 [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 Find common dimension Broadcast individually to the common dim Add rray_broadcast(rray, common_dim) [,4,2][16]> , , 1 [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 1 2 3 4 , , 2 [,1] [,2] [,3] [,4] [1,] 5 6 7 8 [2,] 5 6 7 8 Moar dimensions 12 / 13

Slide 25

Slide 25 text

Thank you! 13 / 13