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,] 9 3 8
#> [2,] 7 6 5
#> [3,] 1 0 2
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 4 1 0
#> [2,] 9 3 8
#> [3,] 7 6 5
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 2 7 6
#> [2,] 4 1 0
#> [3,] 9 3 8
#>
#> 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,] 9 13 108
#> [2,] 7 16 105
#> [3,] 1 10 102
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 4 11 100
#> [2,] 9 13 108
#> [3,] 7 16 105
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 2 17 106
#> [2,] 4 11 100
#> [3,] 9 13 108
#>
#> broadcaster
# add 10 to second row, and 100 to third row:
v %orientbc<-% 1L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 9 3 8
#> [2,] 17 16 15
#> [3,] 101 100 102
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 4 1 0
#> [2,] 19 13 18
#> [3,] 107 106 105
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 2 7 6
#> [2,] 14 11 10
#> [3,] 109 103 108
#>
#> broadcaster
# add 10 to second layer, and 100 to third layer:
v %orientbc<-% 3L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 9 3 8
#> [2,] 7 6 5
#> [3,] 1 0 2
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 14 11 10
#> [2,] 19 13 18
#> [3,] 17 16 15
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 102 107 106
#> [2,] 104 101 100
#> [3,] 109 103 108
#>
#> 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.