idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
LinExpr.h
1//
2// Created by henri on 03/10/22.
3//
4
5#ifndef OPTIMIZE_EXPR_H
6#define OPTIMIZE_EXPR_H
7
8#include "idol/general/utils/SparseVector.h"
9#include "idol/general/utils/IteratorForward.h"
10#include "idol/general/utils/exceptions/Exception.h"
11#include <memory>
12#include <utility>
13#include <functional>
14#include <optional>
15#include <list>
16
17namespace idol {
18 class Var;
19
20 template<class KeyT, class ValueT>
21 class LinExpr;
22}
23
27template<class KeyT = idol::Var, class ValueT = double>
28class idol::LinExpr : public SparseVector<KeyT, ValueT> {
29public:
30 LinExpr() = default;
31 LinExpr(KeyT t_key); // NOLINT(google-explicit-constructor)
32 LinExpr(SparseVector<KeyT, ValueT> t_vector) : SparseVector<KeyT, ValueT>(std::move(t_vector)) {} // NOLINT(*-explicit-constructor)
33 LinExpr(const ValueT& t_factor, const KeyT& t_key);
34 LinExpr(ValueT&& t_factor, const KeyT& t_key);
35
36 LinExpr(const LinExpr<KeyT, ValueT>&) = default;
37 LinExpr(LinExpr<KeyT, ValueT>&&) = default;
38
39 LinExpr& operator=(const LinExpr<KeyT, ValueT>&) noexcept = default;
40 LinExpr& operator=(LinExpr<KeyT, ValueT>&&) noexcept = default;
41
42 LinExpr& operator+=(const LinExpr<KeyT, ValueT>& t_rhs);
43 LinExpr& operator+=(const KeyT& t_rhs);
44
45 LinExpr& operator-=(const LinExpr<KeyT, ValueT>& t_rhs);
46 LinExpr& operator-=(const KeyT& t_rhs);
47
48 static LinExpr<KeyT, ValueT> Zero;
49};
50
51template<class KeyT, class ValueT> idol::LinExpr<KeyT, ValueT> idol::LinExpr<KeyT, ValueT>::Zero {};
52
53template<class KeyT, class ValueT>
55 SparseVector<KeyT, ValueT>::operator-=(SparseVector<KeyT, ValueT>(t_rhs, 1));
56 return *this;
57}
58
59template<class KeyT, class ValueT>
61 SparseVector<KeyT, ValueT>::operator-=((SparseVector<KeyT, ValueT>&) t_rhs);
62 return *this;
63}
64
65template<class KeyT, class ValueT>
67 SparseVector<KeyT, ValueT>::operator+=(SparseVector<KeyT, ValueT>(t_rhs, 1));
68 return *this;
69}
70
71template<class Key, class ValueT>
73 SparseVector<Key, ValueT>::operator+=((SparseVector<Key, ValueT>&) t_rhs);
74 return *this;
75}
76
77template<class Key, class ValueT>
78idol::LinExpr<Key, ValueT>::LinExpr(ValueT &&t_factor, const Key &t_key) : SparseVector<Key, ValueT>(t_key, std::move(t_factor)) {
79
80}
81
82template<class Key, class ValueT>
83idol::LinExpr<Key, ValueT>::LinExpr(Key t_key) : SparseVector<Key, double>(t_key, 1.) {
84
85}
86
87template<class Key, class ValueT>
88idol::LinExpr<Key, ValueT>::LinExpr(const ValueT& t_factor, const Key &t_key) : SparseVector<Key, ValueT>(t_key, t_factor) {
89}
90
91namespace idol {
92 template<class KeyT, class ValueT>
93 std::ostream& operator<<(std::ostream& t_os, const LinExpr<KeyT, ValueT>& t_expr) {
94
95 if (t_expr.empty()) {
96 return t_os << "0";
97 }
98
99 auto it = t_expr.begin();
100 if constexpr (std::is_same_v<ValueT, double>) {
101 t_os << it->second;
102 t_os << " ";
103 t_os << it->first;
104 } else {
105 t_os << "[ " << it->second << " ] " << it->first;
106 }
107 ++it;
108 for (const auto end = t_expr.end() ; it != end ; ++it) {
109 t_os << " + ";
110 if constexpr (std::is_same_v<ValueT, double>) {
111 t_os << it->second << " " << it->first;
112 } else {
113 t_os << "[ " << it->second << " ] " << it->first;
114 }
115 }
116
117 return t_os;
118 }
119}
120
121#endif //OPTIMIZE_EXPR_H