Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Arrays in R

Davis Vaughan
November 27, 2018
150

Arrays in R

Davis Vaughan

November 27, 2018
Tweet

Transcript

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

    View Slide

  2. 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

    View Slide

  3. 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

    View Slide

  4. # 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

    View Slide

  5. 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

    View Slide

  6. 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

    View Slide

  7. 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

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

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

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. 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

    View Slide

  20. 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

    View Slide

  21. 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

    View Slide

  22. 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

    View Slide

  23. 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

    View Slide

  24. 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

    View Slide

  25. Thank you!
    13 / 13

    View Slide