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
#> broadcaster
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
#>
#> broadcaster
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
#> broadcaster
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
#>
#> broadcaster
# modify an array along one specific dimension ====
x <- array(sample(0:9), c(3,3,3))
print(x)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 4 2 0
#> [2,] 9 7 1
#> [3,] 6 5 8
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 3 6 5
#> [2,] 4 2 0
#> [3,] 9 7 1
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 8 9 7
#> [2,] 3 6 5
#> [3,] 4 2 0
# 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,] 4 12 100
#> [2,] 9 17 101
#> [3,] 6 15 108
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 3 16 105
#> [2,] 4 12 100
#> [3,] 9 17 101
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 8 19 107
#> [2,] 3 16 105
#> [3,] 4 12 100
#>
#> broadcaster
# add 10 to second row, and 100 to third row:
v %orientbc<-% 1L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 4 2 0
#> [2,] 19 17 11
#> [3,] 106 105 108
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 3 6 5
#> [2,] 14 12 10
#> [3,] 109 107 101
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 8 9 7
#> [2,] 13 16 15
#> [3,] 104 102 100
#>
#> broadcaster
# add 10 to second layer, and 100 to third layer:
v %orientbc<-% 3L
x + v
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 4 2 0
#> [2,] 9 7 1
#> [3,] 6 5 8
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 13 16 15
#> [2,] 14 12 10
#> [3,] 19 17 11
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 108 109 107
#> [2,] 103 106 105
#> [3,] 104 102 100
#>
#> broadcasterreorient_vector
Change Orientation of An Oriented Vector and Make it a Broadcaster
Description
The lhs %orientbc<-% rhs operator takes an oriented vector (lhs), and changes its orientation.
It also makes lhs a broadcaster if it wasn’t already.
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.