carl  24.04
Computer ARithmetic Library
GCD_Monomial.cpp
Go to the documentation of this file.
1 #include "GCD_Monomial.h"
2 
3 #include "../MonomialPool.h"
4 
5 namespace carl {
6 
7 Monomial::Arg gcd(const Monomial::Arg& lhs, const Monomial::Arg& rhs) {
8  if(!lhs && !rhs) return nullptr;
9  if(!lhs) return rhs;
10  if(!rhs) return lhs;
11 
12  CARL_LOG_FUNC("carl.core.monomial", lhs << ", " << rhs);
13  assert(lhs->is_consistent());
14  assert(rhs->is_consistent());
15 
16  Monomial::Content newExps;
17  std::size_t expsum = 0;
18  // Linear, as we expect small monomials.
19  auto itright = rhs->begin();
20  auto leftEnd = lhs->end();
21  auto rightEnd = rhs->end();
22  for(auto itleft = lhs->begin(); (itleft != leftEnd && itright != rightEnd);)
23  {
24  // Variable is present in both monomials.
25  if(itleft->first == itright->first)
26  {
27  std::size_t newExp = std::min(itleft->second, itright->second);
28  newExps.emplace_back(itleft->first, newExp);
29  expsum += newExp;
30  ++itright;
31  ++itleft;
32  }
33  else if(itleft->first < itright->first)
34  {
35  ++itleft;
36  }
37  else
38  {
39  assert(itleft->first > itright->first);
40  ++itright;
41  }
42  }
43  // Insert remaining part
44  std::shared_ptr<const Monomial> result;
45  if (!newExps.empty()) {
46  result = createMonomial(std::move(newExps), expsum);
47  }
48  CARL_LOG_TRACE("carl.core.monomial", "Result: " << result);
49  return result;
50 }
51 
52 }
#define CARL_LOG_FUNC(channel, args)
Definition: carl-logging.h:46
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
carl is the main namespace for the library.
Monomial::Arg createMonomial(T &&... t)
Definition: MonomialPool.h:168
cln::cl_I gcd(const cln::cl_I &a, const cln::cl_I &b)
Calculate the greatest common divisor of two integers.
Definition: operations.h:310
std::shared_ptr< const Monomial > Arg
Definition: Monomial.h:62
std::vector< std::pair< Variable, std::size_t > > Content
Definition: Monomial.h:63