carl  24.04
Computer ARithmetic Library
RootElimination.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Degree.h"
4 #include "Evaluation.h"
5 #include "../UnivariatePolynomial.h"
6 
7 namespace carl {
8 
9 /**
10  * Reduces the given polynomial such that zero is not a root anymore.
11  * Is functionally equivalent to eliminate_root(0), but faster.
12  */
13 template<typename Coeff>
15  std::size_t i = 0;
16  while ((i < p.coefficients().size()) && (is_zero(p.coefficients()[i]))) i++;
17  if (i == 0) return;
18  // Now shift by i elements, drop lower i coefficients (they are zero anyway)
19  for (std::size_t j = 0; j < p.coefficients().size()-i; j++) {
20  p.coefficients()[j] = p.coefficients()[j+i];
21  }
22  p.coefficients().resize(p.coefficients().size()-i);
23  assert(p.is_consistent());
24 }
25 
26 /**
27  * Reduces the polynomial such that the given root is not a root anymore.
28  * The reduction is achieved by removing the linear factor (mainVar - root) from the polynomial, possibly multiple times.
29  *
30  * This method assumes that the given root is an actual real root of this polynomial.
31  * If this is not the case, i.e. <code>evaluate(root) != 0</code>, the polynomial will contain meaningless garbage.
32  * @param p The polynomial.
33  * @param root Root to be eliminated.
34  */
35 template<typename Coeff>
37  assert(is_root_of(p, root));
38  if (carl::is_zero(p)) return;
39  if (is_zero(root)) {
41  return;
42  }
43  do {
44  std::vector<Coeff> tmp(p.coefficients().size()-1);
45  for (std::size_t i = p.coefficients().size()-1; i > 0; i--) {
46  tmp[i-1] = p.coefficients()[i];
47  p.coefficients()[i-1] += p.coefficients()[i] * root;
48  }
49  p.coefficients() = tmp;
50  } while ((is_zero(evaluate(p, root))) && (p.coefficients().size() > 0));
51 }
52 
53 }
Implements utility functions concerning the (total) degree of monomials, terms and polynomials.
carl is the main namespace for the library.
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
bool is_root_of(const UnivariatePolynomial< Coeff > &p, const Coeff &value)
Definition: Evaluation.h:62
typename UnderlyingNumberType< P >::type Coeff
void eliminate_root(UnivariatePolynomial< Coeff > &p, const Coeff &root)
Reduces the polynomial such that the given root is not a root anymore.
void eliminate_zero_root(UnivariatePolynomial< Coeff > &p)
Reduces the given polynomial such that zero is not a root anymore.
This class represents a univariate polynomial with coefficients of an arbitrary type.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
bool is_consistent() const
Asserts that this polynomial over numeric coefficients complies with the requirements and assumptions...