MCPcopy Create free account
hub / github.com/PatWie/CppNumericalSolvers / MaxZeroExpression

Class MaxZeroExpression

include/cppoptlib/function_expressions.h:362–399  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

360// --- Helper Expression for max{0, f(x)}
361template <typename F>
362struct MaxZeroExpression
363 : public FunctionInterface<typename F::ScalarType, F::Differentiability,
364 F::Dimension> {
365 static constexpr int Dimension = F::Dimension;
366 using ScalarType = typename F::ScalarType;
367 using VectorType = Eigen::Matrix<ScalarType, Dimension, 1>;
368 using MatrixType = Eigen::Matrix<ScalarType, Dimension, Dimension>;
369 static constexpr DifferentiabilityMode Differentiability =
370 F::Differentiability;
371
372 F f;
373 explicit MaxZeroExpression(const F& f_) : f(f_) {}
374
375 virtual ScalarType operator()(const VectorType& x, VectorType* grad = nullptr,
376 MatrixType* hess = nullptr) const override {
377 if constexpr (Differentiability == DifferentiabilityMode::None) {
378 ScalarType val = f(x);
379 return (val <= 0)
380 ? ConstExpression<ScalarType, Differentiability, Dimension>(
381 ScalarType(0))(x)
382 : val;
383 } else {
384 ScalarType val = f(x, grad, hess);
385 if (val <= 0) {
386 return ConstExpression<ScalarType, Differentiability, Dimension>(
387 ScalarType(0))(x, grad, hess);
388 } else {
389 return val;
390 }
391 }
392 }
393
394 virtual std::unique_ptr<
395 FunctionInterface<ScalarType, Differentiability, Dimension>>
396 clone() const override {
397 return std::make_unique<MaxZeroExpression<F>>(f);
398 }
399};
400
401// For addition.
402//

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected