Namespace sym¶
-
namespace sym¶
Term operators
Overloaded operators for the Term struct.
- param lhs:
Left-hand side of the operator.
- param rhs:
Right-hand side of the operator.
- return:
Term obtained by applying the operation.
-
template<typename T, typename = std::enable_if_t<std::is_constructible_v<int, T>>>
Term<T> operator*(Term<T> lhs, const double rhs)¶
Arithmetic Expression operators
Overloaded operators for the Expression struct.
- param lhs:
Left-hand side of the operator.
- param rhs:
Right-hand side of the operator.
- return:
Expression obtained by applying the operation.
-
template<typename T, typename U>
Expression<T, U> operator+(Expression<T, U> lhs, const Expression<T, U> &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator+(Expression<T, U> lhs, const Term<T> &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator+(const Term<T> &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator+(const U &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator+(Expression<T, U> lhs, const U &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator+(const T &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator-(Expression<T, U> lhs, const Expression<T, U> &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator-(Expression<T, U> lhs, const Term<T> &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator-(const Term<T> &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator-(const U &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator-(Expression<T, U> lhs, const U &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator*(Expression<T, U> lhs, const T &rhs)¶
-
template<typename T, typename U, std::enable_if_t<!std::is_same_v<T, U>>* = nullptr>
Expression<T, U> operator*(Expression<T, U> lhs, const U &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator/(Expression<T, U> lhs, const T &rhs)¶
-
template<typename T, typename U, std::enable_if_t<!std::is_same_v<T, U>>* = nullptr>
Expression<T, U> operator/(Expression<T, U> lhs, const U &rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator/(Expression<T, U> lhs, int64_t rhs)¶
-
template<typename T, typename U>
Expression<T, U> operator*(const T &lhs, Expression<T, U> rhs)¶
-
template<typename T, typename U, std::enable_if_t<!std::is_same_v<T, U>>* = nullptr>
Expression<T, U> operator*(const U &lhs, Expression<T, U> rhs)¶
Typedefs
Functions
-
template<typename T>
bool operator==(const Term<T> &lhs, const Term<T> &rhs)¶ Check whether two terms are equal.
Two terms are equal if their variables are equal and their coefficients are equal within a small tolerance.
- Parameters:
lhs – Left-hand side of the operator.
rhs – Right-hand side of the operator.
- Returns:
True if the terms are equal, false otherwise.
-
template<typename T>
bool operator!=(const Term<T> &lhs, const Term<T> &rhs)¶ Check whether two terms are not equal.
Two terms are not equal if their variables are not equal or their coefficients are not equal within a small tolerance.
- Parameters:
lhs – Left-hand side of the operator.
rhs – Right-hand side of the operator.
- Returns:
True if the terms are not equal, false otherwise.
-
template<typename T, typename U>
bool operator==(const Expression<T, U> &lhs, const Expression<T, U> &rhs)¶ Check whether two expressions are equal.
- Parameters:
lhs – Left-hand side of the operator.
rhs – Right-hand side of the operator.
- Returns:
True if the expressions are equal, false otherwise.
-
template<typename T, typename U>
bool operator!=(const Expression<T, U> &lhs, const Expression<T, U> &rhs)¶ Check whether two expressions are not equal.
- Parameters:
lhs – Left-hand side of the operator.
rhs – Right-hand side of the operator.
- Returns:
True if the expressions are not equal, false otherwise.
-
template<typename T, typename U>
std::ostream &operator<<(std::ostream &os, const Expression<T, U> &expr)¶
Variables
-
static constexpr double TOLERANCE = 1e-9¶
-
template<typename T, typename U, typename = std::enable_if_t<std::is_constructible_v<T, U> && std::is_constructible_v<U, T> && std::is_constructible_v<int, T> && std::is_constructible_v<T, double> && std::is_constructible_v<U, double>>>
class Expression¶ - #include <Expression.hpp>
Class representing a symbolic expression. An expression is a sum of terms and a constant.
- Template Parameters:
T – Type of the coefficients of the terms. Must be constructible from an integer and a double.
U – Type of the constant. Must be constructible from a double.
Multiplication operators
Multiply the expression by a scalar. Multiplies the coefficients of the terms and the constant by the scalar.
- param rhs:
Scalar to multiply the expression by.
- return:
Reference to the expression.
Division operators
Divide the expression by a scalar. Divides the coefficients of the terms and the constant by the scalar.
Throws an exception if the scalar is zero.
- param rhs:
Scalar to divide the expression by.
- return:
Reference to the expression.
Public Functions
-
template<typename ...Args>
inline explicit Expression(Term<T> t, Args&&... ms)¶ Construct an Expression from a varargs list of terms. Constant is set to 0.
- Template Parameters:
Args – Variadic template parameter for the terms.
- Parameters:
t – First term.
ms – Remaining terms.
-
template<typename ...Args>
inline explicit Expression(Variable v, Args&&... ms)¶ Construct an Expression from a varargs list of variables. Constant is set to 0 and coefficients of the variables are set to 1.
- Template Parameters:
Args – Variadic template parameter for the variables.
- Parameters:
v – First variable.
ms – Remaining variables.
-
inline Expression(const std::vector<Term<T>> &ts, const U &con)¶
Construct an Expression from a vector of terms and a constant.
- Parameters:
ts – Vector of terms.
con – Constant of the expression.
-
Expression() = default¶
Default constructor. Expression has no Terms and a constant of 0.
-
inline explicit Expression(const U &r)¶
Construct an Expression from a constant. Expression has no Terms.
- Parameters:
r – Constant of the expression.
-
inline bool isZero() const¶
Check whether the expression is zero, i.e. has no Terms and 0 constant.
- Returns:
True if the expression is zero, false otherwise.
-
inline bool isConstant() const¶
Check whether the expression is a constant, i.e. has no Terms.
- Returns:
True if the expression is a constant, false otherwise.
-
inline Expression &operator+=(const Expression &rhs)¶
Add two expressions. Coefficients of like terms and constants are added.
If a term for the same variable is already present in the expression, the coefficients are added. Otherwise, the term is inserted into the expression.
- Parameters:
rhs – Expression to add.
- Returns:
Reference to the expression.
-
inline Expression<T, U> &operator+=(const Term<T> &rhs)¶
Add a term to the expression. Coefficients of like terms are added.
If a term for the same variable is already present in the expression, the coefficients are added. Otherwise, the term is inserted into the expression.
- Parameters:
rhs – Term to add.
- Returns:
Reference to the expression.
-
inline Expression<T, U> &operator+=(const U &rhs)¶
Add a constant to the expression.
- Parameters:
rhs – Constant to add.
- Returns:
Reference to the expression.
-
inline Expression<T, U> &operator-=(const Expression<T, U> &rhs)¶
Subtract two expressions. Coefficients of like terms and constants are subtracted.
If a term for the same variable is already present in the expression, the coefficients are subtracted. Otherwise, the term is inserted into the expression.
- Parameters:
rhs – Expression to subtract.
- Returns:
Reference to the expression.
-
inline Expression<T, U> &operator-=(const Term<T> &rhs)¶
Subtract a term from the expression. Coefficients of like terms are subtracted.
If a term for the same variable is already present in the expression, the coefficients are subtracted. Otherwise, the term is inserted into the expression.
- Parameters:
rhs – Term to subtract.
- Returns:
Reference to the expression.
-
inline Expression<T, U> &operator-=(const U &rhs)¶
Subtract a constant from the expression.
- Parameters:
rhs – Constant to subtract.
- Returns:
Reference to the expression.
-
inline Expression<T, U> operator-() const¶
Get the negative of the expression.
Negates the coefficients of the terms and the constant.
- Returns:
Negative of the expression.
-
inline const Term<T> &operator[](const std::size_t i) const¶
Get the term at a given index.
No bounds checking is performed.
- Parameters:
i – Index of the term.
- Returns:
Term at the given index.
-
inline U getConst() const noexcept¶
Get the constant of the expression.
- Returns:
Constant of the expression.
-
inline void setConst(const U &val)¶
Set the constant of the expression.
- Parameters:
val – Constant to set.
-
inline const std::vector<Term<T>> &getTerms() const¶
Get the terms of the expression.
- Returns:
Terms of the expression.
-
inline std::unordered_set<Variable> getVariables() const¶
Get the variables appearing in terms of the expression.
- Returns:
Variables of the expression.
-
template<typename V, std::enable_if_t<std::is_constructible_v<U, V>>* = nullptr>
inline Expression<T, V> convert() const¶ Convert the expression to a different type.
Converts the coefficients of the terms and the constant to a different type.
- Template Parameters:
V – Type to convert to.
- Returns:
Expression with coefficients and constant converted to type V.
-
inline double evaluate(const VariableAssignment &assignment) const¶
Evaluate the expression with respect to a given assignment.
- Parameters:
assignment – Assignment to evaluate the expression with.
- Returns:
Result of the evaluation.
-
class SymbolicException : public std::invalid_argument¶
-
template<typename T, typename = std::enable_if_t<std::is_constructible_v<int, T> && std::is_constructible_v<T, double>>>
class Term¶ - #include <Expression.hpp>
Struct representing a symbolic term. A term is a variable multiplied by a coefficient.
- Template Parameters:
T – Type of the coefficient. Must be constructible from an integer and a double.
Public Functions
-
inline Variable getVar() const noexcept¶
Get the variable of the term.
- Returns:
Variable of the term.
-
inline T getCoeff() const noexcept¶
Get the coefficient of the term.
- Returns:
Coefficient of the term.
-
inline bool hasZeroCoeff() const¶
Check whether the term has a zero coefficient.
- Returns:
True if the coefficient is zero, false otherwise.
-
inline explicit Term(const Variable v, T coef = 1.)¶
Construct a term with a given variable and coefficient.
- Parameters:
v – Variable of the term.
coef – Coefficient of the term.
-
inline void addCoeff(const T &rhs)¶
Add to the coefficient of the term.
- Parameters:
rhs – Value to add to the coefficient.
-
inline Term &operator*=(const T &rhs)¶
Multiply the term by a scalar (add coefficients).
- Parameters:
rhs – Scalar to multiply the term by.
- Returns:
Reference to the term.
-
inline Term &operator/=(const T &rhs)¶
Divide the term by a scalar (divide coefficients).
- Parameters:
rhs – Scalar to divide the term by.
- Returns:
Reference to the term.
-
inline Term &operator/=(const std::int64_t rhs)¶
Multiply the term by a scalar (add coefficients).
- Parameters:
rhs – Scalar to multiply the term by.
- Returns:
Reference to the term.
-
inline bool totalAssignment(const VariableAssignment &assignment) const¶
Check whether the Term’s variable is assigned in a given assignment.
- Parameters:
assignment – Assignment to check.
- Returns:
True if the variable is assigned, false otherwise.
-
inline double evaluate(const VariableAssignment &assignment) const¶
Evaluate the term with respect to a given assignment.
- Parameters:
assignment – Assignment to evaluate the term with.
- Returns:
Result of the evaluation.
-
struct Variable¶
- #include <Expression.hpp>
Struct representing a symbolic variable.
Variable names are assigned ids during creation. These ids are statically stored in a map. The name of a variable can be retrieved by its id. Variables are lexicographically ordered by their ids.
Public Functions
-
explicit Variable(const std::string &name)¶
Construct variable with given name.
- Parameters:
name – Name of the variable.
-
std::string getName() const noexcept¶
Get the name of the variable.
- Returns:
Name of the variable.
-
inline bool operator==(const Variable &rhs) const¶
Check whether this variable’s id is equal to another variable’s id.
- Parameters:
rhs – Variable to compare with.
- Returns:
True if the variables are equal, false otherwise.
-
inline bool operator!=(const Variable &rhs) const¶
Check whether this variable’s id is not equal to another variable’s id.
- Parameters:
rhs – Variable to compare with.
- Returns:
True if the variables are not equal, false otherwise.
-
inline bool operator<(const Variable &rhs) const¶
Check whether this variable’s id is less than another variable’s id with respect to the default lexicographic ordering.
- Parameters:
rhs – Variable to compare with.
- Returns:
True if this variable’s id is less than the other variable’s id, false otherwise.
-
inline bool operator>(const Variable &rhs) const¶
Check whether this variable’s id is greater than another variable’s id with respect to the default lexicographic ordering.
- Parameters:
rhs – Variable to compare with.
- Returns:
True if this variable’s id is greater than the other variable’s id, false otherwise.
-
explicit Variable(const std::string &name)¶