Element-wise expressions¶
Lazy element-wise operations: operators, math functions, masks and pipes.
Nothing is computed until an expression is assigned or made into a tensor.
Concepts¶
| Name | Description |
|---|---|
left_foldable | Whether a chain (a op b) op c of Fn may be stored as one node over a, b, c. |
stateless | A functor without state: every instance is as good as Fn{}, so none needs to be kept. |
Types¶
| Name | Description |
|---|---|
BitAnd | & folded over two or more values: bitwise on integers, logical on bool. |
BitNot | ~operand: bitwise on integers, logical NOT on bool. |
BitOr | | folded over two or more values: bitwise on integers, logical on bool. |
BitXor | ^ folded over two or more values: bitwise on integers, logical on bool. |
Clamp | value brought into [low, high], as std::clamp does it. |
Divides | Quotient of two or more values, applied as a left fold: Divides{}(a, b, c) is (a / b) / c. |
EqualTo | lhs == rhs as a bool. |
Ewise | An element-wise expression: fn(operands[i...]...) for every index, computed when assigned, not before. |
Greater | lhs > rhs as a bool. |
GreaterEqual | lhs >= rhs as a bool. |
Less | lhs < rhs as a bool. |
LessEqual | lhs <= rhs as a bool. |
LogicalNot | !operand as a bool. |
Maximum | The larger of two values, as std::max chooses: the first of equal ones. |
Minimum | The smaller of two values, as std::min chooses: the first of equal ones. |
Minus | Difference of two or more values, applied as a left fold: Minus{}(a, b, c) is (a - b) - c. |
Multiplies | Product of two or more values, applied as a left fold: Multiplies{}(a, b, c) is (a * b) * c. |
Negate | Unary minus of a value. |
NotEqualTo | lhs != rhs as a bool. |
Plus | Sum of two or more values, applied as a left fold: Plus{}(a, b, c) is (a + b) + c. |
Scalar | A value used as an operand, as in view * 2 or lengths * Speed{3.0}. |
Select | Chooses when_true or when_false by condition, which is a bool and nothing that converts to one. |
Square | operand * operand. |
Functions¶
operator+¶
Element-wise sum, as a lazy expression.
Either side may be a scalar, but not both. a + b + c is one node over three operands.
Precondition
The tensor operands have equal extents.
operator-¶
Element-wise difference, as a lazy expression.
Either side may be a scalar, but not both.
Precondition
The tensor operands have equal extents.
Element-wise negation, as a lazy expression.
operator*¶
Element-wise product, as a lazy expression.
Either side may be a scalar, but not both. Element by element, as in NumPy; the matrix product is a contraction (einsum), not *.
Precondition
The tensor operands have equal extents.
operator/¶
Element-wise quotient, as a lazy expression.
Either side may be a scalar, but not both.
Precondition
The tensor operands have equal extents.
operator<¶
Element-wise lhs < rhs, as a mask of bool.
Also <=, > and >=. == and != are not among the element-wise operators: they answer whether two things are equal, in a single bool, as std::equality_comparable, std::ranges::find and every test expect. Use equal and not_equal for masks. Integers of different signedness are compared by value, so -1 < 0u is true.
Precondition
The tensor operands have equal extents.
operator<=¶
Element-wise lhs <= rhs, as a mask of bool; see operator<.
operator>¶
Element-wise lhs > rhs, as a mask of bool; see operator<.
operator>=¶
Element-wise lhs >= rhs, as a mask of bool; see operator<.
operator!¶
Element-wise logical NOT of a mask, as a lazy expression of bool.
&& and || are not overloaded, as overloads they would no longer short-circuit: use & and | on masks.
operator~¶
Element-wise ~: bitwise on integers, logical NOT on bool.
operator&¶
Element-wise &: bitwise on integers, logical AND on bool masks.
Chains of one operation are one node, as for +.
Precondition
The tensor operands have equal extents.
operator|¶
Element-wise |: bitwise on integers, logical OR on bool masks.
Chains of one operation are one node, as for +.
Note
A pipeable on either side makes | a pipe instead (see pipeable); as a scalar operand, bool | abs would be a bitwise OR of whatever | makes of them.
Precondition
The tensor operands have equal extents.
operator^¶
Element-wise ^: bitwise on integers, logical XOR on bool masks.
Chains of one operation are one node, as for +.
Precondition
The tensor operands have equal extents.
Variables¶
map¶
fn(operands[i...]...) for every index: any function of the values, as an expression.
A stateless fn, a lambda without captures, takes no space in the expression. On scalars alone the result is the function's value. Called with fn only, map(fn) waits for its operand and is pipeable: x | map(fn).
const auto shifted = map([offset](int x) { return x + offset; }, view);
const auto mixed = map([](int x, int y, int z) { return (x * y) - z; }, a, b, 1);
Precondition
The tensor operands have equal extents.
equal¶
Element-wise equality, as a mask of bool.
Equality is a function rather than ==, which stays a single bool answer for whether two things are equal. Integers of different signedness are compared by value. Either argument may be a scalar.
not_equal¶
Element-wise inequality, as a mask of bool.
See equal.
negate¶
Element-wise negation, as a function object: negate(x), x | negate; the value of -x.
plus¶
Element-wise sum, as a function object: plus(a, b), x | plus(y); the value of a + b.
minus¶
Element-wise difference, as a function object: minus(a, b), x | minus(y) is x - y.
multiplies¶
Element-wise product, as a function object: multiplies(a, b), x | multiplies(y) is x * y.
divides¶
Element-wise quotient, as a function object: divides(a, b), x | divides(y) is x / y.
less¶
Element-wise <, as a function object and a mask of bool: x | less(y) is x < y.
less_equal¶
Element-wise <=, as a function object; see less.
greater¶
Element-wise >, as a function object; see less.
greater_equal¶
Element-wise >=, as a function object; see less.
logical_not¶
Element-wise logical NOT of a mask, as a function object: mask | logical_not is !mask.
bit_not¶
Element-wise ~, as a function object; see operator~.
bit_and¶
Element-wise &, as a function object: mask | bit_and(other) is mask & other.
bit_or¶
Element-wise |, as a function object: mask | bit_or(other) is mask | other.
bit_xor¶
Element-wise ^, as a function object; see bit_and.
where¶
Chooses element-wise: where(condition, when_true, when_false).
The condition is a mask of bool, not a number. The branches may be tensors or scalars; the value type is the common type of the two.
Precondition
The tensor operands have equal extents.
abs¶
Absolute value of each element.
Note
In a constant expression abs is the only portable function here: most of <cmath> is constexpr under GCC, not under Clang.
sqrt¶
Square root of each element.
cbrt¶
Cube root of each element.
exp¶
e raised to each element.
exp2¶
2 raised to each element.
expm1¶
exp(x) - 1 of each element, accurate for x near zero.
log¶
Natural logarithm of each element.
log2¶
Base-2 logarithm of each element.
log10¶
Base-10 logarithm of each element.
log1p¶
log(1 + x) of each element, accurate for x near zero.
sin¶
Sine of each element, in radians.
cos¶
Cosine of each element, in radians.
tan¶
Tangent of each element, in radians.
asin¶
Arc sine of each element, in radians.
acos¶
Arc cosine of each element, in radians.
atan¶
Arc tangent of each element, in radians.
sinh¶
Hyperbolic sine of each element.
cosh¶
Hyperbolic cosine of each element.
tanh¶
Hyperbolic tangent of each element.
floor¶
Each element rounded down to an integral value.
ceil¶
Each element rounded up to an integral value.
round¶
Each element rounded to the nearest integral value, halfway cases away from zero.
trunc¶
Each element rounded toward zero to an integral value.
isnan¶
Mask of the elements that are NaN.
isinf¶
Mask of the elements that are infinite.
isfinite¶
Mask of the elements that are neither infinite nor NaN.
pow¶
First operand raised to the second, element by element.
atan2¶
Arc tangent of y / x from the signs of both, in radians: atan2(y, x).
hypot¶
sqrt(x*x + y*y) without undue overflow or underflow, element by element.
fmod¶
Remainder of x / y with the sign of x, element by element.
square¶
x * x of each element.
This is what pow(x, 2) should be and is not quite: std::pow(float, int) is a double.
minimum¶
The smaller of two, element by element, as std::min chooses: the first of equal ones.
Named as in NumPy, so as not to be taken for std::min, which compares two values and returns a reference.
maximum¶
The larger of two, element by element, as std::max chooses; see minimum.
clamp¶
clamp(value, low, high): each element brought into [low, high], as std::clamp does it.
With only low and high, clamp(-1.0, 1.0) waits for its operand in a pipe: x | clamp(-1.0, 1.0).
Precondition
low <= high (not checked).
dense_in< Scalar< T >, O >¶
A scalar is dense in every order: it has no memory to walk.
Aliases¶
Abs¶
Function object of abs.
Sqrt¶
Function object of sqrt.
Cbrt¶
Function object of cbrt.
Exp¶
Function object of exp.
Exp2¶
Function object of exp2.
Expm1¶
Function object of expm1.
Log¶
Function object of log.
Log2¶
Function object of log2.
Log10¶
Function object of log10.
Log1p¶
Function object of log1p.
Sin¶
Function object of sin.
Cos¶
Function object of cos.
Tan¶
Function object of tan.
Asin¶
Function object of asin.
Acos¶
Function object of acos.
Atan¶
Function object of atan.
Sinh¶
Function object of sinh.
Cosh¶
Function object of cosh.
Tanh¶
Function object of tanh.
Floor¶
Function object of floor.
Ceil¶
Function object of ceil.
Round¶
Function object of round.
Trunc¶
Function object of trunc.
IsNan¶
Function object of isnan.
IsInf¶
Function object of isinf.
IsFinite¶
Function object of isfinite.
Pow¶
Function object of pow.
Atan2¶
Function object of atan2.
Hypot¶
Function object of hypot.
Fmod¶
Function object of fmod.