![]() |
Eigen
5.0.1
|
Several Eigen extension points accept a user-provided function object, or functor. This is often the lightest way to customize an expression without defining a new expression type.
Common entry points include:
At minimum, a functor provides an operator() matching the expression kind:
The result type is deduced from operator() and may differ from the input scalar type. Binary functors can rely on Eigen's scalar binary-op traits when the return type follows the usual scalar rules; custom mixed-scalar operations may need a ScalarBinaryOpTraits specialization.
Functors used with redux() must implement an associative binary operation and return the same scalar type as the expression, because Eigen may combine coefficients in an order chosen by the evaluator.
Eigen assigns conservative defaults to unknown functors:
For performance-sensitive functors, specialize Eigen::internal::functor_traits for your functor:
Cost is an approximate scalar cost used by Eigen's expression evaluators. PacketAccess may be set to true only when the functor also provides packet overloads compatible with the packet types used by the relevant scalar type. Unary and binary packetized functors usually provide a packetOp() member; reduction functors that support packet reductions also provide predux().
Most user functors should start with scalar-only code and leave PacketAccess set to false. That keeps the functor portable and lets Eigen use the scalar path for that operation while preserving vectorization opportunities elsewhere in the expression.
Functor objects are copied into expression objects and may be evaluated later. Store any captured data so that it outlives the expression evaluation. For example, a nullary functor producing a view into another matrix should either store a safe reference to an object owned by the caller or require the caller to evaluate the expression before the referenced object goes out of scope.
If the expression needs a new shape, a custom nested expression type, or evaluator-specific behavior that cannot be expressed coefficient by coefficient, see Adding a new expression type. For many procedural matrices and indexed views, Matrix manipulation via nullary-expressions is simpler than a new expression type.