delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
ExpressionCell.h
1
7#pragma once
8
9#include <vector>
10
11#include "delpi/symbolic/Expression.h"
12#include "delpi/util/SelfReferenceCountingObject.hpp"
13#include "delpi/util/intrusive_ptr.hpp"
14
15namespace delpi {
16
27class ExpressionCell : public SelfReferenceCountingObject {
28 public:
29 using Addend = Expression::Addend;
30 using Addends = Expression::Addends;
31 using SubstitutionMap = Expression::SubstitutionMap;
32
34 [[nodiscard]] std::vector<Variable> variables() const;
36 [[nodiscard]] const Addends& addends() const noexcept { return addends_; }
37
39 [[nodiscard]] bool equal_to(const ExpressionCell& o) const noexcept;
41 [[nodiscard]] bool less(const ExpressionCell& o) const noexcept;
43 [[nodiscard]] std::size_t hash() const noexcept;
44
52 ExpressionCell& Add(const Variable& var, const mpq_class& coeff);
58 ExpressionCell& Multiply(const mpq_class& coeff);
65 ExpressionCell& Divide(const mpq_class& coeff);
66
74 template <MapFromTo<Variable, mpq_class> T>
75 [[nodiscard]] mpq_class Evaluate(const T& env = {}) const;
76
88 [[nodiscard]] Expression Substitute(const SubstitutionMap& s) const;
89
90 std::ostream& Print(std::ostream& os) const;
91 static std::ostream& PrintAddend(std::ostream& os, bool print_plus, const Variable& var, const mpq_class& coeff);
92
95 static intrusive_ptr<ExpressionCell> New(Addend linear_monomial);
96 static intrusive_ptr<ExpressionCell> New(Addends addends);
97 static intrusive_ptr<ExpressionCell> Copy(const ExpressionCell& o);
98
99 protected:
100 ExpressionCell() = default;
101 explicit ExpressionCell(Variable var);
102 explicit ExpressionCell(Addend linear_monomial);
103 explicit ExpressionCell(Addends addends);
104
105 mutable std::size_t hash_;
106 Expression::Addends addends_;
107};
108
109} // namespace delpi
std::size_t hash_
Cached hash of the object.
ExpressionCell & Add(const Variable &var, const mpq_class &coeff)
Add a linear monomial , where is a constant and is a Variable, to the current expression.
mpq_class Evaluate(const T &env={}) const
Evaluates using a given environment (by default, an empty environment).
Expression::Addends addends_
Map between each variable and it coefficient as terms of the summation.
ExpressionCell & Divide(const mpq_class &coeff)
Divide all terms of the summation by a coeff.
ExpressionCell & Multiply(const mpq_class &coeff)
Multiply all terms of the summation by a coeff.
Expression Substitute(const SubstitutionMap &s) const
Create a copy of this expression, replacing all occurrences of the variables in s with corresponding ...
Represents a symbolic form of an expression.
Definition Expression.h:37
Utility class to be inherited from to obtain compatibility with the intrusive_ptr.
Real symbolic variable.
Definition Variable.h:20
Pointer to a generic object that supports intrusive reference counting.
Global namespace for the delpi library.