Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Arithmetic.hpp File Reference

Element-wise arithmetic, scalar promotion, and compound assignment. More...

Include dependency graph for Arithmetic.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<Array L, Array R, typename Op >
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto binary_op (const L &lhs, const R &rhs, Op op, bool check_zero_divisor=false)
 Applies an arithmetic callable to two broadcast-compatible arrays.
 
template<Array A, Numeric Scalar, typename Op >
auto binary_scalar_op (const A &lhs, const Scalar &rhs, Op op, bool check_zero_divisor=false)
 Applies an arithmetic callable to an array and a right scalar. The returned array preserves the container family and shape of lhs. Its dtype is selected by the array/scalar promotion rules.
 
template<Numeric Scalar, Array A, typename Op >
auto binary_scalar_op (const Scalar &lhs, const A &rhs, Op op, bool check_zero_divisor=false)
 Applies an arithmetic callable to a left scalar and an array. The returned array preserves the container family and shape of rhs. Its dtype is selected by the array/scalar promotion rules.
 
template<Array L, Array R, typename Op >
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & compound_op (L &lhs, const R &rhs, Op op, bool check_zero_divisor=false)
 Applies an arithmetic callable in place using array broadcasting.
 
template<Array A, Numeric S, typename Op >
A & compound_scalar_op (A &lhs, const S &rhs, Op op, bool check_zero_divisor=false)
 Applies an arithmetic callable in place with a right scalar.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator+ (const L &lhs, const R &rhs)
 Adds two broadcast-compatible arrays.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator- (const L &lhs, const R &rhs)
 Subtracts two broadcast-compatible arrays.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator* (const L &lhs, const R &rhs)
 Multiplies two broadcast-compatible arrays element-wise.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator/ (const L &lhs, const R &rhs)
 Divides two broadcast-compatible arrays element-wise.
 
template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator+ (const A &lhs, const Scalar &rhs)
 Adds a scalar to every array element.
 
template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator- (const A &lhs, const Scalar &rhs)
 Subtracts a scalar from every array element.
 
template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator* (const A &lhs, const Scalar &rhs)
 Multiplies every array element by a scalar.
 
template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator/ (const A &lhs, const Scalar &rhs)
 Divides every array element by a scalar.
 
template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator+ (const Scalar &lhs, const A &rhs)
 Adds a scalar to every array element.
 
template<Numeric Scalar, Array A>
auto operator- (const Scalar &lhs, const A &rhs)
 Subtracts every array element from a scalar.
 
template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator* (const Scalar &lhs, const A &rhs)
 Multiplies every array element by a scalar.
 
template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator/ (const Scalar &lhs, const A &rhs)
 Divides a scalar by every array element.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator+= (L &lhs, const R &rhs)
 Adds a broadcast-compatible array in place.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator-= (L &lhs, const R &rhs)
 Subtracts a broadcast-compatible array in place.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator*= (L &lhs, const R &rhs)
 Multiplies by a broadcast-compatible array in place.
 
template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator/= (L &lhs, const R &rhs)
 Divides by a broadcast-compatible array in place.
 
template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator+= (A &lhs, const S &rhs)
 Adds a scalar to every element in place.
 
template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator-= (A &lhs, const S &rhs)
 Subtracts a scalar from every element in place.
 
template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator*= (A &lhs, const S &rhs)
 Multiplies every element by a scalar in place.
 
template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator/= (A &lhs, const S &rhs)
 Divides every element by a scalar in place.
 
template<Array A>
requires Numeric<typename A::value_type>
A operator- (const A &arr)
 Returns an owning element-wise negation with the input shape and dtype.
 
template<Array A>
requires Numeric<typename A::value_type>
A operator+ (const A &arr)
 Returns a copy of the array object with unchanged values.
 

Detailed Description

Element-wise arithmetic, scalar promotion, and compound assignment.

Operations that allocate results require owning-container result trait specializations. Element operators receive the original operand types; the result is converted afterward. Native C++ arithmetic and conversion rules apply, including representability requirements. Division rejects zero and signed expression-type minimum divided by minus one. Compound operations use the operands' initial values, including overlapping views; repeated destination offsets receive the last logical write. Operations involving views stage O(n) results. Allocation failures propagate.

Definition in file Arithmetic.hpp.

Function Documentation

◆ binary_op()

template<Array L, Array R, typename Op >
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto binary_op ( const L &  lhs,
const R &  rhs,
Op  op,
bool  check_zero_divisor = false 
)

Applies an arithmetic callable to two broadcast-compatible arrays.

Matching container kinds are preserved; mixed kinds produce Tensor. The result uses the promoted dtype of the two operands, has their common broadcasted shape, and owns independent storage. When check_zero_divisor is true, each used broadcasted right-hand value is checked before invoking op.

