carl
24.04
Computer ARithmetic Library
Monomial_Create.cpp
Go to the documentation of this file.
1
#include <cassert>
2
3
#include <
carl-arith/poly/umvpoly/Monomial.h
>
4
#include <
carl-arith/poly/umvpoly/MonomialPool.h
>
5
6
int
main
() {
7
/*
8
* A carl::Monomial object represents a monomial, being the product of
9
* multiple variables. Our representation consists of a list of (unique)
10
* carl::Variable objects, each associated with an exponent.
11
*
12
* Internally, a carl::Monomial stores the actual monomial as
13
* std::vector<std::pair<carl::Variable,unsigned>>
14
* and, for caching purposes, the following data:
15
* - the total degree, that is the sum of all exponents,
16
* - the id, a unique identifier managed by the carl::MonomialPool and
17
* - the hash of the monomial.
18
*/
19
carl::Variable
x =
carl::fresh_real_variable
(
"x"
);
20
carl::Variable
y =
carl::fresh_real_variable
(
"y"
);
21
22
/*
23
* However, carl::Monomial objects are managed by a carl::MonomialPool,
24
* though the carl::MonomialPool seldomly accessed directly.
25
* This means, that there is only a single instance of every monomial and
26
* all objects that use this monomial have a std::shared_ptr of this
27
* instance. To obtain this std::shared_ptr, use carl::createMonomial
28
* instead of the normal constructor.
29
* The type of the std::shared_ptr is defined by carl::Monomial::Arg.
30
*/
31
32
auto
a =
carl::createMonomial
(x, (
carl::exponent
)2);
33
auto
b =
carl::createMonomial
(std::initializer_list<std::pair<carl::Variable, carl::exponent>>({std::make_pair(x,(
carl::exponent
)3), std::make_pair(y, (
carl::exponent
)2)}), (
carl::exponent
)5);
34
35
assert(a == x*x);
36
assert(b == x*x*x * y*y);
37
38
/*
39
* As you see, the creation of monomials with multiple variables is somewhat
40
* clumsy, but in the above assertions a simpler way is already used.
41
* Two variables can be multiplied, resulting in a monomial. Monomials can
42
* also be multiplied, resulting in a new monomial.
43
*/
44
auto
c = x*x*y;
45
auto
d = x*y;
46
auto
e = c*d;
47
48
assert(e == b);
49
}
Monomial.h
MonomialPool.h
main
int main()
Definition:
Monomial_Create.cpp:6
carl::createMonomial
Monomial::Arg createMonomial(T &&... t)
Definition:
MonomialPool.h:168
carl::exponent
std::size_t exponent
Type of an exponent.
Definition:
Monomial.h:29
carl::fresh_real_variable
Variable fresh_real_variable() noexcept
Definition:
VariablePool.h:198
carl::Variable
A Variable represents an algebraic variable that can be used throughout carl.
Definition:
Variable.h:85
examples
tutorial
Monomial_Create.cpp
Generated by
1.9.1