carl  24.04
Computer ARithmetic Library
to_univariate_polynomial.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../MultivariatePolynomial.h"
4 #include "../UnivariatePolynomial.h"
6 
7 namespace carl {
8 
9 /**
10  * Convert a univariate polynomial that is currently (mis)represented by a
11  * 'MultivariatePolynomial' into a more appropiate 'UnivariatePolynomial'
12  * representation.
13  * Note that the current polynomial must mention one and only one variable,
14  * i.e., be indeed univariate.
15  */
16 template<typename C, typename O, typename P>
18  // Only correct when it is already only in one variable.
19  assert(variables(p).size() == 1);
20  Variable::Arg x = p.lmon()->single_variable();
21  std::vector<C> coeffs(p.total_degree()+1,0);
22  for (const auto& t : p)
23  {
24  coeffs[t.tdeg()] = t.coeff();
25  }
26  return UnivariatePolynomial<C>(x, coeffs);
27 }
28 
29 /**
30  * Convert a multivariate polynomial that is currently represented by a
31  * MultivariatePolynomial into a UnivariatePolynomial representation.
32  * The main variable of the resulting polynomial is given as second argument.
33  */
34 template<typename C, typename O, typename P>
36  assert(p.is_consistent());
37  std::vector<MultivariatePolynomial<C,O,P>> coeffs(1);
38  for (const auto& term: p) {
39  if (term.monomial() == nullptr) coeffs[0] += term;
40  else {
41  const auto& mon = term.monomial();
42  auto exponent = mon->exponent_of_variable(v);
43  if (exponent >= coeffs.size()) {
44  coeffs.resize(exponent + 1);
45  }
46  std::shared_ptr<const carl::Monomial> tmp = mon->drop_variable(v);
47  coeffs[exponent] += term.coeff() * tmp;
48  }
49  }
50  // Convert result back to MultivariatePolynomial and check that the result is equal to p
53 }
54 
55 }
carl is the main namespace for the library.
UnivariatePolynomial< C > to_univariate_polynomial(const MultivariatePolynomial< C, O, P > &p)
Convert a univariate polynomial that is currently (mis)represented by a 'MultivariatePolynomial' into...
std::size_t exponent
Type of an exponent.
Definition: Monomial.h:29
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
This class represents a univariate polynomial with coefficients of an arbitrary type.
The general-purpose multivariate polynomial class.
bool is_consistent() const
Asserts that this polynomial complies with the requirements and assumptions for MultivariatePolynomia...
const Monomial::Arg & lmon() const
The leading monomial.
std::size_t total_degree() const
Calculates the max.