Skip to content

Element wise binary operations

Roman edited this page Jun 11, 2020 · 5 revisions

Element wise binary operations

Element wise binary arithmetic operations

The following element-wise binary arithmetic operators are available all as free functions (note a Fastor expression can be a tensor, a complex expression or a number).

+(Fastor::Expression, Fastor::Expression)

Element-wise addition of two tensors

Tensor<double,2,3> a,b,c;
c = a+b;

-(Fastor::Expression, Fastor::Expression)

Element-wise subtraction of two tensors

Tensor<double,2,3> a,b,c;
c = a-b;

*(Fastor::Expression, Fastor::Expression)

Element-wise multiplication of two tensors

Tensor<double,2,3> a,b,c;
c = a*b;

/(Fastor::Expression, Fastor::Expression)

Element-wise division of two tensors

Tensor<double,2,3> a,b,c;
c = a/b;

min(Fastor::Expression, Fastor::Expression)

Compute element-wise minimum of two tensors

Tensor<double,2,3> a,b,c;
c = min(a,b);

max(Fastor::Expression, Fastor::Expression)

Compute element-wise maximum of two tensors

Tensor<double,2,3> a,b,c;
c = max(a,b);

pow(Fastor::Expression, Fastor::Expression)

Raise a tensor to the power given by another tensor element-wise. If both operands are tensors or tensor expressions then they should have the same size and dimension

Tensor<double,2,3> a,b,c;
c = pow(a,b);

atan2(Fastor::Expression, Fastor::Expression)

Compute arc tangent element-wise given two tensors. If both operands are tensors or tensor expressions then they should have the same size and dimension

Tensor<double,2,3> a,b,c;
c = atan2(a,b);

hypot(Fastor::Expression, Fastor::Expression)

Compute square root of the sum of the squares of two tensors. If both operands are tensors or tensor expressions then they should have the same size and dimension

Tensor<double,2,3> a,b,c;
c = hypot(a,b);

Element wise binary comparison/boolean operations

The following element-wise binary boolean operators are available all as free functions

==(Fastor::Expression, Fastor::Expression)

Check for element-wise equality between two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a==b;

!=(Fastor::Expression, Fastor::Expression)

Check for element-wise inequality between two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a!=b;

>(Fastor::Expression, Fastor::Expression)

Element-wise greater than of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a>b;

<(Fastor::Expression, Fastor::Expression)

Element-wise less than of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a<b;

>=(Fastor::Expression, Fastor::Expression)

Element-wise greater than or equal of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a>=b;

<=(Fastor::Expression, Fastor::Expression)

Element-wise less than or equal of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a<=b;

&&(Fastor::Expression, Fastor::Expression)

Element-wise and of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a && b;

||(Fastor::Expression, Fastor::Expression)

Element-wise or of two tensors

Tensor<double,2,3> a,b;
Tensor<bool,2,3> c = a || b;

A Fastor::Expression can also include numbers/scalars, for example

Tensor<int,2,2> a = {{1,2},{3,4}};
Tensor<bool,2,2> b = a > 2; 

will give you

[0, 0]
[1, 1]

You can have complex boolean expressions on both sides of the comparisons

Tensor<int,2,2> a = {{1,2},{3,4}};
Tensor<bool,2,2> b = a+1 > a-1; 

will give you

[1, 1]
[1, 1]

Note that, the result of the comparison is an expression and not another tensor so you will have to manually assign the result to a boolean tensor of the same dimensions.

Clone this wiki locally