| 317 | // --- Helper Expression for min{0, f(x)} |
| 318 | template <typename F> |
| 319 | struct MinZeroExpression |
| 320 | : public FunctionInterface<typename F::ScalarType, F::Differentiability, |
| 321 | F::Dimension> { |
| 322 | static constexpr int Dimension = F::Dimension; |
| 323 | using ScalarType = typename F::ScalarType; |
| 324 | using VectorType = Eigen::Matrix<ScalarType, Dimension, 1>; |
| 325 | using MatrixType = Eigen::Matrix<ScalarType, Dimension, Dimension>; |
| 326 | static constexpr DifferentiabilityMode Differentiability = |
| 327 | F::Differentiability; |
| 328 | |
| 329 | F f; |
| 330 | explicit MinZeroExpression(const F& f_) : f(f_) {} |
| 331 | |
| 332 | virtual ScalarType operator()(const VectorType& x, VectorType* grad = nullptr, |
| 333 | MatrixType* hess = nullptr) const override { |
| 334 | if constexpr (Differentiability == DifferentiabilityMode::None) { |
| 335 | ScalarType val = f(x); |
| 336 | // Use ConstExpression to return zero (with zero derivatives) if inactive. |
| 337 | return (val >= 0) |
| 338 | ? ConstExpression<ScalarType, Differentiability, Dimension>( |
| 339 | ScalarType(0))(x) |
| 340 | : val; |
| 341 | } else { |
| 342 | ScalarType val = f(x, grad, hess); |
| 343 | if (val >= 0) { |
| 344 | // Return a constant zero value along with zero gradient/Hessian. |
| 345 | return ConstExpression<ScalarType, Differentiability, Dimension>( |
| 346 | ScalarType(0))(x, grad, hess); |
| 347 | } else { |
| 348 | return val; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | virtual std::unique_ptr< |
| 354 | FunctionInterface<ScalarType, Differentiability, Dimension>> |
| 355 | clone() const override { |
| 356 | return std::make_unique<MinZeroExpression<F>>(f); |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | // --- Helper Expression for max{0, f(x)} |
| 361 | template <typename F> |
nothing calls this directly
no outgoing calls
no test coverage detected