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

Class MinZeroExpression

include/cppoptlib/function_expressions.h:319–358  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

317// --- Helper Expression for min{0, f(x)}
318template <typename F>
319struct 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)}
361template <typename F>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected