eachRight
Syntax
eachRight(func, X, Y, [consistent=false])
or
X <operator>:R Y
(when consistent = false)
or
X <operator>:RC Y
(when consistent = true)
or
func:R(X, Y)
(when consistent = false)
or
func:RC(X, Y)
(when consistent = true)
Arguments
func is a binary function.
X/ Y is a vector/matrix/table/array vector/dictionary.
consistent is a Boolean value. The default value is false, indicating that the data type of the result is determined by each calculation result. Otherwise, the data type of the result is the same as the data type of the first calculation result. Note that if the data forms of result are inconsistent, consistent can only be specified as false. Otherwise, an error will be reported.
Details
Calculate func(X, Y(i))
for each element of Y.
-
Y(i)
is each element when Y is a vector. -
Y(i)
is each column when Y is a matrix. -
Y(i)
is each row when Y is a table. -
Y(i)
is each row when Y is an array vector. -
Y(i)
is each value when Y is a dictionary.
If the function/operator supports vector operation and the input itself is a vector,
we should avoid the eachRight
template and use the vector
function/operator directly instead for better performance.
Examples
eachRight
with 2 vectors:
x = 4 3 2 1
y = 3 0 6;
eachRight(add, x, y);
3 | 0 | 6 |
---|---|---|
7 | 4 | 10 |
6 | 3 | 9 |
5 | 2 | 8 |
4 | 1 | 7 |
x pow :R y;
3 | 0 | 6 |
---|---|---|
64 | 1 | 4096 |
27 | 1 | 729 |
8 | 1 | 64 |
1 | 1 | 1 |
eachRight
with a matrix and a vector:
x=1..6$2:3;
x;
col1 | col2 | col3 |
---|---|---|
1 | 3 | 5 |
2 | 4 | 6 |
1 1 ** :R x;
// output
[3,7,11]
eachRight
with 2 matrices:
y=6..1$3:2;
y;
col1 | col2 |
---|---|
6 | 3 |
5 | 2 |
4 | 1 |
eachRight(**, x, y);
// output
(#0
--
41
56
,#0
--
14
20
)
eachRight
with a dictionary and a vector:
d=dict(`a`b`c, [[1, 2, 3],[4, 5, 6], [7, 8, 9]])
eachRight(add,10 20 30,d)
// output
a->[11,22,33]
b->[14,25,36]
c->[17,28,39]