carl
24.04
Computer ARithmetic Library
|
The general-purpose monomials. More...
#include <Monomial.h>
Public Types | |
using | Arg = std::shared_ptr< const Monomial > |
using | Content = std::vector< std::pair< Variable, std::size_t > > |
Public Member Functions | |
~Monomial () | |
Monomial ()=delete | |
Default constructor. More... | |
Monomial (const Monomial &rhs)=delete | |
Monomial (Monomial &&rhs)=delete | |
exponents_it | begin () |
Returns iterator on first pair of variable and exponent. More... | |
exponents_cIt | begin () const |
Returns constant iterator on first pair of variable and exponent. More... | |
exponents_it | end () |
Returns past-the-end iterator. More... | |
exponents_cIt | end () const |
Returns past-the-end iterator. More... | |
std::size_t | hash () const |
Returns the hash of this monomial. More... | |
std::size_t | id () const |
Return the id of this monomial. More... | |
exponent | tdeg () const |
Gives the total degree, i.e. More... | |
const Content & | exponents () const |
bool | is_constant () const |
Checks whether the monomial is a constant. More... | |
bool | integer_valued () const |
bool | is_linear () const |
Checks whether the monomial has exactly degree one. More... | |
bool | isAtMostLinear () const |
Checks whether the monomial has at most degree one. More... | |
bool | is_square () const |
Checks whether the monomial is a square, i.e. More... | |
std::size_t | num_variables () const |
Returns the number of variables that occur in the monomial. More... | |
Variable | single_variable () const |
Retrieves the single variable of the monomial. More... | |
bool | has_no_other_variable (Variable v) const |
Checks that there is no other variable than the given one. More... | |
const std::pair< Variable, std::size_t > & | operator[] (std::size_t index) const |
Retrieves the given VarExpPair. More... | |
exponent | exponent_of_variable (Variable v) const |
Retrieves the exponent of the given variable. More... | |
bool | has (Variable v) const |
TODO: write code if binary search is preferred. More... | |
Monomial::Arg | drop_variable (Variable v) const |
For a monomial m = Prod( x_i^{e_i} ) * v^e, divides m by v^e. More... | |
bool | divide (Variable v, Monomial::Arg &res) const |
Divides the monomial by a variable v. More... | |
bool | divisible (const Monomial::Arg &m) const |
Checks if this monomial is divisible by the given monomial m. More... | |
bool | divide (const Monomial::Arg &m, Monomial::Arg &res) const |
Returns a new monomial that is this monomial divided by m. More... | |
Monomial::Arg | sqrt () const |
Calculates and returns the square root of this monomial, iff the monomial is a square as checked by is_square(). More... | |
bool | is_consistent () const |
Checks if the monomial is consistent. More... | |
Static Public Member Functions | |
static CompareResult | compareLexical (const Monomial::Arg &lhs, const Monomial::Arg &rhs) |
static CompareResult | compareLexical (const Monomial::Arg &lhs, Variable rhs) |
static CompareResult | compareGradedLexical (const Monomial::Arg &lhs, const Monomial::Arg &rhs) |
static CompareResult | compareGradedLexical (const Monomial::Arg &lhs, Variable rhs) |
static Monomial::Arg | lcm (const Monomial::Arg &lhs, const Monomial::Arg &rhs) |
Calculates the least common multiple of two monomial pointers. More... | |
static Monomial::Arg | calcLcmAndDivideBy (const Monomial::Arg &lhs, const Monomial::Arg &rhs) |
Returns lcm(lhs, rhs) / rhs. More... | |
static CompareResult | lexicalCompare (const Monomial &lhs, const Monomial &rhs) |
This method performs a lexical comparison as defined in [3], page 47. More... | |
static std::size_t | hashContent (const Monomial::Content &c) |
Calculate the hash of a monomial based on its content. More... | |
Private Types | |
using | exponents_it = Content::iterator |
using | exponents_cIt = Content::const_iterator |
Private Member Functions | |
void | calc_hash () |
Calculates the hash and stores it to mHash. More... | |
void | calc_total_degree () |
Calculates the total degree and stores it to mTotalDegree. More... | |
Monomial (Content &&content, std::size_t totalDegree=0) | |
Generate a monomial from a vector of variable-exponent pairs and a total degree. More... | |
Monomial (const Content &content, std::size_t totalDegree=0) | |
Generate a monomial from a vector of variable-exponent pairs and a total degree. More... | |
Private Attributes | |
Content | mExponents |
A vector of variable exponent pairs (v_i^e_i) with nonzero exponents. More... | |
std::size_t | mTotalDegree = 0 |
Some applications performance depends on getting the degree of monomials very fast. More... | |
std::size_t | mId = 0 |
Monomial id. More... | |
std::size_t | mHash = 0 |
Cached hash. More... | |
std::weak_ptr< const Monomial > | mWeakPtr |
Friends | |
class | MonomialPool |
The general-purpose monomials.
Notice that we aim to keep this object as small as possbible, while also limiting the use of expensive language features such as RTTI, exceptions and even polymorphism.
Although a Monomial can conceptually be seen as a map from variables to exponents, this implementation uses a vector of pairs of variables and exponents. Due to the fact that monomials usually contain only a small number of variables, the overhead introduced by std::map
makes up for the asymptotically slower std::find
on the std::vector
that is used.
Besides, many operations like multiplication, division or substitution do not rely on finding some variable, but must iterate over all entries anyway.
Definition at line 58 of file Monomial.h.
using carl::Monomial::Arg = std::shared_ptr<const Monomial> |
Definition at line 62 of file Monomial.h.
using carl::Monomial::Content = std::vector<std::pair<Variable, std::size_t> > |
Definition at line 63 of file Monomial.h.
|
private |
Definition at line 83 of file Monomial.h.
|
private |
Definition at line 82 of file Monomial.h.
carl::Monomial::~Monomial | ( | ) |
|
delete |
Default constructor.
|
delete |
|
delete |
|
inlineprivate |
Generate a monomial from a vector of variable-exponent pairs and a total degree.
If the totalDegree is zero, it is recalculated. The content is not expected to be sorted.
content | The variables and their exponents. |
totalDegree | The total degree of the monomial to generate, or zero. |
Definition at line 110 of file Monomial.h.
|
inlineprivate |
Generate a monomial from a vector of variable-exponent pairs and a total degree.
If the totalDegree is zero, it is recalculated. The content is not expected to be sorted.
content | The variables and their exponents |
totalDegree | The total degree of the monomial to generate, or zero. |
Definition at line 131 of file Monomial.h.
|
inline |
Returns iterator on first pair of variable and exponent.
Definition at line 140 of file Monomial.h.
|
inline |
Returns constant iterator on first pair of variable and exponent.
Definition at line 147 of file Monomial.h.
|
inlineprivate |
Calculates the hash and stores it to mHash.
Definition at line 90 of file Monomial.h.
|
inlineprivate |
Calculates the total degree and stores it to mTotalDegree.
Definition at line 96 of file Monomial.h.
|
inlinestatic |
Returns lcm(lhs, rhs) / rhs.
Definition at line 444 of file Monomial.h.
|
inlinestatic |
|
inlinestatic |
Definition at line 419 of file Monomial.h.
|
inlinestatic |
|
inlinestatic |
Definition at line 396 of file Monomial.h.
bool carl::Monomial::divide | ( | const Monomial::Arg & | m, |
Monomial::Arg & | res | ||
) | const |
Returns a new monomial that is this monomial divided by m.
Returns a pair of a monomial pointer and a bool. The bool indicates if the division was possible. The monomial pointer holds the result of the division. If the division resulted in an empty monomial (i.e. the two monomials were equal), the pointer is nullptr.
m | Monomial. |
res | Resulting monomial. |
Definition at line 68 of file Monomial.cpp.
bool carl::Monomial::divide | ( | Variable | v, |
Monomial::Arg & | res | ||
) | const |
Divides the monomial by a variable v.
If the division is impossible (because v does not occur in the monomial), nullptr is returned.
v | Variable |
res | Resulting monomial |
Definition at line 37 of file Monomial.cpp.
|
inline |
Checks if this monomial is divisible by the given monomial m.
m | Monomial. |
Definition at line 324 of file Monomial.h.
Monomial::Arg carl::Monomial::drop_variable | ( | Variable | v | ) | const |
For a monomial m = Prod( x_i^{e_i} ) * v^e, divides m by v^e.
Definition at line 17 of file Monomial.cpp.
|
inline |
Returns past-the-end iterator.
Definition at line 154 of file Monomial.h.
|
inline |
Retrieves the exponent of the given variable.
v | Variable. |
Definition at line 285 of file Monomial.h.
|
inline |
|
inline |
TODO: write code if binary search is preferred.
v | The variable to check for its occurrence. |
Definition at line 299 of file Monomial.h.
|
inline |
Checks that there is no other variable than the given one.
v | Variable. |
Definition at line 264 of file Monomial.h.
|
inline |
Returns the hash of this monomial.
Definition at line 169 of file Monomial.h.
|
inlinestatic |
Calculate the hash of a monomial based on its content.
c | Content of a monomial. |
Definition at line 466 of file Monomial.h.
|
inline |
Return the id of this monomial.
Definition at line 177 of file Monomial.h.
|
inline |
Definition at line 204 of file Monomial.h.
bool carl::Monomial::is_consistent | ( | ) | const |
Checks if the monomial is consistent.
Definition at line 207 of file Monomial.cpp.
|
inline |
Checks whether the monomial is a constant.
Definition at line 197 of file Monomial.h.
|
inline |
Checks whether the monomial has exactly degree one.
Definition at line 216 of file Monomial.h.
|
inline |
Checks whether the monomial is a square, i.e.
whether all exponents are even.
Definition at line 232 of file Monomial.h.
|
inline |
Checks whether the monomial has at most degree one.
Definition at line 224 of file Monomial.h.
|
static |
Calculates the least common multiple of two monomial pointers.
If both are valid objects, the lcm of both is calculated. If only one is a valid object, this one is returned. If both are invalid objects, an empty monomial is returned.
lhs | First monomial. |
rhs | Second monomial. |
Definition at line 151 of file Monomial.cpp.
|
static |
This method performs a lexical comparison as defined in [3], page 47.
We define the exponent vectors to be in decreasing order, i.e. the exponents of the larger variables first.
lhs | First monomial. |
rhs | Second monomial. |
Definition at line 231 of file Monomial.cpp.
|
inline |
Returns the number of variables that occur in the monomial.
Definition at line 245 of file Monomial.h.
|
inline |
Retrieves the given VarExpPair.
index | Index. |
Definition at line 276 of file Monomial.h.
|
inline |
Retrieves the single variable of the monomial.
Asserts that there is in fact only a single variable.
Definition at line 254 of file Monomial.h.
Monomial::Arg carl::Monomial::sqrt | ( | ) | const |
Calculates and returns the square root of this monomial, iff the monomial is a square as checked by is_square().
Otherwise, nullptr is returned.
Definition at line 141 of file Monomial.cpp.
|
inline |
Gives the total degree, i.e.
the sum of all exponents.
Definition at line 185 of file Monomial.h.
|
friend |
Definition at line 60 of file Monomial.h.
|
private |
A vector of variable exponent pairs (v_i^e_i) with nonzero exponents.
Definition at line 74 of file Monomial.h.
|
mutableprivate |
Cached hash.
Definition at line 80 of file Monomial.h.
|
mutableprivate |
Monomial id.
Definition at line 78 of file Monomial.h.
|
private |
Some applications performance depends on getting the degree of monomials very fast.
Definition at line 76 of file Monomial.h.
|
mutableprivate |
Definition at line 85 of file Monomial.h.