Version: v0.2.0
Status: Complete
Header: include/stratax/core/ops/Arithmetic.hpp
Overview
Arithmetic.hpp defines generic element-wise arithmetic for Stratax array-like containers.
It provides array-array, array-scalar, scalar-array, compound assignment, and unary operators for types satisfying the Array and Numeric concepts.
Responsibilities
The arithmetic module is responsible for:
- Enforcing shape compatibility for array-array operations
- Producing element-wise arithmetic results with preserved shape
- Validating division-by-zero conditions where required
- Providing in-place compound assignment operators
The arithmetic module is not responsible for:
- Broadcasting or automatic shape expansion
- Type-promotion policy beyond C++ operator semantics
- SIMD or parallel execution policy
Relationships
Arithmetic operators
├── Array concept constraints
├── Numeric concept constraints
├── validation::require_same_shape(...)
└── Exceptions::ZeroDivisionError for division checks
Depends on:
Used by:
- User-facing vector/matrix/tensor arithmetic expressions
Invariants
The following conditions are always true:
- Array-array operators require identical shape.
- Result shape matches the array operand shape.
- Non-compound operators do not mutate inputs.
- Compound operators are implemented via non-compound operators and assignment.
- Division by zero raises Exceptions::ZeroDivisionError.
Public Interface
Shape guard
template<Array A>
void require_same_arithmetic_shape(const A &lhs, const A &rhs)
Verifies that two arrays have the same shape before arithmetic.
Throws
Complexity
Array-array operators
template<Array A> A
operator+(
const A& lhs,
const A& rhs);
template<Array A> A
operator-(
const A& lhs,
const A& rhs);
template<Array A> A
operator*(
const A& lhs,
const A& rhs);
template<Array A> A
operator/(
const A& lhs,
const A& rhs);
A operator/(const A &lhs, const A &rhs)
Divides two arrays element by element.
A operator+(const A &lhs, const A &rhs)
Adds two arrays element by element.
A operator-(const A &lhs, const A &rhs)
Subtracts two arrays element by element.
A operator*(const A &lhs, const A &rhs)
Multiplies two arrays element by element.
Throws
Complexity
- O(n), plus O(r) shape check
Array-scalar operators
template<Array A, Numeric Scalar> A
operator+(
const A& lhs,
const Scalar& rhs);
template<Array A, Numeric Scalar> A
operator-(
const A& lhs,
const Scalar& rhs);
template<Array A, Numeric Scalar> A
operator*(
const A& lhs,
const Scalar& rhs);
template<Array A, Numeric Scalar> A
operator/(
const A& lhs,
const Scalar& rhs);
Alias for any scalar type accepted by Stratax numeric containers.
Throws
Complexity
Scalar-array operators
template<Numeric Scalar, Array A> A
operator+(
const Scalar& lhs,
const A& rhs);
template<Numeric Scalar, Array A> A
operator-(
const Scalar& lhs,
const A& rhs);
template<Numeric Scalar, Array A> A
operator*(
const Scalar& lhs,
const A& rhs);
template<Numeric Scalar, Array A> A
operator/(
const Scalar& lhs,
const A& rhs);
Throws
Complexity
Compound assignment operators
template<Array A> A&
operator+=(A& lhs,
const A& rhs);
template<Array A> A&
operator-=(A& lhs,
const A& rhs);
template<Array A> A&
operator*=(A& lhs,
const A& rhs);
template<Array A> A&
operator/=(A& lhs,
const A& rhs);
A & operator+=(A &lhs, const A &rhs)
Adds an array to itself in place.
A & operator*=(A &lhs, const A &rhs)
Multiplies an array by another array in place.
A & operator/=(A &lhs, const A &rhs)
Divides an array by another array in place.
A & operator-=(A &lhs, const A &rhs)
Subtracts an array from itself in place.
Throws
- Same categories as corresponding non-compound operator
Complexity
- O(n), plus O(r) shape check for array-array forms
Unary operators
Behavior
- Unary minus multiplies by -1
- Unary plus returns a copy
Complexity
Complexity Summary
| Operation | Complexity |
| require_same_arithmetic_shape | O(r) |
| Array-array operators | O(n + r) |
| Array-scalar operators | O(n) |
| Scalar-array operators | O(n) |
| Compound assignment | O(n + r) for array-array, O(n) for array-scalar |
| Unary plus/minus | O(n) |
n is element count and r is rank.
Examples
const auto sum = a + b;
const auto shifted = a + 2.0;
const auto inv = 1.0 / a;
a += b;
a *= 3.0;
Design Notes
Operators are intentionally generic and concept-constrained so they work uniformly across vector, matrix, and tensor containers.
Compound assignments delegate to their non-compound counterparts to centralize validation and arithmetic behavior.
Future Improvements
- Broadcasting semantics
- Explicit type-promotion policy controls
- SIMD kernels for common numeric types
- Optional parallel backends
See Also