Template Parameters
LLeft owning Stratax array type.
RRight owning Stratax array type; it may differ from L.
OpBinary arithmetic callable.
Parameters
lhsLeft array operand.
rhsRight array operand.
opCallable invoked for every broadcasted element pair.
check_zero_divisorWhether zero right-hand values are rejected.
Returns
Owning array rebound to the promoted operand dtype and containing the element-wise results.
Exceptions
Exceptions::BroadcastErrorIf shapes are incompatible or an empty operand would supply values to a nonempty result.
Exceptions::ZeroDivisionErrorIf divisor checking is enabled and a broadcasted right-hand element equals zero.
Exceptions::OverflowErrorIf divisor checking is enabled and signed division is not representable.
Anyexception propagated by allocation or op.
Complexity
O((n + 1) * r), where n is result size and r is result rank.

Definition at line 53 of file Arithmetic.hpp.

◆ binary_scalar_op() [1/2]

template<Array A, Numeric Scalar, typename Op >
auto binary_scalar_op ( const A &  lhs,
const Scalar &  rhs,
Op  op,
bool  check_zero_divisor = false 
)

Applies an arithmetic callable to an array and a right scalar. The returned array preserves the container family and shape of lhs. Its dtype is selected by the array/scalar promotion rules.

Template Parameters
AStratax array and result type.
ScalarNumeric scalar type.
OpBinary arithmetic callable.
Parameters
lhsArray supplying each left argument.
rhsScalar supplied as every right argument.
opCallable invoked for each array element.
check_zero_divisorWhether a zero rhs is rejected.
Returns
Owning array with the same shape and container family as lhs, and promoted dtype.
Exceptions
Exceptions::ZeroDivisionErrorIf divisor checking is enabled and rhs equals zero, including when lhs is empty.
Exceptions::OverflowErrorIf divisor checking is enabled and signed division is not representable.
Anyexception propagated by allocation or op.
Complexity
O(lhs.size()).

Definition at line 100 of file Arithmetic.hpp.

◆ binary_scalar_op() [2/2]

template<Numeric Scalar, Array A, typename Op >
auto binary_scalar_op ( const Scalar &  lhs,
const A &  rhs,
Op  op,
bool  check_zero_divisor = false 
)

Applies an arithmetic callable to a left scalar and an array. The returned array preserves the container family and shape of rhs. Its dtype is selected by the array/scalar promotion rules.

Template Parameters
ScalarNumeric scalar type.
AStratax array and result type.
OpBinary arithmetic callable.
Parameters
lhsScalar supplied as every left argument.
rhsArray supplying each right argument.
opCallable invoked for each array element.
check_zero_divisorWhether zero elements of rhs are rejected.
Returns
Owning array with the same shape and container family as rhs, and promoted dtype.
Exceptions
Exceptions::ZeroDivisionErrorIf divisor checking is enabled and an element of rhs equals zero.
Exceptions::OverflowErrorIf divisor checking is enabled and signed division is not representable.
Anyexception propagated by allocation or op.
Complexity
O(rhs.size()).

Definition at line 138 of file Arithmetic.hpp.

◆ compound_op()

template<Array L, Array R, typename Op >
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & compound_op ( L &  lhs,
const R &  rhs,
Op  op,
bool  check_zero_divisor = false 
)

Applies an arithmetic callable in place using array broadcasting.

The right operand may broadcast to the existing shape of lhs, but the operation never resizes or reshapes lhs. Callable results are converted to L::value_type before assignment. All built-in validation precedes writes. Operands are read as they were before mutation; if destination elements alias, the last logical write wins. Non-owning operands require O(n) staging storage. A throwing callable may leave partial writes when both operands own storage.

Precondition
The callable does not modify either operand or its metadata.
Template Parameters
LMutable left Stratax array type.
RRight Stratax array type.
OpBinary arithmetic callable.
Parameters
lhsArray modified in place.
rhsArray broadcast over lhs.
opCallable invoked for every used element pair.
check_zero_divisorWhether used zero values from rhs are rejected.
Returns
Reference to lhs.
Exceptions
Exceptions::BroadcastErrorIf the shapes are incompatible or their broadcasted shape differs from the existing shape of lhs.
Exceptions::ZeroDivisionErrorIf divisor checking is enabled and a used right-hand element is zero.
Exceptions::OverflowErrorIf divisor checking is enabled and signed division is not representable.
Anyexception propagated by allocation or op.
Complexity
O((n + 1) * r), where n is lhs.size() and r is its rank.

Definition at line 185 of file Arithmetic.hpp.

◆ compound_scalar_op()

template<Array A, Numeric S, typename Op >
A & compound_scalar_op ( A &  lhs,
const S &  rhs,
Op  op,
bool  check_zero_divisor = false 
)

Applies an arithmetic callable in place with a right scalar.

Callable results are converted to A::value_type before assignment. The scalar is copied first, even when it references a destination element. Views stage O(n) results; repeated destination offsets use the last result. A throwing callable may leave partial writes for owning destinations.

Precondition
The callable does not modify either operand or its metadata.
Template Parameters
AMutable Stratax array type.
SNumeric scalar type.
OpBinary arithmetic callable.
Parameters
lhsArray modified in place.
rhsScalar supplied as every right argument.
opCallable invoked for each element.
check_zero_divisorWhether a zero rhs is rejected.
Returns
Reference to lhs.
Exceptions
Exceptions::ZeroDivisionErrorIf divisor checking is enabled and rhs is zero, including when lhs is empty.
Exceptions::OverflowErrorIf divisor checking is enabled and signed division is not representable.
Anyexception propagated by allocation or op.
Complexity
O(n) for owning arrays, O((n + 1) * r) for views.

Definition at line 246 of file Arithmetic.hpp.

◆ operator*() [1/3]

template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator* ( const A &  lhs,
const Scalar &  rhs 
)

Multiplies every array element by a scalar.

Returns
Owning result with the array shape.
Complexity
O(lhs.size()).

Definition at line 335 of file Arithmetic.hpp.

◆ operator*() [2/3]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator* ( const L &  lhs,
const R &  rhs 
)

Multiplies two broadcast-compatible arrays element-wise.

Returns
Owning broadcasted product.
Exceptions
Exceptions::BroadcastErrorIf shapes are incompatible or an empty operand would supply values to a nonempty result.
Complexity
O((n + 1) * r).

Definition at line 300 of file Arithmetic.hpp.

◆ operator*() [3/3]

template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator* ( const Scalar &  lhs,
const A &  rhs 
)

Multiplies every array element by a scalar.

Returns
Owning result with the array shape.
Complexity
O(rhs.size()).

Definition at line 366 of file Arithmetic.hpp.

◆ operator*=() [1/2]

template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator*= ( A &  lhs,
const S &  rhs 
)

Multiplies every element by a scalar in place.

Returns
Reference to lhs.
Complexity
O(lhs.size()).

Definition at line 442 of file Arithmetic.hpp.

◆ operator*=() [2/2]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator*= ( L &  lhs,
const R &  rhs 
)

Multiplies by a broadcast-compatible array in place.

Returns
Reference to lhs; its shape is unchanged.
Exceptions
Exceptions::BroadcastErrorIf broadcasting is impossible or would change the shape of lhs.
Complexity
O((n + 1) * r).

Definition at line 407 of file Arithmetic.hpp.

◆ operator+() [1/4]

template<Array A>
requires Numeric<typename A::value_type>
A operator+ ( const A &  arr)

Returns a copy of the array object with unchanged values.

Note
Owning arrays copy their storage; copying an ArrayView preserves aliasing.
Complexity
O(arr.size() + arr.rank()) for owning arrays; O(arr.rank()) for views.

Definition at line 473 of file Arithmetic.hpp.

◆ operator+() [2/4]

template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator+ ( const A &  lhs,
const Scalar &  rhs 
)

Adds a scalar to every array element.

Returns
Owning result with the array shape.
Complexity
O(lhs.size()).

Definition at line 319 of file Arithmetic.hpp.

◆ operator+() [3/4]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator+ ( const L &  lhs,
const R &  rhs 
)

Adds two broadcast-compatible arrays.

Returns
Owning broadcasted sum.
Exceptions
Exceptions::BroadcastErrorIf shapes are incompatible or an empty operand would supply values to a nonempty result.
Complexity
O((n + 1) * r).

Definition at line 278 of file Arithmetic.hpp.

◆ operator+() [4/4]

template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator+ ( const Scalar &  lhs,
const A &  rhs 
)

Adds a scalar to every array element.

Returns
Owning result with the array shape.
Complexity
O(rhs.size()).

Definition at line 351 of file Arithmetic.hpp.

◆ operator+=() [1/2]

template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator+= ( A &  lhs,
const S &  rhs 
)

Adds a scalar to every element in place.

Returns
Reference to lhs.
Complexity
O(lhs.size()).

Definition at line 426 of file Arithmetic.hpp.

◆ operator+=() [2/2]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator+= ( L &  lhs,
const R &  rhs 
)

Adds a broadcast-compatible array in place.

Returns
Reference to lhs; its shape is unchanged.
Exceptions
Exceptions::BroadcastErrorIf broadcasting is impossible or would change the shape of lhs.
Complexity
O((n + 1) * r).

Definition at line 385 of file Arithmetic.hpp.

◆ operator-() [1/4]

template<Array A>
requires Numeric<typename A::value_type>
A operator- ( const A &  arr)

