carl  24.04
Computer ARithmetic Library
Power.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../MultivariatePolynomial.h"
4 #include "../UnivariatePolynomial.h"
5 
6 namespace carl {
7 
8 /**
9  * Calculates the given power of a monomial m.
10  * @param m The monomial.
11  * @param exp Exponent.
12  * @return m to the power of exp.
13  */
14 inline Monomial::Arg pow(const Monomial& m, uint exp) {
15  if (exp == 0) {
16  return nullptr;
17  }
18  Monomial::Content newExps;
19  uint expsum = 0;
20  for (const auto& it: m.exponents()) {
21  newExps.emplace_back(it.first, uint(it.second * exp));
22  expsum += newExps.back().second;
23  }
24  return createMonomial(std::move(newExps), expsum);
25 }
26 inline Monomial::Arg pow(const Monomial::Arg& m, uint exp) {
27  if (m == nullptr) return nullptr;
28  return pow(*m, exp);
29 }
30 
31 template<typename Coeff>
33  if (t.monomial()) {
34  return Term<Coeff>(carl::pow(t.coeff(), exp), pow(*t.monomial(), exp));
35  } else {
36  return Term<Coeff>(carl::pow(t.coeff(), exp), nullptr);
37  }
38 }
39 
40 template<typename C, typename O, typename P>
42  //std::cout << "pw(" << *this << " ^ " << exp << ")" << std::endl;
45  if (exp == 1) return MultivariatePolynomial<C,O,P>(p);
46  if (exp == 2) return p*p;
49  while(exp > 0) {
50  res *= mult;
51  exp--;
52  }
53  return res;
54 }
55 
56 template<typename C, typename O, typename P>
58  if (exp == 0) {
60  }
61 
64  for (std::size_t i = 1; i < exp; i++) {
65  res *= p;
66  }
67  return res;
68 }
69 
70 /**
71  * Returns a polynomial to the given power.
72  * @param p The polynomial.
73  * @param exp Exponent.
74  * @return The polynomial to the power of exp.
75  */
76 template<typename Coeff>
82  while (exp > 0) {
83  if ((exp & 1) != 0) res *= mult;
84  exp /= 2;
85  if(exp > 0) mult = mult * mult;
86  }
87  return res;
88 }
89 
90 }
#define CARL_LOG_INEFFICIENT()
Definition: carl-logging.h:49
carl is the main namespace for the library.
Monomial::Arg createMonomial(T &&... t)
Definition: MonomialPool.h:168
std::uint64_t uint
Definition: numbers.h:16
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
MultivariatePolynomial< C, O, P > pow_naive(const MultivariatePolynomial< C, O, P > &p, std::size_t exp)
Definition: Power.h:57
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
This class represents a univariate polynomial with coefficients of an arbitrary type.
Variable main_var() const
Retrieves the main variable of this polynomial.
The general-purpose multivariate polynomial class.
The general-purpose monomials.
Definition: Monomial.h:59
std::shared_ptr< const Monomial > Arg
Definition: Monomial.h:62
std::vector< std::pair< Variable, std::size_t > > Content
Definition: Monomial.h:63
const Content & exponents() const
Definition: Monomial.h:189
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91