carl  24.04
Computer ARithmetic Library
FactorizedPolynomial.h
Go to the documentation of this file.
1 /*
2  * File: FactorizedPolynomial.h
3  * Author: Florian Corzilius
4  *
5  * Created on September 4, 2014, 10:55 AM
6  */
7 
8 #pragma once
9 
10 #include <iostream>
16 
17 namespace carl
18 {
19  template <typename P>
21 
22  template<typename P>
24  {
25  public:
26  template<typename P1>
27  friend Factorization<P1> gcd( const PolynomialFactorizationPair<P1>& _pfPairA, const PolynomialFactorizationPair<P1>& _pfPairB, Factorization<P1>& _restA, Factorization<P1>& _rest2B, bool& _pfPairARefined, bool& _pfPairBRefined );
28 
29  /// The ordering of the terms.
30  using OrderedBy = typename P::OrderedBy;
31  /// Type of the coefficients.
32  using CoeffType = typename P::CoeffType;
33  /// Type of the terms.
34  using TermType = typename P::TermType;
35  /// Type of the monomials within the terms.
36  using MonomType = typename P::MonomType;
37  /// Policies for this monomial.
38  using Policy = typename P::Policy;
39  /// Number type within the coefficients.
41  /// Integer type associated with the number type.
43  ///
44  using PolyType = P;
45  ///
46  using TermsType = typename P::TermsType;
47  ///
49 
50  enum ConstructorOperation : unsigned { ADD, SUB, MUL, DIV };
51 
52  private:
53  // Members
54 
55  /**
56  * The reference of the entry in the cache corresponding to this factorized polynomial.
57  */
58  mutable typename CACHE::Ref mCacheRef;
59 
60  /**
61  * The cache in which the actual content of this factorized polynomial is stored.
62  */
63  std::shared_ptr<CACHE> mpCache;
64 
65  /**
66  * Co-prime coefficient of the factorization
67  */
69 
70  /**
71  * Updates the hash of the entry in the cache corresponding to this factorized
72  * polynomial, which is also its hash.
73  */
74  void rehash() const
75  {
76  if( mpCache != nullptr )
77  mpCache->rehash( mCacheRef );
78  }
79 
80  void strengthenActivity() const
81  {
82  if( mpCache != nullptr )
83  mpCache->strengthenActivity( mCacheRef );
84  }
85 
86  template<typename P1>
87  friend bool existsFactorization( const FactorizedPolynomial<P1>& fpoly )
88  {
89  assert( fpoly.mpCache == nullptr || fpoly.mCacheRef != Cache<PolynomialFactorizationPair<P1>>::NO_REF );
90  return fpoly.mpCache != nullptr;
91  }
92 
93  #define ASSERT_CACHE_EQUAL( _cacheA, _cacheB ) assert( _cacheA == nullptr || _cacheB == nullptr || _cacheA == _cacheB )
94 
95  #define ASSERT_CACHE_REF_LEGAL( _fp ) assert( (_fp.pCache() == nullptr) == (_fp.cacheRef() == CACHE::NO_REF) )
96 
97  /**
98  * Computes the coefficient of the factorization and sets the coefficients of all factors to 1.
99  * @param _factorization The factorization.
100  * @return The coefficients of the whole factorization.
101  */
102  template<typename P1>
104 
105  /**
106  * Computes the common divisor with rest of two factorizations.
107  * @param _fFactorizationA The factorization of the first polynomial.
108  * @param _fFactorizationB The factorization of the second polynomial.
109  * @param _fFactorizationRestA Returns the remaining factorization of the first polynomial without the common divisor
110  * @param _fFactorizationRestB Returns the remaining factorization of the second polynomial without the common divisor
111  * @return The factorization of a common divisor of the two given factorized polynomials.
112  */
113  template<typename P1>
114  friend Factorization<P1> commonDivisor( const FactorizedPolynomial<P1>& _fFactorizationA, const FactorizedPolynomial<P1>& _fFactorizationB, Factorization<P1>& _fFactorizationRestA, Factorization<P1>& _fFactorizationRestB);
115 
116  /**
117  * Determines the greatest common divisor of the two given factorized polynomials. The method exploits the partial factorization
118  * stored in the arguments and refines it. (c.f. Accelerating Parametric Probabilistic Verification, Section 4)
119  * @param _fpolyA The first factorized polynomial to compute the greatest common divisor for.
120  * @param _fpolyB The second factorized polynomial to compute the greatest common divisor for.
121  * @param _fpolyRestA Returns the remaining part of the first factorized polynomial without the gcd.
122  * @param _fpolyRestB Returns the remaining part of the second factorized polynomial without the gcd.
123  * @return The greatest common divisor of the two given factorized polynomials.
124  */
125  template<typename P1>
127 
128  public:
129 
130  // Constructors.
132  explicit FactorizedPolynomial( const CoeffType& );
133  explicit FactorizedPolynomial( const P& _polynomial, const std::shared_ptr<CACHE>&, bool _polyNormalized = false );
136  explicit FactorizedPolynomial(const std::pair<ConstructorOperation, std::vector<FactorizedPolynomial>>& _p);
137  explicit FactorizedPolynomial( Factorization<P>&& _factorization, const CoeffType&, const std::shared_ptr<CACHE>& );
138 
139  // Destructor.
141 
142  /**
143  * Copies the given factorized polynomial.
144  * @param The factorized polynomial to copy.
145  * @return A reference to the copy of the given factorized polynomial.
146  */
148 
149  explicit operator PolyType() const
150  {
151  if( existsFactorization( *this ) )
152  return this->polynomial()*this->mCoefficient;
153  return PolyType( mCoefficient );
154  }
155 
156  /**
157  * @return The reference of the entry in the cache corresponding to this factorized polynomial.
158  */
159  typename CACHE::Ref cacheRef() const
160  {
161  return mCacheRef;
162  }
163 
164  /**
165  * @return The cache used by this factorized polynomial.
166  */
167  std::shared_ptr<CACHE> pCache() const
168  {
169  return mpCache;
170  }
171 
172  /**
173  * @return The cache used by this factorized polynomial.
174  */
175  CACHE& cache() const
176  {
177  return *mpCache;
178  }
179 
180  /**
181  * @return The entry in the cache corresponding to this factorized polynomial.
182  */
184  {
185  assert( existsFactorization( *this ) );
186  return mpCache->get( mCacheRef );
187  }
188 
189  /**
190  * @return The hash value of the entry in the cache corresponding to this factorized polynomial.
191  */
192  size_t hash() const
193  {
194  if( existsFactorization( *this ) )
195  {
196  return mpCache->get( mCacheRef ).hash();
197  }
198  return std::hash<CoeffType>()( mCoefficient );
199  }
200 
201  /**
202  * Set coefficient
203  * @param coeff Coefficient
204  */
205  //TODO make private and fix friend declaration
206  void setCoefficient( CoeffType coeff ) const
207  {
208  mCoefficient = coeff;
209  }
210 
211  /**
212  * @return The factorization of this polynomial.
213  */
215  {
216  assert( existsFactorization( *this ) );
217  //TODO (matthias) activate?
218  CoeffType c = content().flattenFactorization();
219  if( c != CoeffType( 0 ) )
220  {
221  mCoefficient *= c;
222  rehash();
223  }
224  return content().factorization();
225  }
226 
227  const P& polynomial() const
228  {
229  assert( existsFactorization( *this ) );
230  if( content().mpPolynomial == nullptr )
231  {
232  content().mpPolynomial = new P( computePolynomial( content().factorization() ) );
233  rehash();
234  }
235 
236  return *content().mpPolynomial;
237  }
238 
239  /**
240  * @return Coefficient of the polynomial.
241  */
242  const CoeffType& coefficient() const
243  {
244  return mCoefficient;
245  }
246 
248  {
249  if(existsFactorization(*this)) {
250  return this->mCoefficient * polynomial();
251  }
252  else {
253  return P(mCoefficient);
254  }
255  }
256 
257  /**
258  * @return true, if the factorized polynomial is constant.
259  */
260  bool is_constant() const
261  {
262  return mpCache == nullptr;
263  }
264 
265  /**
266  * @return true, if the factorized polynomial is one.
267  */
268  bool is_one() const
269  {
271  }
272 
273  /**
274  * @return true, if the factorized polynomial is zero.
275  */
276  bool is_zero() const
277  {
279  }
280 
281  /**
282  * Calculates the number of terms. (Note, that this requires to expand the factorization and, thus, can be expensive in the case that
283  * the factorization has not yet been expanded.)
284  * @return the number of terms
285  */
286  size_t nr_terms() const
287  {
288  return polynomial().nr_terms();
289  }
290 
291  /**
292  * @return A rough estimation of the size of this factorized polynomial. If it has already been expanded, the number of terms
293  * of the expanded form are returned; otherwise the number of terms in the factors.
294  */
295  size_t size() const
296  {
297  if( existsFactorization(*this) )
298  {
299  if( factorizedTrivially() )
300  return polynomial().size();
301  size_t result = 0;
302  for( const auto& factor : content().factorization() )
303  result += factor.first.size();
304  return result;
305  }
306  return 1;
307  }
308 
309  /**
310  * @return An approximation of the complexity of this polynomial.
311  */
312  size_t complexity() const
313  {
314  if( existsFactorization(*this) )
315  {
316  if( factorizedTrivially() )
317  return complexity(polynomial());
318  size_t result = 0;
319  for( const auto& factor : content().factorization() )
320  result += complexity(factor.first);
321  return result;
322  }
323  return 1;
324  }
325 
326  /**
327  * Checks if the polynomial is linear.
328  * @return If this is linear.
329  */
330  bool is_linear() const
331  {
332  if( existsFactorization(*this) )
333  {
334  if( factorizedTrivially() )
335  return polynomial().is_linear();
336  return false;
337  }
338  return true;
339  }
340 
341  /**
342  * @return The lcm of the denominators of the coefficients in p divided by the gcd of numerators
343  * of the coefficients in p.
344  */
345  template<typename C = CoeffType, EnableIf<is_subset_of_rationals_type<C>> = dummy>
347  {
349  }
350 
351  /**
352  * @return The lcm of the denominators of the coefficients (without the constant one) in p divided by the gcd of numerators
353  * of the coefficients in p.
354  */
355  template<typename C = CoeffType, EnableIf<is_subset_of_rationals_type<C>> = dummy>
357 
358  /**
359  * @return p * p.coprime_factor()
360  * @see coprime_factor()
361  */
363  {
364  if( existsFactorization(*this) )
365  {
366  FactorizedPolynomial<P> result( *this );
368  return result;
369  }
371  }
372 
373  /**
374  * @return true, if this factorized polynomial, has only itself as factor.
375  */
376  bool factorizedTrivially() const
377  {
378  return content().factorizedTrivially();
379  }
380 
381  /**
382  * Iterates through all factors and their terms to find variables occurring in this polynomial.
383  * @param vars Holds the variables occurring in the polynomial at return.
384  */
385  void gatherVariables( std::set<carl::Variable>& _vars ) const
386  {
387  if( mpCache == nullptr )
388  return;
389  content().gatherVariables( _vars );
390  }
391 
392  std::set<Variable> gatherVariables() const
393  {
394  std::set<Variable> vars;
395  gatherVariables(vars);
396  return vars;
397  }
398 
399  /**
400  * Retrieves the constant term of this polynomial or zero, if there is no constant term.
401  * @reiturn Constant term.
402  */
404 
405  /**
406  * Calculates the max. degree over all monomials occurring in the polynomial.
407  * As the degree of the zero polynomial is \f$-\infty\f$, we assert that this polynomial is not zero. This must be checked by the caller before calling this method.
408  * @see @cite GCL92, page 48
409  * @return Total degree.
410  */
411  size_t total_degree() const;
412 
413  /**
414  * Returns the coefficient of the leading term.
415  * Notice that this is not defined for zero polynomials.
416  * @return
417  */
418  CoeffType lcoeff() const;
419 
420  /**
421  * The leading term
422  * @return
423  */
424  TermType lterm() const;
425 
426  /**
427  * Gives the last term according to Ordering. Notice that if there is a constant part, it is always trailing.
428  * @return
429  */
431 
432  /**
433  * For terms with exactly one variable, get this variable.
434  * @return The only variable occuring in the term.
435  */
437  {
438  assert( existsFactorization( *this ) );
439  if( factorizedTrivially() )
440  return polynomial().single_variable();
441  return content().factorization().begin()->first.single_variable();
442  }
443 
444  /**
445  * Checks whether only one variable occurs.
446  * @return
447  * Notice that it might be better to use the variable information if several pieces of information are requested.
448  */
449  bool is_univariate() const;
450 
452  {
453  return std::move(polynomial().toUnivariatePolynomial() *= mCoefficient); // In this case it makes sense to expand the polynomial.
454  }
455 
457 
458  /**
459  * Checks if the polynomial has a constant term that is not zero.
460  * @return If there is a constant term unequal to zero.
461  */
462  bool has_constant_term() const;
463 
464  /**
465  * @param _var The variable to check for its occurrence.
466  * @return true, if the variable occurs in this term.
467  */
468  bool has( Variable _var ) const;
469 
470  template<bool gatherCoeff>
472 
473  template<bool gatherCoeff>
475  {
476  if( existsFactorization( *this ) )
477  {
478  VarsInfo<P> vi = carl::vars_info(polynomial(),false);
479  std::map<Variable, VarInfo<FactorizedPolynomial<P>>> resultVarInfos;
480  for( const auto& varViPair : vi )
481  {
482  const auto& varI = varViPair.second;
483  VarInfo<FactorizedPolynomial<P>> viFactorized( varI.max_degree(), varI.min_degree(), varI.num_occurences() );
484  resultVarInfos.insert( resultVarInfos.end(), std::make_pair( varViPair.first, std::move( viFactorized ) ) );
485  }
486  return VarsInfo<FactorizedPolynomial<P>>(std::move(resultVarInfos));
487  }
489  }
490 
492  {
493  if( existsFactorization( *this ) )
494  {
495  // TODO: Maybe we should use the factorization for collecting degrees and coefficients.
498  for( const auto& varViPair : vi )
499  {
500  const auto& varI = varViPair.second;
501  std::map<unsigned,FactorizedPolynomial<P>> coeffs;
502  for( const auto& expCoeffPair : varI.coeffs() )
503  {
504  if( expCoeffPair.second.is_constant() )
505  {
506  coeffs.insert( coeffs.end(), std::make_pair( expCoeffPair.first, FactorizedPolynomial<P>( expCoeffPair.second.constant_part() * mCoefficient ) ) );
507  }
508  else
509  {
510  coeffs.insert( coeffs.end(), std::make_pair( expCoeffPair.first, FactorizedPolynomial<P>( expCoeffPair.second, mpCache ) * mCoefficient ) );
511  }
512  }
513  VarInfo<FactorizedPolynomial<P>> viFactorized( varI.max_degree(), varI.min_degree(), varI.num_occurences(), std::move( coeffs ) );
514  result.insert( result.end(), std::make_pair( varViPair.first, std::move( viFactorized ) ) );
515  }
516  return result;
517  }
519  }
520 
521  /**
522  * Retrieves information about the definiteness of the polynomial.
523  * @return Definiteness of this.
524  */
525  Definiteness definiteness( bool _fullEffort = true ) const;
526 
527  /**
528  * Derivative of the factorized polynomial wrt variable x
529  * @param _var main variable
530  * @param _nth how often should derivative be applied
531  *
532  * @todo only _nth == 1 is supported
533  * @todo we do not use factorization currently
534  */
535  FactorizedPolynomial<P> derivative(const carl::Variable& _var, unsigned _nth = 1) const;
536 
537  /**
538  * Raise polynomial to the power
539  * @param _exp the exponent of the power
540  * @return p^exponent
541  *
542  * @todo uses multiplication -> bad idea.
543  */
544  FactorizedPolynomial<P> pow(unsigned _exp) const;
545 
546  /**
547  * Calculates the square of this factorized polynomial if it is a square.
548  * @param _result Used to store the result in.
549  * @return true, if this factorized polynomial is a square; false, otherwise.
550  */
551  bool sqrt( FactorizedPolynomial<P>& _result ) const;
552 
553  /**
554  * Divides the polynomial by the given coefficient.
555  * Applies if the coefficients are from a field.
556  * @param _divisor
557  * @return
558  */
559  template<typename C = CoeffType, EnableIf<is_field_type<C>> = dummy>
560  FactorizedPolynomial<P> divideBy( const CoeffType& _divisor ) const;
561 
562  /**
563  * Calculating the quotient and the remainder, such that for a given polynomial p we have
564  * p = _divisor * quotient + remainder.
565  * @param _divisor Another polynomial
566  * @return A divisionresult, holding the quotient and the remainder.
567  * @see
568  * @note Division is only defined on fields
569  */
571 
572  /**
573  * Divides the polynomial by another polynomial.
574  * If the divisor divides this polynomial, quotient contains the result of the division and true is returned.
575  * Otherwise, false is returned and the content of quotient remains unchanged.
576  * Applies if the coefficients are from a field.
577  * Note that the quotient must not be *this.
578  * @param _divisor
579  * @param _quotient
580  * @return
581  */
582  template<typename C = CoeffType, EnableIf<is_field_type<C>> = dummy>
583  bool divideBy( const FactorizedPolynomial<P>& _divisor, FactorizedPolynomial<P>& _quotient ) const;
584 
585  /**
586  * Choose a non-null cache from two caches.
587  * @param _pCacheA First cache.
588  * @param _pCacheB Second cache.
589  * @return A non-null cache.
590  */
591  static std::shared_ptr<CACHE> chooseCache( std::shared_ptr<CACHE> _pCacheA, std::shared_ptr<CACHE> _pCacheB )
592  {
593  if ( _pCacheA != nullptr )
594  return _pCacheA;
595  else
596  {
597  assert( _pCacheB != nullptr );
598  return _pCacheB;
599  }
600  }
601 
602  /**
603  * @param _fpoly The factorized polynomial to retrieve the expanded polynomial for.
604  * @return The polynomial (of the underlying polynomial type) when expanding the factorization of the given factorized polynomial.
605  */
606  template<typename P1>
607  friend P1 computePolynomial(const FactorizedPolynomial<P1>& _fpoly );
608 
609  /**
610  * @param _fpoly The operand.
611  * @return The given factorized polynomial times -1.
612  */
614 
615  /**
616  * @param _coef The summand to add this factorized polynomial with.
617  * @return This factorized polynomial after adding the given summand.
618  */
620 
621  /**
622  * @param _fpoly The summand to add this factorized polynomial with.
623  * @return This factorized polynomial after adding the given summand.
624  */
626 
627  /**
628  * @param _coef The number to subtract from this factorized polynomial.
629  * @return This factorized polynomial after subtracting the given number.
630  */
632 
633  /**
634  * @param _fpoly The factorized polynomial to subtract from this factorized polynomial.
635  * @return This factorized polynomial after adding the given factorized polynomial.
636  */
638 
639  /**
640  * @param _coef The factor to multiply this factorized polynomial with.
641  * @return This factorized polynomial after multiplying it with the given factor.
642  */
644 
645  /**
646  * @param _fpoly The factor to multiply this factorized polynomial with.
647  * @return This factorized polynomial after multiplying it with the given factor.
648  */
650 
651  /** Calculates the quotient. Notice: the divisor has to be a factor of the polynomial.
652  * @param _coef The divisor to divide this factorized polynomial with.
653  * @return This factorized polynomial after dividing it with the given divisor.
654  */
656 
657  /** Calculates the quotient. Notice: the divisor has to be a factor of the polynomial.
658  * @param _fpoly The divisor to divide this factorized polynomial with.
659  * @return This factorized polynomial after dividing it with the given divisor.
660  */
662 
663  /**
664  * Calculates the quotient. Notice: the divisor has to be a factor of the polynomial.
665  * @param _fdivisor The divisor
666  * @return The quotient
667  */
669 
670  /**
671  * @param _infix
672  * @param _friendlyVarNames
673  * @return
674  */
675  std::string toString( bool _infix = true, bool _friendlyVarNames = true ) const;
676 
677  /**
678  * Calculates the quotient of the polynomials. Notice: the second polynomial has to be a factor of the first polynomial.
679  * @param _fpolyA The dividend.
680  * @param _fpolyB The divisor.
681  * @return The quotient
682  */
683  template<typename P1>
685 
686  /**Computes the least common multiple of two given polynomials. The method refines the factorization.
687  * @param _fpolyA The first factorized polynomial to compute the lcm for.
688  * @param _fpolyB The second factorized polynomial to compute the lcm for.
689  * @return The lcm of the two given factorized polynomials.
690  */
691  template<typename P1>
693 
694  /**
695  * @param _fpolyA The first factorized polynomial to compute the common divisor for.
696  * @param _fpolyB The second factorized polynomial to compute the common divisor for.
697  * @return A common divisor of the two given factorized polynomials.
698  */
699  template<typename P1>
701 
702  /**
703  * @param _fpolyA The first factorized polynomial to compute the common multiple for.
704  * @param _fpolyB The second factorized polynomial to compute the common multiple for.
705  * @return A common multiple of the two given factorized polynomials.
706  */
707  template<typename P1>
709 
710  /**
711  * Determines the greatest common divisor of the two given factorized polynomials. The method exploits the partial factorization
712  * stored in the arguments and refines it. (c.f. Accelerating Parametric Probabilistic Verification, Section 4)
713  * @param _fpolyA The first factorized polynomial to compute the greatest common divisor for.
714  * @param _fpolyB The second factorized polynomial to compute the greatest common divisor for.
715  * @return The greatest common divisor of the two given factorized polynomials.
716  */
717  template<typename P1>
719 
720  /**
721  * Divides each of the two given factorized polynomials by their common factors of their (partial) factorization.
722  * @param _fpolyA The first factorized polynomial.
723  * @param _fpolyB The second factorized polynomial.
724  * @return The pair of the resulting factorized polynomials.
725  */
726  template<typename P1>
727  friend std::pair<FactorizedPolynomial<P1>,FactorizedPolynomial<P1>> lazyDiv( const FactorizedPolynomial<P1>& _fpolyA, const FactorizedPolynomial<P1>& _fpolyB );
728 
729  /**
730  * @param _fpoly The polynomial to calculate the factorization for.
731  * @return A factorization of this factorized polynomial. (probably finer than the one factorization() returns)
732  */
733  template<typename P1>
735 
736  // Operators which need to be friend.
737  template <typename P1>
739 
740  template <typename P1>
742 
743  template <typename P1>
745 
746  template <typename P1>
748 
749  template <typename P1>
751 
752  template <typename P1>
754  };
755 
756 /**
757  * @return true, if the factorized polynomial is one.
758  */
759 template <typename P>
761  return fp.is_one();
762 }
763 
764 /**
765  * @return true, if the factorized polynomial is zero.
766  */
767 template <typename P>
769  return fp.is_zero();
770 }
771 
772  /**
773  * Obtains the polynomial (representation) of this factorized polynomial. Note, that the result won't be stored
774  * in the factorized polynomial, hence, this method should only be called for debug purpose.
775  * @param _fpoly The factorized polynomial to get its polynomial (representation) for.
776  * @return The polynomial (representation) of this factorized polynomial
777  */
778  template<typename P>
780 
781  /**
782  * Prints the factorization representation of the given factorized polynomial on the given output stream.
783  * @param _out The stream to print on.
784  * @param _fpoly The factorized polynomial to print.
785  * @return The output stream after inserting the output.
786  */
787  template <typename P>
788  std::ostream& operator<<( std::ostream& _out, const FactorizedPolynomial<P>& _fpoly );
789 
790  template<typename P> struct needs_cache_type<FactorizedPolynomial<P>>: std::true_type {};
791 
792  template<typename P> struct is_factorized_type<FactorizedPolynomial<P>>: std::true_type {};
793 
794  /// @name Equality comparison operators
795  /// @{
796  /**
797  * Checks if the two arguments are equal.
798  * @param _lhs First argument.
799  * @param _rhs Second argument.
800  * @return `_lhs == _rhs`
801  */
802  template <typename P>
804 
805  template <typename P>
807 
808  template <typename P>
809  inline bool operator==(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
810  {
811  return _rhs == _lhs;
812  }
813  /// @}
814 
815  /// @name Inequality comparison operators
816  /// @{
817  /**
818  * Checks if the two arguments are not equal.
819  * @param _lhs First argument.
820  * @param _rhs Second argument.
821  * @return `_lhs != _rhs`
822  */
823  template <typename P>
824  inline bool operator!=(const FactorizedPolynomial<P>& _lhs, const FactorizedPolynomial<P>& _rhs);
825 
826  template <typename P>
827  inline bool operator!=(const FactorizedPolynomial<P>& _lhs, const typename FactorizedPolynomial<P>::CoeffType& _rhs)
828  {
829  return !(_lhs == _rhs);
830  }
831 
832  template <typename P>
833  inline bool operator!=(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
834  {
835  return !(_lhs == _rhs);
836  }
837  /// @}
838 
839  /// @name Less than comparison operators
840  /// @{
841  /**
842  * Checks if the first arguments is less than the second.
843  * @param _lhs First argument.
844  * @param _rhs Second argument.
845  * @return `_lhs < _rhs`
846  */
847  template <typename P>
849 
850  template <typename P>
852 
853  template <typename P>
854  inline bool operator<(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
855  {
856  return _rhs < _lhs;
857  }
858  /// @}
859 
860  /// @name Less or equal comparison operators
861  /// @{
862  /**
863  * Checks if the first arguments is less or equal than the second.
864  * @param _lhs First argument.
865  * @param _rhs Second argument.
866  * @return `_lhs <= _rhs`
867  */
868  template <typename P>
869  inline bool operator<=(const FactorizedPolynomial<P>& _lhs, const FactorizedPolynomial<P>& _rhs)
870  {
871  return !(_rhs < _lhs);
872  }
873 
874  template <typename P>
875  inline bool operator<=(const FactorizedPolynomial<P>& _lhs, const typename FactorizedPolynomial<P>::CoeffType& _rhs)
876  {
877  return !(_rhs < _lhs);
878  }
879 
880  template <typename P>
881  inline bool operator<=(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
882  {
883  return !(_rhs < _lhs);
884  }
885  /// @}
886 
887  /// @name Greater than comparison operators
888  /// @{
889  /**
890  * Checks if the first arguments is greater than the second.
891  * @param _lhs First argument.
892  * @param _rhs Second argument.
893  * @return `_lhs > _rhs`
894  */
895  template <typename P>
896  inline bool operator>(const FactorizedPolynomial<P>& _lhs, const FactorizedPolynomial<P>& _rhs)
897  {
898  return _rhs < _lhs;
899  }
900 
901  template <typename P>
902  inline bool operator>(const FactorizedPolynomial<P>& _lhs, const typename FactorizedPolynomial<P>::CoeffType& _rhs)
903  {
904  return _rhs < _lhs;
905  }
906 
907  template <typename P>
908  inline bool operator>(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
909  {
910  return _rhs < _lhs;
911  }
912  /// @}
913 
914  /// @name Greater or equal comparison operators
915  /// @{
916  /**
917  * Checks if the first arguments is greater or equal than the second.
918  * @param _lhs First argument.
919  * @param _rhs Second argument.
920  * @return `_lhs >= _rhs`
921  */
922  template <typename P>
923  inline bool operator>=(const FactorizedPolynomial<P>& _lhs, const FactorizedPolynomial<P>& _rhs)
924  {
925  return _rhs <= _lhs;
926  }
927 
928  template <typename P>
929  inline bool operator>=(const FactorizedPolynomial<P>& _lhs, const typename FactorizedPolynomial<P>::CoeffType& _rhs)
930  {
931  return _rhs <= _lhs;
932  }
933 
934  template <typename P>
935  inline bool operator>=(const typename FactorizedPolynomial<P>::CoeffType& _lhs, const FactorizedPolynomial<P>& _rhs)
936  {
937  return _rhs <= _lhs;
938  }
939  /// @}
940 
941  /// @name Addition operators
942  /// @{
943  /**
944  * Performs an addition involving a polynomial.
945  * @param _lhs First argument.
946  * @param _rhs Second argument.
947  * @return `_lhs + _rhs`
948  */
949  template <typename P>
951 
952  template <typename P>
954 
955  template <typename P>
957  {
958  return std::move(_rhs + _lhs);
959  }
960  /// @}
961 
962  /// @name Subtraction operators
963  /// @{
964  /**
965  * Performs an subtraction involving a polynomial.
966  * @param _lhs First argument.
967  * @param _rhs Second argument.
968  * @return `_lhs - _rhs`
969  */
970  template <typename P>
972 
973  template <typename P>
975 
976  template <typename P>
978  {
979  return std::move( -_rhs + _lhs );
980  }
981  /// @}
982 
983  /// @name Multiplication operators
984  /// @{
985  /**
986  * Perform a multiplication involving a polynomial.
987  * @param _lhs Left hand side.
988  * @param _rhs Right hand side.
989  * @return `_lhs * _rhs`
990  */
991  template <typename P>
993 
994  template <typename P>
996 
997  template <typename P>
999  {
1000  return std::move(_rhs * _lhs);
1001  }
1002 
1003  template <typename P>
1005  {
1006  return FactorizedPolynomial<P>(_lhs)/=_rhs;
1007  }
1008  /// @}
1009 
1010 } // namespace carl
1011 
1012 namespace std
1013 {
1014  template<typename P>
1015  struct hash<carl::FactorizedPolynomial<P>>
1016  {
1017  size_t operator()( const carl::FactorizedPolynomial<P>& _factPoly ) const
1018  {
1019  return _factPoly.hash();
1020  }
1021  };
1022 } // namespace std
1023 
1024 #include "FactorizedPolynomial.tpp"
carl is the main namespace for the library.
bool operator>(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
Interval< Number > operator/(const Interval< Number > &lhs, const Number &rhs)
Operator for the division of an interval and a number.
Definition: operators.h:453
Interval< Number > operator+(const Interval< Number > &lhs, const Interval< Number > &rhs)
Operator for the addition of two intervals.
Definition: operators.h:261
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
Interval< Number > operator*(const Interval< Number > &lhs, const Interval< Number > &rhs)
Operator for the multiplication of two intervals.
Definition: operators.h:386
typename UnderlyingNumberType< P >::type Coeff
Interval< Number > operator-(const Interval< Number > &rhs)
Unary minus.
Definition: operators.h:318
bool operator!=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator<=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator>=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
P computePolynomial(const FactorizedPolynomial< P > &_fpoly)
Obtains the polynomial (representation) of this factorized polynomial.
std::map< Pol, uint > Factors
Definition: Common.h:21
Definiteness
Regarding a polynomial as a function , its definiteness gives information about the codomain .
Definition: Definiteness.h:16
VarsInfo< MultivariatePolynomial< Coeff, Ordering, Policies > > vars_info(const MultivariatePolynomial< Coeff, Ordering, Policies > &poly, bool collect_coeff=false)
Definition: VarInfo.h:63
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
TermType
Definition: term.h:7
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
static const T & get()
Definition: constants.h:51
T type
A type associated with the type.
Definition: typetraits.h:77
This class represents a univariate polynomial with coefficients of an arbitrary type.
A strongly typed pair encoding the result of a division, being a quotient and a remainder.
Definition: Division.h:16
auto end()
Definition: VarInfo.h:105
std::size_t Ref
Definition: Cache.h:51
friend FactorizedPolynomial< P1 > quotient(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
Calculates the quotient of the polynomials.
void setCoefficient(CoeffType coeff) const
Set coefficient.
FactorizedPolynomial(const FactorizedPolynomial< P > &)
typename P::MonomType MonomType
Type of the monomials within the terms.
std::shared_ptr< CACHE > mpCache
The cache in which the actual content of this factorized polynomial is stored.
bool divideBy(const FactorizedPolynomial< P > &_divisor, FactorizedPolynomial< P > &_quotient) const
Divides the polynomial by another polynomial.
UnivariatePolynomial< FactorizedPolynomial< P > > toUnivariatePolynomial(Variable _var) const
FactorizedPolynomial< P > pow(unsigned _exp) const
Raise polynomial to the power.
FactorizedPolynomial< P > quotient(const FactorizedPolynomial< P > &_fdivisor) const
Calculates the quotient.
typename UnderlyingNumberType< CoeffType >::type NumberType
Number type within the coefficients.
friend FactorizedPolynomial< P1 > operator-(const FactorizedPolynomial< P1 > &_lhs, const typename FactorizedPolynomial< P1 >::CoeffType &_rhs)
static std::shared_ptr< CACHE > chooseCache(std::shared_ptr< CACHE > _pCacheA, std::shared_ptr< CACHE > _pCacheB)
Choose a non-null cache from two caches.
typename P::TermsType TermsType
std::shared_ptr< CACHE > pCache() const
FactorizedPolynomial< P > divideBy(const CoeffType &_divisor) const
Divides the polynomial by the given coefficient.
FactorizedPolynomial< P > & operator-=(const CoeffType &_coef)
const Factorization< P > & factorization() const
const CoeffType & coefficient() const
bool is_linear() const
Checks if the polynomial is linear.
FactorizedPolynomial< P > derivative(const carl::Variable &_var, unsigned _nth=1) const
Derivative of the factorized polynomial wrt variable x.
CoeffType lcoeff() const
Returns the coefficient of the leading term.
const PolynomialFactorizationPair< P > & content() const
FactorizedPolynomial(FactorizedPolynomial< P > &&)
CoeffType coprime_factor_without_constant() const
typename IntegralType< NumberType >::type IntNumberType
Integer type associated with the number type.
CoeffType mCoefficient
Co-prime coefficient of the factorization.
std::set< Variable > gatherVariables() const
TermType trailingTerm() const
Gives the last term according to Ordering.
friend Factorization< P1 > commonDivisor(const FactorizedPolynomial< P1 > &_fFactorizationA, const FactorizedPolynomial< P1 > &_fFactorizationB, Factorization< P1 > &_fFactorizationRestA, Factorization< P1 > &_fFactorizationRestB)
Computes the common divisor with rest of two factorizations.
bool has(Variable _var) const
Definiteness definiteness(bool _fullEffort=true) const
Retrieves information about the definiteness of the polynomial.
FactorizedPolynomial< P > & operator/=(const CoeffType &_coef)
Calculates the quotient.
FactorizedPolynomial(const P &_polynomial, const std::shared_ptr< CACHE > &, bool _polyNormalized=false)
FactorizedPolynomial< P > & operator/=(const FactorizedPolynomial< P > &_fpoly)
Calculates the quotient.
typename P::TermType TermType
Type of the terms.
friend FactorizedPolynomial< P1 > lcm(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
Computes the least common multiple of two given polynomials.
FactorizedPolynomial< P > & operator-=(const FactorizedPolynomial< P > &_fpoly)
friend FactorizedPolynomial< P1 > gcd(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB, FactorizedPolynomial< P1 > &_fpolyRestA, FactorizedPolynomial< P1 > &_fpolyRestB)
Determines the greatest common divisor of the two given factorized polynomials.
FactorizedPolynomial< P > & operator*=(const CoeffType &_coef)
friend Coeff< P1 > distributeCoefficients(Factorization< P1 > &_factorization)
Computes the coefficient of the factorization and sets the coefficients of all factors to 1.
FactorizedPolynomial< P > operator-() const
friend bool existsFactorization(const FactorizedPolynomial< P1 > &fpoly)
CACHE::Ref mCacheRef
The reference of the entry in the cache corresponding to this factorized polynomial.
friend P1 computePolynomial(const FactorizedPolynomial< P1 > &_fpoly)
friend FactorizedPolynomial< P1 > operator-(const FactorizedPolynomial< P1 > &_lhs, const FactorizedPolynomial< P1 > &_rhs)
typename P::CoeffType CoeffType
Type of the coefficients.
friend std::pair< FactorizedPolynomial< P1 >, FactorizedPolynomial< P1 > > lazyDiv(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
Divides each of the two given factorized polynomials by their common factors of their (partial) facto...
UnivariatePolynomial< CoeffType > toUnivariatePolynomial() const
bool sqrt(FactorizedPolynomial< P > &_result) const
Calculates the square of this factorized polynomial if it is a square.
FactorizedPolynomial(Factorization< P > &&_factorization, const CoeffType &, const std::shared_ptr< CACHE > &)
friend FactorizedPolynomial< P1 > operator*(const FactorizedPolynomial< P1 > &_lhs, const typename FactorizedPolynomial< P1 >::CoeffType &_rhs)
TermType lterm() const
The leading term.
CoeffType constant_part() const
Retrieves the constant term of this polynomial or zero, if there is no constant term.
FactorizedPolynomial< P > & operator+=(const FactorizedPolynomial< P > &_fpoly)
typename P::Policy Policy
Policies for this monomial.
friend FactorizedPolynomial< P1 > gcd(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
Determines the greatest common divisor of the two given factorized polynomials.
friend FactorizedPolynomial< P1 > operator*(const FactorizedPolynomial< P1 > &_lhs, const FactorizedPolynomial< P1 > &_rhs)
FactorizedPolynomial(const CoeffType &)
FactorizedPolynomial< P > & operator=(const FactorizedPolynomial< P > &)
Copies the given factorized polynomial.
FactorizedPolynomial(const std::pair< ConstructorOperation, std::vector< FactorizedPolynomial >> &_p)
friend FactorizedPolynomial< P1 > operator+(const FactorizedPolynomial< P1 > &_lhs, const FactorizedPolynomial< P1 > &_rhs)
VarsInfo< FactorizedPolynomial< P > > var_info() const
VarsInfo< FactorizedPolynomial< P > > var_info() const
size_t nr_terms() const
Calculates the number of terms.
bool is_univariate() const
Checks whether only one variable occurs.
friend FactorizedPolynomial< P1 > commonDivisor(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
Variable single_variable() const
For terms with exactly one variable, get this variable.
size_t total_degree() const
Calculates the max.
DivisionResult< FactorizedPolynomial< P > > divideBy(const FactorizedPolynomial< P > &_divisor) const
Calculating the quotient and the remainder, such that for a given polynomial p we have p = _divisor *...
friend Factors< FactorizedPolynomial< P1 > > factor(const FactorizedPolynomial< P1 > &_fpoly)
void rehash() const
Updates the hash of the entry in the cache corresponding to this factorized polynomial,...
typename P::OrderedBy OrderedBy
The ordering of the terms.
friend FactorizedPolynomial< P1 > commonMultiple(const FactorizedPolynomial< P1 > &_fpolyA, const FactorizedPolynomial< P1 > &_fpolyB)
std::string toString(bool _infix=true, bool _friendlyVarNames=true) const
friend Factorization< P1 > gcd(const PolynomialFactorizationPair< P1 > &_pfPairA, const PolynomialFactorizationPair< P1 > &_pfPairB, Factorization< P1 > &_restA, Factorization< P1 > &_rest2B, bool &_pfPairARefined, bool &_pfPairBRefined)
VarInfo< FactorizedPolynomial< P > > var_info(Variable _var) const
FactorizedPolynomial< P > coprime_coefficients() const
void gatherVariables(std::set< carl::Variable > &_vars) const
Iterates through all factors and their terms to find variables occurring in this polynomial.
bool has_constant_term() const
Checks if the polynomial has a constant term that is not zero.
friend FactorizedPolynomial< P1 > operator+(const FactorizedPolynomial< P1 > &_lhs, const typename FactorizedPolynomial< P1 >::CoeffType &_rhs)
FactorizedPolynomial< P > & operator*=(const FactorizedPolynomial< P > &_fpoly)
FactorizedPolynomial< P > & operator+=(const CoeffType &_coef)
size_t operator()(const carl::FactorizedPolynomial< P > &_factPoly) const