7 #include "../Monomial.h"
18 template<
typename Coeff>
20 CARL_LOG_FUNC(
"carl.core.monomial", m <<
", " << substitutions);
22 for (
const auto& ve : m) {
23 auto it = substitutions.find(ve.first);
24 if (it == substitutions.end()) {
34 template<
typename Coeff>
40 auto it = substitutions.find(c.first);
41 if (it == substitutions.end()) {
54 template<
typename Coeff>
63 template<
typename C,
typename O,
typename P>
74 bool removedLast =
false;
75 for (
const auto& term: p) {
77 newTerms.push_back(term);
79 }
else removedLast =
true;
81 p.
terms().swap(newTerms);
82 CARL_LOG_TRACE(
"carl.core", p <<
" [ " << var <<
" -> " << value <<
" ] = " << p);
85 p.template makeMinimallyOrdered<false, true>();
87 assert(p.is_consistent());
93 std::map<exponent, std::pair<MultivariatePolynomial<C,O,P>,
size_t>> expResults;
94 size_t expectedResultSize = 0;
96 for(
const auto& term: p)
100 exponent e = term.monomial()->exponent_of_variable(var);
104 if(!iterBoolPair.second)
106 ++(iterBoolPair.first->second.second);
111 expectedResultSize += value.
nr_terms();
115 ++expectedResultSize;
120 ++expectedResultSize;
125 if( !expResults.empty() )
128 auto expResultA = expResults.begin();
130 auto expResultB = expResultA;
132 expResultB->second.first =
carl::pow(value, expResultB->first);
133 expectedResultSize += expResultB->second.second * expResultB->second.first.nr_terms();
135 while(expResultB != expResults.end())
138 expResultB->second.first = expResultA->second.first *
carl::pow(value, expResultB->first - expResultA->first);
139 expectedResultSize += expResultB->second.second * expResultB->second.first.nr_terms();
146 auto id = tam.getId(expectedResultSize);
147 for (
const auto& term: p)
149 if (term.monomial() ==
nullptr) {
150 tam.template addTerm<false>(
id, term);
152 exponent e = term.monomial()->exponent_of_variable(var);
154 if (e > 0) mon = term.monomial()->drop_variable(var);
156 for(
auto vterm : value)
158 if (mon ==
nullptr) tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), vterm.monomial()));
159 else if (vterm.monomial() ==
nullptr) tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), mon));
160 else tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), vterm.monomial() * mon));
163 auto iter = expResults.find(e);
164 assert(iter != expResults.end());
165 for(
auto vterm : iter->second.first)
167 if (mon ==
nullptr) tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), vterm.monomial()));
168 else if (vterm.monomial() ==
nullptr) tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), mon));
169 else tam.template addTerm<false>(
id,
Term<C>(vterm.coeff() * term.coeff(), vterm.monomial() * mon));
174 tam.template addTerm<false>(
id, term);
178 tam.readTerms(
id, p.
terms());
180 p.template makeMinimallyOrdered<false, true>();
181 assert(p.
nr_terms() <= expectedResultSize);
185 template<
typename C,
typename O,
typename P>
192 template<
typename C,
typename O,
typename P,
typename S>
194 static_assert(!std::is_same<S,
Term<C>>::value,
"Terms are handled by a separate method.");
198 for (
const auto& term: p) {
202 tam.template addTerm<false>(
id, resultTerm );
205 tam.readTerms(
id, result.
terms());
207 result.template makeMinimallyOrdered<false, true>();
212 template<
typename C,
typename O,
typename P>
217 for (
const auto& term: p) {
218 tam.template addTerm<false>(
id,
substitute(term, substitutions));
220 tam.readTerms(
id, result.
terms());
222 result.template makeMinimallyOrdered<false, true>();
227 template<
typename C,
typename O,
typename P>
236 for (
const auto& sub: substitutions) {
249 for(
const auto& term: result)
253 const Monomial& m = *(term.monomial());
257 CARL_LOG_TRACE(
"carl.core.monomial",
"Iterating: " << m[i].first);
258 if(m[i].second > 1 && substitutions.find(m[i].first) != substitutions.
end())
267 if(!expResults.empty())
269 auto expResultA = expResults.begin();
270 auto expResultB = expResultA;
271 auto sub = substitutions.begin();
272 while (sub->first != expResultB->first.first)
274 assert(sub != substitutions.end());
277 expResultB->second =
carl::pow(sub->second, expResultB->first.second);
279 while(expResultB != expResults.end())
281 if(expResultA->first.first != expResultB->first.first)
284 assert(sub != substitutions.end());
286 while (sub->first != expResultB->first.first)
288 assert(sub != substitutions.end());
291 assert(sub->first == expResultB->first.first);
292 expResultB->second =
pow(sub->second, expResultB->first.second);
296 expResultB->second = expResultA->second *
pow(sub->second, expResultB->first.second-expResultA->first.second);
304 for(
const auto& term: result)
309 const Monomial& m = *(term.monomial());
313 CARL_LOG_TRACE(
"carl.core.monomial",
"Iterating: " << m[i].first);
316 auto iter = substitutions.find(m[i].first);
317 if(iter != substitutions.end())
319 termResult *= iter->second;
323 termResult *= m[i].first;
328 auto iter = expResults.find(m[i]);
329 if(iter != expResults.end())
331 termResult *= iter->second;
340 resultB += termResult;
347 template<
typename Coeff>
354 if (value.has(var)) {
370 template<
typename Coeff>
383 CARL_LOG_TRACE(
"carl.core.uvpolynomial", p <<
" [ " << var <<
" -> " << value <<
" ] = " << res);
386 if (value.has(var)) {
393 for (std::size_t i = 0; i < res.size(); i++) {
398 CARL_LOG_TRACE(
"carl.core.uvpolynomial", p <<
" [ " << var <<
" -> " << value <<
" ] = " << resp);
408 template<
typename Rational>
412 template<
typename Poly,
typename Rational>
Implements utility functions concerning the (total) degree of monomials, terms and polynomials.
#define CARL_LOG_FUNC(channel, args)
#define CARL_LOG_TRACE(channel, msg)
carl is the main namespace for the library.
Coeff content(const UnivariatePolynomial< Coeff > &p)
The content of a polynomial is the gcd of the coefficients of the normal part of a polynomial.
Monomial::Arg createMonomial(T &&... t)
UnivariatePolynomial< C > to_univariate_polynomial(const MultivariatePolynomial< C, O, P > &p)
Convert a univariate polynomial that is currently (mis)represented by a 'MultivariatePolynomial' into...
bool is_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
std::size_t exponent
Type of an exponent.
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
typename UnderlyingNumberType< P >::type Coeff
Coeff substitute(const Monomial &m, const std::map< Variable, Coeff > &substitutions)
Applies the given substitutions to a monomial.
void substitute_inplace(MultivariateRoot< Poly > &mr, Variable var, const Poly &poly)
Create a copy of the underlying polynomial with the given variable replaced by the given polynomial.
Interval< Number > pow(const Interval< Number > &i, Integer exp)
A Variable represents an algebraic variable that can be used throughout carl.
This class represents a univariate polynomial with coefficients of an arbitrary type.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
Variable main_var() const
Retrieves the main variable of this polynomial.
bool is_consistent() const
Asserts that this polynomial over numeric coefficients complies with the requirements and assumptions...
void strip_leading_zeroes()
Coefficient evaluate(const Coefficient &value) const
The general-purpose multivariate polynomial class.
const TermsType & terms() const
bool is_consistent() const
Asserts that this polynomial complies with the requirements and assumptions for MultivariatePolynomia...
void reset_ordered() const
std::vector< Term< Coeff > > TermsType
Type our terms vector.f.
std::size_t nr_terms() const
Calculate the number of terms.
bool has(Variable v) const
States if a type is a number type.
The general-purpose monomials.
std::shared_ptr< const Monomial > Arg
exponents_it end()
Returns past-the-end iterator.
std::vector< std::pair< Variable, std::size_t > > Content
std::size_t num_variables() const
Returns the number of variables that occur in the monomial.
Coefficient & coeff()
Get the coefficient.
Monomial::Arg & monomial()
Get the monomial.