9#include "idol/general/numericals.h"
14 template<
class KeyT,
class ValueT>
18template<
class KeyT =
idol::Var,
class ValueT =
double>
21 ValueT m_constant = 0.;
24 AffExpr(ValueT t_constant);
25 AffExpr(
const KeyT& t_key);
28 virtual ~AffExpr() =
default;
30 AffExpr(
const AffExpr& t_src) =
default;
31 AffExpr(AffExpr&&)
noexcept =
default;
33 AffExpr& operator=(
const AffExpr& t_rhs) =
default;
34 AffExpr& operator=(AffExpr&&)
noexcept =
default;
36 AffExpr& operator+=(
const AffExpr& t_rhs);
38 AffExpr& operator-=(
const AffExpr& t_rhs);
39 AffExpr& operator*=(
double t_rhs);
40 AffExpr& operator/=(
double t_rhs);
41 AffExpr operator-()
const;
46 ValueT& constant() {
return m_constant; }
48 [[nodiscard]]
const ValueT& constant()
const {
return m_constant; }
50 [[nodiscard]]
bool is_zero(
double t_tolerance)
const { return ::idol::is_zero(constant(), t_tolerance) && linear().is_zero(t_tolerance); }
57 static const AffExpr<KeyT, ValueT> Zero;
62template<
class KeyT,
class ValueT>
63idol::AffExpr<KeyT, ValueT>::AffExpr(
const KeyT &t_key) : m_linear(t_key) {
67template<
class Key1,
class ValueT>
68idol::AffExpr<Key1, ValueT> idol::AffExpr<Key1, ValueT>::operator-()
const {
70 result.constant() = -result.constant();
71 result.linear() = -result.linear();
75template<
class Key1,
class ValueT>
76idol::AffExpr<Key1, ValueT>::AffExpr() {
80template<
class Key1,
class ValueT>
81idol::AffExpr<Key1, ValueT>::AffExpr(ValueT t_constant) : m_constant(std::move(t_constant)) {
85template<
class Key1,
class ValueT>
86idol::AffExpr<Key1, ValueT>::AffExpr(
LinExpr<Key1> t_expr) : m_linear(std::move(t_expr)) {
90template<
class Key1,
class ValueT>
91idol::AffExpr<Key1, ValueT> &idol::AffExpr<Key1, ValueT>::operator+=(
const AffExpr &t_rhs) {
92 m_linear += t_rhs.m_linear;
93 m_constant += t_rhs.m_constant;
97template<
class Key1,
class ValueT>
98idol::AffExpr<Key1, ValueT> &idol::AffExpr<Key1, ValueT>::operator-=(
const AffExpr &t_rhs) {
99 m_linear -= t_rhs.m_linear;
100 m_constant -= t_rhs.m_constant;
104template<
class Key1,
class ValueT>
105idol::AffExpr<Key1, ValueT> &idol::AffExpr<Key1, ValueT>::operator*=(
double t_rhs) {
111template<
class Key1,
class ValueT>
112idol::AffExpr<Key1, ValueT> &idol::AffExpr<Key1, ValueT>::operator/=(
double t_rhs) {
120 template<
class Key1,
class ValueT>
121 std::ostream &operator<<(std::ostream &t_os,
const idol::AffExpr<Key1, ValueT> &t_expr) {
125 t_os << t_expr.linear();
130 t_os << t_expr.constant();
132 if (!t_expr.linear().empty()) {
133 t_os <<
" + " << t_expr.linear();