sigma  1.0.0
Loading...
Searching...
No Matches
monomial.hpp
Go to the documentation of this file.
1#pragma once
2#include <atomic>
3#include <map>
4#include <ostream>
5#include <sstream>
6#include <stdexcept>
7
11
12namespace sigma {
13
33class Monomial {
34public:
36 using size_type = std::size_t;
37
40
43
45 using exponents_t = std::map<variable_t, exponent_t>;
46
47 // --- Constructors and Assignment ----------------------------------------
48
56 Monomial() = default;
57
80 explicit Monomial(variable_t var, exponent_t exp = 1) {
81 if(exp == 0) {
82 throw std::invalid_argument(
83 "Monomial: exponent must be nonzero. Use the default "
84 "constructor for the constant monomial instead.");
85 }
86 m_exponents_[var] = exp;
87 }
88
108 for(auto&& [var, exp] : exponents) {
109 if(exp != 0) { m_exponents_[var] = exp; }
110 }
111 }
112
114 Monomial(const Monomial& other) = default;
115
117 Monomial(Monomial&& other) noexcept = default;
118
120 Monomial& operator=(const Monomial& other) = default;
121
123 Monomial& operator=(Monomial&& other) noexcept = default;
124
125 // -- State Accessors -----------------------------------------------------
126
133 const exponents_t& exponents() const noexcept { return m_exponents_; }
134
145 auto it = m_exponents_.find(var);
146 return it == m_exponents_.end() ? exponent_t{0} : it->second;
147 }
148
158 size_type degree() const noexcept {
159 size_type d = 0;
160 for(auto&& [var, exp] : m_exponents_) { d += exp; }
161 return d;
162 }
163
175 bool empty() const noexcept { return m_exponents_.empty(); }
176
177 // -- Arithmetic Operators ------------------------------------------------
178
199 Monomial& operator*=(const Monomial& other) {
200 for(auto&& [var, exp] : other.m_exponents_) {
201 m_exponents_[var] += exp;
202 }
203 return *this;
204 }
205
219 Monomial operator*(const Monomial& other) const {
220 return Monomial(*this) *= other;
221 }
222
223 // -- Comparison Operators ------------------------------------------------
224
237 bool operator==(const Monomial& other) const {
238 return m_exponents_ == other.m_exponents_;
239 }
240
250 bool operator!=(const Monomial& other) const { return !(*this == other); }
251
266 bool operator<(const Monomial& other) const {
267 return m_exponents_ < other.m_exponents_;
268 }
269
281 std::string print() const {
282 if(empty()) { return "1"; }
283 std::stringstream ss;
284 bool first = true;
285 for(auto&& [var, exp] : m_exponents_) {
286 if(!first) { ss << " * "; }
287 ss << "v" << var << "^" << exp;
288 first = false;
289 }
290 return ss.str();
291 }
292
296 static std::atomic<size_type> s_next_id{0};
297 return s_next_id.fetch_add(1);
298 }
299
300private:
303 exponents_t m_exponents_;
304};
305
306// -- Non-member functions -----------------------------------------------
307
322inline std::ostream& operator<<(std::ostream& os, const Monomial& m) {
323 os << m.print();
324 return os;
325}
326
327} // namespace sigma
328
329namespace std {
330
333template<>
334struct hash<sigma::Monomial> {
338 std::size_t operator()(const sigma::Monomial& m) const noexcept {
339 std::size_t seed = 0;
340 for(auto&& [var, exp] : m.exponents()) {
341 auto combine = [&seed](std::size_t value) {
342 // Same mixing constant/shifts as boost::hash_combine.
343 seed ^= std::hash<std::size_t>{}(value) + 0x9e3779b9 +
344 (seed << 6) + (seed >> 2);
345 };
346 combine(var);
347 combine(exp);
348 }
349 return seed;
350 }
351};
352
353} // namespace std
Represents a sparse multi-index exponent.
Definition monomial.hpp:33
exponent_t exponent(variable_t var) const
Returns the exponent of a single variable.
Definition monomial.hpp:144
bool operator==(const Monomial &other) const
Checks if *this and other represent the same monomial.
Definition monomial.hpp:237
Monomial(exponents_t exponents)
Constructs a monomial from a full map of exponents.
Definition monomial.hpp:107
Monomial()=default
Constructs the constant monomial.
size_type degree() const noexcept
Returns the total degree of the monomial.
Definition monomial.hpp:158
Monomial(const Monomial &other)=default
Makes a deep copy of other.
Monomial & operator=(const Monomial &other)=default
Assigns the value of other to this monomial.
std::map< variable_t, exponent_t > exponents_t
Type used to map variables to their exponents.
Definition monomial.hpp:45
Monomial & operator=(Monomial &&other) noexcept=default
Moves the value of other to this monomial.
Monomial(variable_t var, exponent_t exp=1)
Constructs a single-variable monomial.
Definition monomial.hpp:80
Monomial & operator*=(const Monomial &other)
Overwrites *this with the product of *this and other.
Definition monomial.hpp:199
Monomial(Monomial &&other) noexcept=default
Constructs a monomial by moving other.
size_type exponent_t
Type used to store the exponent of a single variable.
Definition monomial.hpp:42
std::ostream & operator<<(std::ostream &os, const Monomial &m)
Outputs a monomial to an output stream.
Definition monomial.hpp:322
bool operator<(const Monomial &other) const
Defines a strict weak ordering over monomials.
Definition monomial.hpp:266
bool empty() const noexcept
Checks if *this is the constant monomial.
Definition monomial.hpp:175
static variable_t make_variable()
Definition monomial.hpp:295
const exponents_t & exponents() const noexcept
Returns the exponents of the monomial.
Definition monomial.hpp:133
size_type variable_t
Opaque type used to identify an expansion variable.
Definition monomial.hpp:39
Monomial operator*(const Monomial &other) const
Returns the product of *this and other.
Definition monomial.hpp:219
std::string print() const
Creates a string representation of *this.
Definition monomial.hpp:281
bool operator!=(const Monomial &other) const
Checks if *this and other represent different monomials.
Definition monomial.hpp:250
std::size_t size_type
Type used for indexing, offsets, and total degree.
Definition monomial.hpp:36
The primary namespace for the sigma library.
Definition affine.hpp:12
Affine< T > exp(const Affine< T > &a)
Calculate the exponential of an affine form.
Definition exponents.ipp:31
std::size_t operator()(const sigma::Monomial &m) const noexcept
Does the hashing.
Definition monomial.hpp:338