library("broadcast")
# basic usage ====
x <- setNames(1:10, letters[1:10])
print(x)
#> a b c d e f g h i j
#> 1 2 3 4 5 6 7 8 9 10
x %orientbc<-% 2L
print(x)
#> a b c d e f g h i j
#> [1,] 1 2 3 4 5 6 7 8 9 10
x %orientbc<-% 3L
print(x)
#> , , a
#>
#> [,1]
#> [1,] 1
#>
#> , , b
#>
#> [,1]
#> [1,] 2
#>
#> , , c
#>
#> [,1]
#> [1,] 3
#>
#> , , d
#>
#> [,1]
#> [1,] 4
#>
#> , , e
#>
#> [,1]
#> [1,] 5
#>
#> , , f
#>
#> [,1]
#> [1,] 6
#>
#> , , g
#>
#> [,1]
#> [1,] 7
#>
#> , , h
#>
#> [,1]
#> [1,] 8
#>
#> , , i
#>
#> [,1]
#> [1,] 9
#>
#> , , j
#>
#> [,1]
#> [1,] 10
x %orientbc<-% 1:2
print(x)
#> [,1]
#> a 1
#> b 2
#> c 3
#> d 4
#> e 5
#> f 6
#> g 7
#> h 8
#> i 9
#> j 10
x %orientbc<-% c(1, 3)
print(x)
#> , , 1
#>
#> [,1]
#> a 1
#> b 2
#> c 3
#> d 4
#> e 5
#> f 6
#> g 7
#> h 8
#> i 9
#> j 10
# modify an array along one specific dimension ====
x <- array(sample(0:9), c(3,3,3))
broadcaster(x) <- TRUE
print(x)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 6 9 1
#> [2,] 5 8 2
#> [3,] 4 3 0
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 4 3
#> [2,] 6 9 1
#> [3,] 5 8 2
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0 5 8
#> [2,] 7 4 3
#> [3,] 6 9 1
#>
#> broadcaster
# add 10 to second column, and 100 to third column:
v <- vector2array(c(0, 10, 100), 2, 2, TRUE)
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 6 19 101
#> [2,] 5 18 102
#> [3,] 4 13 100
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 14 103
#> [2,] 6 19 101
#> [3,] 5 18 102
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0 15 108
#> [2,] 7 14 103
#> [3,] 6 19 101
#>
#> broadcaster
# add 10 to second row, and 100 to third row:
v %orientbc<-% 1L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 6 9 1
#> [2,] 15 18 12
#> [3,] 104 103 100
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 4 3
#> [2,] 16 19 11
#> [3,] 105 108 102
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0 5 8
#> [2,] 17 14 13
#> [3,] 106 109 101
#>
#> broadcaster
# add 10 to second layer, and 100 to third layer:
v %orientbc<-% 3L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 6 9 1
#> [2,] 5 8 2
#> [3,] 4 3 0
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 17 14 13
#> [2,] 16 19 11
#> [3,] 15 18 12
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 100 105 108
#> [2,] 107 104 103
#> [3,] 106 109 101
#>
#> broadcasterreorient_vector
Change Orientation of An Oriented Vector For Broadcasting
Description
The lhs %orientbc<-% rhs operator takes an oriented vector (lhs), and changes its orientation.
The modification is done directly on lhs itself, using Rβs native semantics.
Usage
lhs %orientbc<-% rhs
Arguments
lhs
|
an atomic or recursive oriented vector. |
rhs
|
an integer vector. The first value must be the new orientation, referred to as orient. The second value, optional, must be the number of dimensions, referred to as ndim. If no second value is given, it will be set at max(ndim(lhs), rhs[1]). |
Details
In the context of broadcasting, an oriented vector x is a vector or array that satisfies the following conditions:
-
It has length
> 1and<= (2^31 - 1); -
It has no dimensions, or one dimension is
length(x)and all other dimensions are1.
The orientation of an oriented vector is the index of the dimension with size > 1.
For example, if array lhs has dimensions c(1, 10, 1), its orientation is 2L, since the second dimension has size > 1.
A vector with no dimensions has orientation 1L.
In the context of broadcasted operations, oriented vectors can be used to modify an array along a dimension.
The examples section gives several examples of this.
Value
Nothing, but changes lhs directly.