carl  24.04
Computer ARithmetic Library
PolynomialFactorizationPair.h
Go to the documentation of this file.
1 /*
2  * File: PolynomialFactorizationPair.h
3  * Author: Florian Corzilius
4  *
5  * Created on September 5, 2014, 3:57 PM
6  */
7 
8 #pragma once
9 
10 #include <map>
11 #include <mutex>
12 
14 #include <carl-arith/core/Common.h>
15 
16 namespace carl
17 {
18  template<typename P>
19  class FactorizedPolynomial;
20 
21  template<typename P>
22  class Factorization: public std::map<FactorizedPolynomial<P>, carl::exponent>
23  {
24  private:
25  using super = std::map<FactorizedPolynomial<P>, carl::exponent>;
26 
27  bool checkFactorization() const
28  {
29  for(auto f = super::begin(); f != super::end(); ++f )
30  {
31  if( !carl::is_one( f->first.coefficient() ) )
32  return false;
33  }
34  return true;
35  }
36  public:
37 
38  std::pair<typename super::iterator, bool> insert(typename super::const_iterator _hint, const std::pair<FactorizedPolynomial<P>, carl::exponent>& _val)
39  {
40  return insert( _hint, std::move(std::pair<FactorizedPolynomial<P>, carl::exponent>( _val.first, _val.second )) );
41  }
42 
43  typename super::iterator insert(typename super::const_iterator _hint, std::pair<FactorizedPolynomial<P>, carl::exponent>&& _val)
44  {
45  assert( super::find( _val.first ) == super::end() );
46  auto result = super::insert( _hint, std::move(_val) );
47  assert( checkFactorization() );
48  return result;
49  }
50 
51  std::pair<typename super::iterator, bool> insert(const std::pair<FactorizedPolynomial<P>, carl::exponent>& _val)
52  {
53  return insert( std::move(std::pair<FactorizedPolynomial<P>, carl::exponent>( _val.first, _val.second )) );
54  }
55 
56  std::pair<typename super::iterator, bool> insert(std::pair<FactorizedPolynomial<P>, carl::exponent>&& _val)
57  {
58  exponent e = _val.second;
59  std::pair<typename super::iterator, bool> insertResult = super::insert( std::move(_val) );
60  if ( !insertResult.second )
61  {
62  //Increment exponent for already existing factor
63  insertResult.first->second += e;
64  }
65  assert( checkFactorization() );
66  return insertResult;
67  }
68 
69  void insert( typename super::const_iterator _first, typename super::const_iterator _last )
70  {
71  if( super::empty() )
72  super::insert( _first, _last );
73  else
74  {
75  typename super::iterator lastInserted = super::begin();
76  while( _first != _last )
77  {
78  lastInserted = this->insert( lastInserted, *_first );
79  ++_first;
80  }
81  }
82  assert( checkFactorization() );
83  }
84  };
85 
86  template <typename P>
87  std::string factorizationToString( const Factorization<P>& _factorization, bool _infix = true, bool _friendlyVarNames = true );
88 
89  template <typename P>
90  std::ostream& operator<<( std::ostream& _out, const Factorization<P>& _factorization );
91 
92  template<typename P>
93  bool factorizationsEqual( const Factorization<P>& _factorizationA, const Factorization<P>& _factorizationB );
94 
95  template<typename P>
97  {
98  friend class FactorizedPolynomial<P>;
99 
100  private:
101  // Members
102 
103  /**
104  * The hash of this polynomial factorization pair.
105  */
106  mutable size_t mHash;
107 
108  /**
109  * A mutex for situation where any member is changed.
110  */
111  mutable std::recursive_mutex mMutex;
112 
113  /**
114  * A factorization (not necessarily the prime factorization) of the polynomial.
115  */
117 
118  /**
119  * A pointer to a polynomial. This pointer might be set to nullptr, if the factorization has not yet been expanded.
120  */
121  mutable P* mpPolynomial;
122 
123  /**
124  * Indicates, if the polynomial is irreducible
125  */
126  mutable int mIrreducible;
127 
128  template<typename P1>
130 
131  template<typename P1>
133 
134  /**
135  * Turn (possible) tree structure of factorization into linear list of factors.
136  * @return true, if the factorization has been changed;
137  * false, otherwise.
138  */
139  typename P::CoeffType flattenFactorization() const;
140 
141  bool checkFactorization() const;
142 
143  inline bool assertFactorization() const
144  {
145  return (mpPolynomial == nullptr || computePolynomial( *this ) == *mpPolynomial );
146  }
147 
148  /**
149  * Get the flattened factorization.
150  * @return The factorization of this polynomial factorization pair
151  */
153  {
154  return mFactorization;
155  }
156 
157  inline bool factorizedTrivially() const
158  {
159  assert( !mFactorization.empty() );
160  return this == &mFactorization.begin()->first.content();
161  }
162 
163  void gatherVariables( std::set<carl::Variable>& _vars ) const
164  {
165  if( mpPolynomial != nullptr )
166  {
167  mpPolynomial->gatherVariables( _vars );
168  }
169  else
170  {
171  for( auto const& factor : mFactorization )
172  {
173  factor.first.gatherVariables( _vars );
174  }
175  }
176  }
177 
178  /**
179  * Set new factorization for polynomial as two factors
180  * @param _fpolyA First polynomial.
181  * @param exponentA Exponent of first polynomial.
182  * @param _fpolyB Second polynomial.
183  * @param exponentB Exponent of second polynomial.
184  */
185  void setNewFactors( const FactorizedPolynomial<P>& _fpolyA, carl::exponent exponentA, const FactorizedPolynomial<P>& _fpolyB, carl::exponent exponentB ) const;
186 
187  bool isIrreducible() const;
188 
189  public:
190  // Constructor.
191  PolynomialFactorizationPair() = delete; // no implementation
192  /*
193  * @param _factorization The factorization. Every factor must be not constant.
194  * @param _polynomial Polynomial with Polynomial = Factorization * Coefficient
195  */
196  explicit PolynomialFactorizationPair( Factorization<P>&& _factorization, P* _polynomial = nullptr );
197  PolynomialFactorizationPair( const PolynomialFactorizationPair& ) = delete; // no implementation
199 
201 
202  /**
203  * @return The hash of this polynomial factorization pair.
204  */
205  size_t hash() const
206  {
207  return mHash;
208  }
209 
210  const auto& polynomial() const {
211  return *mpPolynomial;
212  }
213 
214  /**
215  * @param _polyFactA The first polynomial factorization pair to compare.
216  * @param _polyFactB The second polynomial factorization pair to compare.
217  * @return true, if the two given polynomial factorization pairs are equal.
218  */
219  template<typename P1>
220  friend bool operator==( const PolynomialFactorizationPair<P1>& _polyFactA, const PolynomialFactorizationPair<P1>& _polyFactB );
221 
222  /**
223  * @param _polyFactA The first polynomial factorization pair to compare.
224  * @param _polyFactB The second polynomial factorization pair to compare.
225  * @return true, if the first given polynomial factorization pair is less than the second given polynomial factorization pair.
226  */
227  template<typename P1>
228  friend bool operator<( const PolynomialFactorizationPair<P1>& _polyFactA, const PolynomialFactorizationPair<P1>& _polyFactB );
229 
230  /**
231  * @param _toUpdate The polynomial factorization pair to be checked for the possibility to be updated.
232  * @param _updateWith The polynomial factorization pair used to update the first given one.
233  * @return true, if the first polynomial factorization pair can be updated with the second one.
234  */
235  template<typename P1>
236  friend bool canBeUpdated( const PolynomialFactorizationPair<P1>& _toUpdate, const PolynomialFactorizationPair<P1>& _updateWith );
237 
238  /**
239  * Updates the first given polynomial factorization pair with the information stored in the second given polynomial factorization pair.
240  * @param _toUpdate The polynomial factorization pair to update with the second given one.
241  * @param _updateWith The polynomial factorization pair used to update the first given one.
242  */
243  template<typename P1>
245 
246  /**
247  * Calculates the factorization of the gcd of the polynomial represented by the two given polynomial factorization pairs.
248  * As a side effect the factorizations of these pairs can be refined. (c.f. Accelerating Parametric Probabilistic Verification, Algorithm 2)
249  * @param _pfPairA The first polynomial factorization pair to calculate the gcd with.
250  * @param _pfPairB The second polynomial factorization pair to calculate the gcd with.
251  * @param _restA The remaining factorization of the first polynomial without the gcd.
252  * @param _restB The remaining factorization of the second polynomial without the gcd.
253  * @param _coeff
254  * @param _pfPairARefined A bool which is set to true, if the factorization of the first given polynomial factorization pair has been refined.
255  * @param _pfPairBRefined A bool which is set to true, if the factorization of the second given polynomial factorization pair has been refined.
256  * @return The factorization of the gcd of the polynomial represented by the two given polynomial factorization pairs.
257  */
258  template<typename P1>
259  friend Factorization<P1> gcd( const PolynomialFactorizationPair<P1>& _pfPairA, const PolynomialFactorizationPair<P1>& _pfPairB, Factorization<P1>& _restA, Factorization<P1>& _restB, typename P1::CoeffType& _coeff, bool& _pfPairARefined, bool& _pfPairBRefined );
260 
261  /**
262  * @param _pfPair The polynomial to calculate the factorization for.
263  * @return A factorization of this factorized polynomial. (probably finer than the one factorization() returns)
264  */
265  template<typename P1>
266  friend Factors<FactorizedPolynomial<P1>> factor( const PolynomialFactorizationPair<P1>& _pfPair, const typename P1::CoeffType& );
267 
268  /**
269  * Prints the given polynomial-factorization pair on the given output stream.
270  * @param _out The stream to print on.
271  * @param _pfPair The polynomial-factorization pair to print.
272  * @return The output stream after inserting the output.
273  */
274  template <typename P1>
275  friend std::ostream& operator<<( std::ostream& _out, const PolynomialFactorizationPair<P1>& _pfPair );
276 
277  /**
278  * Updates the hash.
279  */
280  void rehash() const;
281 
282  };
283 
284  /**
285  * Compute the polynomial from the given polynomial-factorization pair.
286  * @param _fpPair A polynomial-factorization pair.
287  * @return The polynomial.
288  */
289  template<typename P>
291 } // namespace carl
292 
293 namespace std
294 {
295  template<typename P>
296  struct hash<carl::PolynomialFactorizationPair<P>>
297  {
299  {
300  return _pfp.hash();
301  }
302  };
303 } // namespace std
304 
carl is the main namespace for the library.
std::string factorizationToString(const Factorization< P > &_factorization, bool _infix=true, bool _friendlyVarNames=true)
std::size_t exponent
Type of an exponent.
Definition: Monomial.h:29
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool factorizationsEqual(const Factorization< P > &_factorizationA, const Factorization< P > &_factorizationB)
P computePolynomial(const FactorizedPolynomial< P > &_fpoly)
Obtains the polynomial (representation) of this factorized polynomial.
std::map< Pol, uint > Factors
Definition: Common.h:21
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
std::map< FactorizedPolynomial< P >, carl::exponent > super
std::pair< typename super::iterator, bool > insert(const std::pair< FactorizedPolynomial< P >, carl::exponent > &_val)
std::pair< typename super::iterator, bool > insert(std::pair< FactorizedPolynomial< P >, carl::exponent > &&_val)
std::pair< typename super::iterator, bool > insert(typename super::const_iterator _hint, const std::pair< FactorizedPolynomial< P >, carl::exponent > &_val)
super::iterator insert(typename super::const_iterator _hint, std::pair< FactorizedPolynomial< P >, carl::exponent > &&_val)
P::CoeffType flattenFactorization() const
Turn (possible) tree structure of factorization into linear list of factors.
PolynomialFactorizationPair(const PolynomialFactorizationPair &)=delete
friend P1 computePolynomial(const Factorization< P1 > &)
void rehash() const
Updates the hash.
std::recursive_mutex mMutex
A mutex for situation where any member is changed.
int mIrreducible
Indicates, if the polynomial is irreducible.
void gatherVariables(std::set< carl::Variable > &_vars) const
void setNewFactors(const FactorizedPolynomial< P > &_fpolyA, carl::exponent exponentA, const FactorizedPolynomial< P > &_fpolyB, carl::exponent exponentB) const
Set new factorization for polynomial as two factors.
friend bool operator==(const PolynomialFactorizationPair< P1 > &_polyFactA, const PolynomialFactorizationPair< P1 > &_polyFactB)
PolynomialFactorizationPair(Factorization< P > &&_factorization, P *_polynomial=nullptr)
friend P1 computePolynomial(const PolynomialFactorizationPair< P1 > &)
size_t mHash
The hash of this polynomial factorization pair.
PolynomialFactorizationPair & operator=(const PolynomialFactorizationPair &pfp)=default
friend std::ostream & operator<<(std::ostream &_out, const PolynomialFactorizationPair< P1 > &_pfPair)
Prints the given polynomial-factorization pair on the given output stream.
friend bool operator<(const PolynomialFactorizationPair< P1 > &_polyFactA, const PolynomialFactorizationPair< P1 > &_polyFactB)
friend bool canBeUpdated(const PolynomialFactorizationPair< P1 > &_toUpdate, const PolynomialFactorizationPair< P1 > &_updateWith)
P * mpPolynomial
A pointer to a polynomial.
friend Factors< FactorizedPolynomial< P1 > > factor(const PolynomialFactorizationPair< P1 > &_pfPair, const typename P1::CoeffType &)
friend void update(PolynomialFactorizationPair< P1 > &_toUpdate, PolynomialFactorizationPair< P1 > &_updateWith)
Updates the first given polynomial factorization pair with the information stored in the second given...
const Factorization< P > & factorization() const
Get the flattened factorization.
Factorization< P > mFactorization
A factorization (not necessarily the prime factorization) of the polynomial.
friend Factorization< P1 > gcd(const PolynomialFactorizationPair< P1 > &_pfPairA, const PolynomialFactorizationPair< P1 > &_pfPairB, Factorization< P1 > &_restA, Factorization< P1 > &_restB, typename P1::CoeffType &_coeff, bool &_pfPairARefined, bool &_pfPairBRefined)
Calculates the factorization of the gcd of the polynomial represented by the two given polynomial fac...
size_t operator()(const carl::PolynomialFactorizationPair< P > &_pfp) const