idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
QuadExpr.h
1//
2// Created by henri on 15.11.24.
3//
4
5#ifndef IDOL_QUADEXPR_H
6#define IDOL_QUADEXPR_H
7
8#include "AffExpr.h"
9
10namespace idol {
11 template<class, class>
12 class QuadExpr;
13}
14
15template<class KeyT = idol::Var, class ValueT = double>
16class idol::QuadExpr : public LinExpr<CommutativePair<KeyT>, ValueT> {
17 AffExpr<KeyT, ValueT> m_affine;
18public:
19 QuadExpr() = default;
20 QuadExpr(ValueT t_constant) : m_affine(std::move(t_constant)) {} // NOLINT(*-explicit-constructor)
21 QuadExpr(const KeyT& t_key) : m_affine(std::move(t_key)) {} // NOLINT(*-explicit-constructor)
22 QuadExpr(LinExpr<KeyT, ValueT> t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
23 QuadExpr(AffExpr<KeyT, ValueT> t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
24
25 QuadExpr(const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2)) {}
26 QuadExpr(const ValueT& t_factor, const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2), t_factor) {}
27 QuadExpr(ValueT&& t_factor, const KeyT& t_key1, const KeyT& t_key2) : LinExpr<CommutativePair<KeyT>, ValueT>(CommutativePair<KeyT>(t_key1, t_key2), std::move(t_factor)) {}
28 QuadExpr(const ValueT& t_factor, const KeyT& t_key) : m_affine(t_factor, t_key) {}
29 QuadExpr(ValueT&& t_factor, const KeyT& t_key) : m_affine(std::move(t_factor), t_key) {}
30
31 QuadExpr(const QuadExpr& t_src) = default;
32 QuadExpr(QuadExpr&&) noexcept = default;
33
34 QuadExpr& operator=(const QuadExpr& t_rhs) = default;
35 QuadExpr& operator=(QuadExpr&&) noexcept = default;
36
37 QuadExpr& operator+=(const QuadExpr& t_rhs);
38 QuadExpr& operator-=(const QuadExpr& t_rhs);
39 QuadExpr& operator*=(double t_rhs);
40 QuadExpr& operator/=(double t_rhs);
41 QuadExpr operator-() const;
42
43 AffExpr<KeyT, ValueT>& affine() { return m_affine; }
44 const AffExpr<KeyT, ValueT>& affine() const { return m_affine; }
45
46 [[nodiscard]] bool has_quadratic() const { return !LinExpr<CommutativePair<KeyT>, ValueT>::empty(); }
47
48 [[nodiscard]] bool empty_all() const { return LinExpr<CommutativePair<KeyT>, ValueT>::empty() && m_affine.linear().empty(); }
49
50 [[nodiscard]] bool is_zero(double t_tolerance) const override;
51
52 void clear_all();
53};
54
55template<class KeyT, class ValueT>
57 LinExpr<CommutativePair<KeyT>, ValueT>::clear();
58 m_affine.clear();
59}
60
61template<class KeyT, class ValueT>
62bool idol::QuadExpr<KeyT, ValueT>::is_zero(double t_tolerance) const {
63 return m_affine.is_zero(t_tolerance) && LinExpr<CommutativePair<KeyT>, ValueT>::is_zero(t_tolerance);
64}
65
66template<class KeyT, class ValueT>
68 QuadExpr result;
69 result -= *this;
70 return result;
71}
72
73template<class KeyT, class ValueT>
75 LinExpr<CommutativePair<KeyT>, ValueT>::operator/=(t_rhs);
76 m_affine /= t_rhs;
77 return *this;
78}
79
80template<class KeyT, class ValueT>
82 LinExpr<CommutativePair<KeyT>, ValueT>::operator*=(t_rhs);
83 m_affine *= t_rhs;
84 return *this;
85}
86
87template<class KeyT, class ValueT>
89idol::QuadExpr<KeyT, ValueT>::operator-=(const QuadExpr &t_rhs) {
90 LinExpr<CommutativePair<KeyT>, ValueT>::operator-=(t_rhs);
91 m_affine -= t_rhs.m_affine;
92 return *this;
93}
94
95template<class KeyT, class ValueT>
97idol::QuadExpr<KeyT, ValueT>::operator+=(const QuadExpr &t_rhs) {
98 LinExpr<CommutativePair<KeyT>, ValueT>::operator+=(t_rhs);
99 m_affine += t_rhs.m_affine;
100 return *this;
101}
102
103
104namespace idol {
105
106 template<class KeyT, class ValueT>
107 std::ostream& operator<<(std::ostream& t_os, const QuadExpr<KeyT, ValueT>& t_expr) {
108 t_os << t_expr.affine() << " + " << (const LinExpr<CommutativePair<KeyT>, ValueT>&) t_expr;
109 return t_os;
110 }
111
112}
113
114
115#endif //IDOL_QUADEXPR_H