Returns an owning element-wise negation with the input shape and dtype.

Template Parameters
AOwning numeric array whose value type can be list-initialized from -1.
Note
Unsigned dtypes are not supported by this implementation; native arithmetic representability requirements apply.
Complexity
O(arr.size() + arr.rank()).

Definition at line 462 of file Arithmetic.hpp.

◆ operator-() [2/4]

template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator- ( const A &  lhs,
const Scalar &  rhs 
)

Subtracts a scalar from every array element.

Returns
Owning result with the array shape.
Complexity
O(lhs.size()).

Definition at line 327 of file Arithmetic.hpp.

◆ operator-() [3/4]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator- ( const L &  lhs,
const R &  rhs 
)

Subtracts two broadcast-compatible arrays.

Returns
Owning lhs - rhs result.
Exceptions
Exceptions::BroadcastErrorIf shapes are incompatible or an empty operand would supply values to a nonempty result.
Complexity
O((n + 1) * r).

Definition at line 289 of file Arithmetic.hpp.

◆ operator-() [4/4]

template<Numeric Scalar, Array A>
auto operator- ( const Scalar &  lhs,
const A &  rhs 
)

Subtracts every array element from a scalar.

Returns
Owning lhs - rhs[i] result.
Complexity
O(rhs.size()).

Definition at line 358 of file Arithmetic.hpp.

◆ operator-=() [1/2]

template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator-= ( A &  lhs,
const S &  rhs 
)

Subtracts a scalar from every element in place.

Returns
Reference to lhs.
Complexity
O(lhs.size()).

Definition at line 434 of file Arithmetic.hpp.

◆ operator-=() [2/2]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator-= ( L &  lhs,
const R &  rhs 
)

Subtracts a broadcast-compatible array in place.

Returns
Reference to lhs; its shape is unchanged.
Exceptions
Exceptions::BroadcastErrorIf broadcasting is impossible or would change the shape of lhs.
Complexity
O((n + 1) * r).

Definition at line 396 of file Arithmetic.hpp.

◆ operator/() [1/3]

template<Array A, Numeric Scalar>
requires Numeric<typename A::value_type>
auto operator/ ( const A &  lhs,
const Scalar &  rhs 
)

Divides every array element by a scalar.

Returns
Owning result with the array shape.
Exceptions
Exceptions::ZeroDivisionErrorIf rhs is zero.
Exceptions::OverflowErrorIf native signed division is not representable.
Complexity
O(lhs.size()).

Definition at line 343 of file Arithmetic.hpp.

◆ operator/() [2/3]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
auto operator/ ( const L &  lhs,
const R &  rhs 
)

Divides two broadcast-compatible arrays element-wise.

Returns
Owning broadcasted quotient.
Exceptions
Exceptions::BroadcastErrorIf shapes are incompatible or an empty operand would supply values to a nonempty result.
Exceptions::ZeroDivisionErrorIf a used divisor element is zero.
Exceptions::OverflowErrorIf native signed division is not representable.
Complexity
O((n + 1) * r).

Definition at line 311 of file Arithmetic.hpp.

◆ operator/() [3/3]

template<Numeric Scalar, Array A>
requires Numeric<typename A::value_type>
auto operator/ ( const Scalar &  lhs,
const A &  rhs 
)

Divides a scalar by every array element.

Returns
Owning lhs / rhs[i] result.
Exceptions
Exceptions::ZeroDivisionErrorIf any divisor element is zero.
Exceptions::OverflowErrorIf native signed division is not representable.
Complexity
O(rhs.size()).

Definition at line 374 of file Arithmetic.hpp.

◆ operator/=() [1/2]

template<Array A, Numeric S>
requires Numeric<typename A::value_type>
A & operator/= ( A &  lhs,
const S &  rhs 
)

Divides every element by a scalar in place.

Returns
Reference to lhs.
Exceptions
Exceptions::ZeroDivisionErrorIf rhs is zero, including for an empty array.
Exceptions::OverflowErrorIf native signed division is not representable.
Complexity
O(n) for owning arrays; O((n + 1) * r) for views.

Definition at line 450 of file Arithmetic.hpp.

◆ operator/=() [2/2]

template<Array L, Array R>
requires ( Numeric<typename L::value_type> && Numeric<typename R::value_type> )
L & operator/= ( L &  lhs,
const R &  rhs 
)

Divides by a broadcast-compatible array in place.

Returns
Reference to lhs; its shape is unchanged.
Exceptions
Exceptions::BroadcastErrorIf broadcasting is impossible or would change the shape of lhs.
Exceptions::ZeroDivisionErrorIf a used divisor is zero.
Exceptions::OverflowErrorIf native signed division is not representable.
Complexity
O((n + 1) * r).

Definition at line 418 of file Arithmetic.hpp.