cross

Syntax

cross(func, X, [Y])

or

X <operator>:C Y

or

func:C(X, [Y])

Arguments

func is a binary function.

X and Y can be pair/vector/matrix. They can have different data forms and sizes.

Y is optional. If Y is not specified, perform cross(func, X, X) where func must be a symmetric binary function, such as corr .

Details

Apply func to the permutation of all individual elements of X and Y and return a matrix.

Assume X has m elements and Y has n elements. The template returns an m (rows) by n (columns) matrix. Below is the pseudo code for the cross template.

for(i:0~(size(X)-1)){
for(j:0~(size(Y)-1)){
  result[i,j]=<function>(X[i], Y[j]);
}
}
return result;

If X and Y are matrices, the iteration is over the columns of X and Y .

If the result of func(X[i], Y[j]) is a scalar, the result of the cross template is a matrix.

If the result of func(X[i], Y[j]) is a vector, the result of the cross template is a tuple with m elements. Each of the element is a tuple with n elements.

Examples

cross with two vectors:

x=1 2;
y=3 5 7;
x+:C y;
lable 3 5 7
1 4 6 8
2 5 7 9
cross(mul, x, y);
lable 3 5 7
1 3 5 7
2 6 10 14
cross(pow, x, y);
lable 3 5 7
1 1 1 1
2 8 32 128

cross with two matrices:

m = 1..6$2:3;
m;
col1 col2 col3
1 3 5
2 4 6
n=1..4$2:2;
n;
col1 col2
1 3
2 4
cross(**, m, n);
col1 col2
5 11
11 25
17 39

cross with a vector and a matrix:

def topsum(x,n){return sum x[0:n]};
a=1..18$6:3;
a;
col1 col2 col3
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
b=2 4;
a topsum :C b;
2 4
3 10
15 34
27 58

cross with tuple type results:

x=1 2
y=1..6$2:3
cross(add, x, y);
// output
(([2,3],[4,5],[6,7]),([3,4],[5,6],[7,8]))

x=1 2
y=1..6$3:2
cross(add, x, y);
// output
(([2,3,4],[5,6,7]),([3,4,5],[6,7,8]))