carl  24.04
Computer ARithmetic Library
substitution.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "FactorizedPolynomial.h"
4 
5 namespace carl {
6 
7 /**
8  * Replace the given variable by the given value.
9  * @return A new factorized polynomial resulting from this substitution.
10  */
11 template<typename P>
13  std::map<Variable, FactorizedPolynomial<P>> varFPolMap;
14  varFPolMap.insert(std::make_pair(var, value));
15  std::map<Variable, P> varPolMap;
16  varPolMap.insert(std::make_pair(var, value.polynomial()));
17  return carl::substitute(p, varFPolMap, varPolMap);
18 }
19 
20 /**
21  * Replace all variables by a value given in their map.
22  * @return A new factorized polynomial without the variables in map.
23  */
24 template<typename P>
26  std::map<Variable, P> varPolMap;
27  for (const auto& varFPolPair : substitutions) {
28  varPolMap.insert(varPolMap.end(), std::make_pair(varFPolPair.first, varFPolPair.second.polynomial()));
29  }
30  return carl::substitute(p, substitutions, varPolMap);
31 }
32 
33 /**
34  * Replace all variables by a value given in their map.
35  * @return A new factorized polynomial without the variables in map.
36  */
37 template<typename P>
38 FactorizedPolynomial<P> substitute(const FactorizedPolynomial<P>& p, const std::map<Variable, FactorizedPolynomial<P>>& substitutions, const std::map<Variable, P>& substitutionsAsP) {
39  if (!existsFactorization(p)) {
40  // Contains no variables but only coefficients
41  return p;
42  }
43  if (p.factorizedTrivially()) {
44  // Only has one factor
45  // Substitute in copy
46  P subResult = carl::substitute(p.polynomial(), substitutionsAsP);
47  if (subResult.is_constant()) {
48  FactorizedPolynomial<P> result(subResult.constant_part() * p.coefficient());
49  assert(computePolynomial(result) == substitute(computePolynomial(p),substitutionsAsP));
50  return std::move(result);
51  } else {
52  FactorizedPolynomial<P> result(std::move(subResult), p.pCache());
53  result *= p.coefficient();
54  assert(computePolynomial(result) == substitute(computePolynomial(p),substitutionsAsP));
55  return std::move(result);
56  }
57  } else {
58  P resultCoeff = p.coefficient();
59  Factorization<P> resultFactorization;
60  // Substitute in all factors
61  for (const auto& factor : p.factorization()) {
62  FactorizedPolynomial<P> subResult = substitute(factor.first,substitutions);
63  if (subResult.is_zero()) {
65  }
66  if (subResult.is_constant()) {
67  resultCoeff *= carl::pow(subResult.constant_part(), factor.second);
68  } else {
69  // Add substituted polynomial into factorization
70  resultFactorization.insert(std::pair<FactorizedPolynomial<P>, carl::exponent>(subResult, factor.second));
71  }
72  }
73  FactorizedPolynomial<P> result(std::move(resultFactorization), resultCoeff, p.pCache());
74  assert(computePolynomial(result) == substitute(computePolynomial(p),substitutionsAsP));
75  return std::move(result);
76  }
77 }
78 
79 /**
80  * Replace all variables by a value given in their map.
81  * @return A new factorized polynomial without the variables in map.
82  */
83 template<typename P, typename Subs>
84 FactorizedPolynomial<P> substitute(const FactorizedPolynomial<P>& p, const std::map<Variable, Subs>& substitutions) {
85  if (!existsFactorization(p)) {
86  // Contains no variables but only coefficients
87  return p;
88  }
89  if (p.factorizedTrivially()) {
90  // Only has one factor
91  P subResult = carl::substitute(p.polynomial(), substitutions);
92  if (subResult.is_constant()) {
93  FactorizedPolynomial<P> result(subResult.constant_part() * p.coefficient());
94  assert(computePolynomial(result) == carl::substitute(computePolynomial(p), substitutions));
95  return result;
96  } else {
97  FactorizedPolynomial<P> result(std::move(subResult), p.pCache());
98  result *= p.coefficient();
99  assert(computePolynomial(result) == carl::substitute(computePolynomial(p), substitutions));
100  return result;
101  }
102  } else {
103  auto resultCoeff = p.coefficient();
104  Factorization<P> resultFactorization;
105  // Substitute in all factors
106  for (const auto& factor : p.factorization()) {
107  FactorizedPolynomial<P> subResult = carl::substitute(factor.first, substitutions);
108  if (subResult.is_zero())
110  if (subResult.is_constant()) {
111  resultCoeff *= carl::pow(subResult.constant_part(), factor.second);
112  } else {
113  // Add substituted polynomial into factorization
114  resultFactorization.insert(std::pair<FactorizedPolynomial<P>, carl::exponent>(subResult, factor.second));
115  }
116  }
117  FactorizedPolynomial<P> result(std::move(resultFactorization), resultCoeff, p.pCache());
118  assert(computePolynomial(result) == carl::substitute(computePolynomial(p), substitutions));
119  return result;
120  }
121 }
122 
123 
124 }
carl is the main namespace for the library.
std::size_t exponent
Type of an exponent.
Definition: Monomial.h:29
Coeff substitute(const Monomial &m, const std::map< Variable, Coeff > &substitutions)
Applies the given substitutions to a monomial.
Definition: Substitution.h:19
P computePolynomial(const FactorizedPolynomial< P > &_fpoly)
Obtains the polynomial (representation) of this factorized polynomial.
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
std::shared_ptr< CACHE > pCache() const
const Factorization< P > & factorization() const
const CoeffType & coefficient() const
CoeffType constant_part() const
Retrieves the constant term of this polynomial or zero, if there is no constant term.
std::pair< typename super::iterator, bool > insert(typename super::const_iterator _hint, const std::pair< FactorizedPolynomial< P >, carl::exponent > &_val)