00001
00002
00316
00317
00318 #ifndef BoolePolynomial_h_
00319 #define BoolePolynomial_h_
00320
00321
00322 #include <vector>
00323
00324
00325 #include <map>
00326
00327
00328 #include <algorithm>
00329
00330 #include "BooleRing.h"
00331
00332 #include "CDDInterface.h"
00333
00334
00335 #include "CTermIter.h"
00336 #include "CBidirectTermIter.h"
00337
00338 #include "pbori_func.h"
00339 #include "pbori_tags.h"
00340 #include "BooleSet.h"
00341
00342 #include "CTermIter.h"
00343 #include "BooleConstant.h"
00344
00345 BEGIN_NAMESPACE_PBORI
00346
00347
00348
00349 class LexOrder;
00350 class DegLexOrder;
00351 class DegRevLexAscOrder;
00352 class BlockDegLexOrder;
00353 class BlockDegRevLexAscOrder;
00354
00355 class BooleMonomial;
00356 class BooleVariable;
00357 class BooleExponent;
00358
00359
00360 template <class IteratorType, class MonomType>
00361 class CIndirectIter;
00362
00363 template <class IteratorType, class MonomType>
00364 class COrderedIter;
00365
00366
00367
00368 template<class, class, class, class> class CDelayedTermIter;
00369
00370 template<class OrderType, class NavigatorType, class MonomType>
00371 class CGenericIter;
00372
00373 template<class OrderType, class NavigatorType, class MonomType>
00374 class MyCGenericIter;
00375
00376
00377 template<class NavigatorType, class ExpType>
00378 class CExpIter;
00379
00380
00386 class BoolePolynomial;
00387 BoolePolynomial
00388 operator+(const BoolePolynomial& lhs, const BoolePolynomial& rhs);
00389
00390 class BoolePolynomial {
00391
00392 public:
00393
00395 friend class BooleMonomial;
00396
00397
00398
00399
00400
00402 typedef BoolePolynomial self;
00403
00405
00406 typedef CTypes::manager_type manager_type;
00407 typedef CTypes::manager_reference manager_reference;
00408 typedef CTypes::manager_ptr manager_ptr;
00409 typedef CTypes::dd_type dd_type;
00410 typedef CTypes::size_type size_type;
00411 typedef CTypes::idx_type idx_type;
00412 typedef CTypes::bool_type bool_type;
00413 typedef CTypes::ostream_type ostream_type;
00414 typedef CTypes::hash_type hash_type;
00416
00418 typedef dd_type::first_iterator first_iterator;
00419
00421 typedef dd_type::navigator navigator;
00422
00424 typedef dd_type::pretty_out_type pretty_out_type;
00425
00427 typedef dd_type::filename_type filename_type;
00428
00430
00432 typedef BooleMonomial monom_type;
00433
00435 typedef BooleVariable var_type;
00436
00438 typedef BooleExponent exp_type;
00439
00441 typedef BooleConstant constant_type;
00442
00444 typedef BooleRing ring_type;
00445
00447 typedef
00448 binary_composition< std::plus<size_type>,
00449 project_ith<1>, integral_constant<size_type, 1> >
00450 increment_type;
00451
00453 typedef
00454 binary_composition< std::minus<size_type>,
00455 project_ith<1>, integral_constant<size_type, 1> >
00456 decrement_type;
00457
00458
00459
00461
00462 typedef COrderedIter<navigator, exp_type> ordered_exp_iterator;
00463
00465
00466 typedef COrderedIter<navigator, monom_type> ordered_iterator;
00467
00469
00470 typedef CGenericIter<LexOrder, navigator, monom_type> lex_iterator;
00472 typedef CGenericIter<DegLexOrder, navigator, monom_type> dlex_iterator;
00473 typedef CGenericIter<DegRevLexAscOrder, navigator, monom_type>
00474 dp_asc_iterator;
00475
00476 typedef CGenericIter<BlockDegLexOrder, navigator, monom_type>
00477 block_dlex_iterator;
00478 typedef CGenericIter<BlockDegRevLexAscOrder, navigator, monom_type>
00479 block_dp_asc_iterator;
00480
00481 typedef CGenericIter<LexOrder, navigator, exp_type> lex_exp_iterator;
00482 typedef CGenericIter<DegLexOrder, navigator, exp_type> dlex_exp_iterator;
00483 typedef CGenericIter<DegRevLexAscOrder, navigator, exp_type>
00484 dp_asc_exp_iterator;
00485 typedef CGenericIter<BlockDegLexOrder, navigator, exp_type>
00486 block_dlex_exp_iterator;
00487 typedef CGenericIter<BlockDegRevLexAscOrder, navigator, exp_type>
00488 block_dp_asc_exp_iterator;
00490
00492 typedef lex_iterator const_iterator;
00493
00495 typedef CExpIter<navigator, exp_type> exp_iterator;
00496
00498 typedef CGenericIter<LexOrder, navigator, size_type> deg_iterator;
00499
00501 typedef std::vector<monom_type> termlist_type;
00502
00504 typedef dd_type::easy_equality_property easy_equality_property;
00505
00507 typedef BooleSet set_type;
00508
00510 typedef std::map<self, idx_type, symmetric_composition<
00511 std::less<navigator>, navigates<self> > > idx_map_type;
00512 typedef std::map<self, std::vector<self>, symmetric_composition<
00513 std::less<navigator>, navigates<self> > > poly_vec_map_type;
00514
00515
00516
00517
00518
00520 BoolePolynomial();
00521
00523 explicit BoolePolynomial(constant_type);
00524
00526 BoolePolynomial(constant_type isOne, const ring_type& ring):
00527 m_dd(isOne? ring.one(): ring.zero() ) { }
00528
00530 BoolePolynomial(const dd_type& rhs): m_dd(rhs) {}
00531
00533 BoolePolynomial(const set_type& rhs): m_dd(rhs.diagram()) {}
00534
00536 BoolePolynomial(const exp_type&, const ring_type&);
00537
00539 BoolePolynomial(const navigator& rhs, const ring_type& ring):
00540 m_dd(ring.manager().manager(), rhs) {
00541 assert(rhs.isValid());
00542 }
00543
00545 ~BoolePolynomial() {}
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 self& operator=(constant_type rhs) {
00556 return (*this) = self(rhs);
00557 }
00559
00560 const self& operator-() const { return *this; }
00561 self& operator+=(const self&);
00562 self& operator+=(constant_type rhs) {
00563
00564
00565 if (rhs) (*this) = (*this + ring().one());
00566 return *this;
00567 }
00568 template <class RHSType>
00569 self& operator-=(const RHSType& rhs) { return operator+=(rhs); }
00570 self& operator*=(const monom_type&);
00571 self& operator*=(const exp_type&);
00572 self& operator*=(const self&);
00573 self& operator*=(constant_type rhs) {
00574 if (!rhs) *this = ring().zero();
00575 return *this;
00576 }
00577 self& operator/=(const monom_type&);
00578 self& operator/=(const exp_type&);
00579 self& operator/=(const self& rhs);
00580 self& operator/=(constant_type rhs);
00581 self& operator%=(const monom_type&);
00582 self& operator%=(const self& rhs) {
00583 return (*this) -= (self(rhs) *= (self(*this) /= rhs));
00584 }
00585 self& operator%=(constant_type rhs) { return (*this) /= (!rhs); }
00587
00589
00590 bool_type operator==(const self& rhs) const { return (m_dd == rhs.m_dd); }
00591 bool_type operator!=(const self& rhs) const { return (m_dd != rhs.m_dd); }
00592 bool_type operator==(constant_type rhs) const {
00593 return ( rhs? isOne(): isZero() );
00594 }
00595 bool_type operator!=(constant_type rhs) const {
00596 return ( rhs? isZero(): isOne() );
00597 }
00599
00601 bool_type isZero() const { return m_dd.emptiness(); }
00602
00604 bool_type isOne() const { return m_dd.blankness(); }
00605
00607 bool_type isConstant() const { return m_dd.isConstant(); }
00608
00610 bool_type hasConstantPart() const { return m_dd.ownsOne(); }
00611
00613 bool_type reducibleBy(const self&) const;
00614
00616 monom_type lead() const;
00617
00619 monom_type lexLead() const;
00620
00622 monom_type boundedLead(size_type bound) const;
00623
00625 exp_type leadExp() const;
00626
00628 exp_type boundedLeadExp(size_type bound) const;
00629
00631 set_type lmDivisors() const { return leadFirst().firstDivisors(); };
00632
00634 hash_type hash() const { return m_dd.hash(); }
00635
00637 hash_type stableHash() const { return m_dd.stableHash(); }
00638
00640 hash_type lmStableHash() const;
00641
00643 size_type deg() const;
00644
00646 size_type lmDeg() const;
00647
00649 size_type lexLmDeg() const;
00650
00652 size_type totalDeg() const;
00653
00655 size_type lmTotalDeg() const;
00656
00658 self gradedPart(size_type deg) const;
00659
00661 size_type nNodes() const;
00662
00664 size_type nUsedVariables() const;
00665
00667 monom_type usedVariables() const;
00668
00670 exp_type usedVariablesExp() const;
00671
00673 size_type length() const;
00674
00676 ostream_type& print(ostream_type&) const;
00677
00679 void prettyPrint() const;
00680
00682 void prettyPrint(const char* filename) const;
00683
00685 const_iterator begin() const;
00686
00688 const_iterator end() const;
00689
00691 exp_iterator expBegin() const;
00692
00694 exp_iterator expEnd() const;
00695
00697 first_iterator firstBegin() const;
00698
00700 first_iterator firstEnd() const;
00701
00703 monom_type firstTerm() const;
00704
00706 deg_iterator degBegin() const;
00707
00709 deg_iterator degEnd() const;
00710
00712 ordered_iterator orderedBegin() const;
00713
00715 ordered_iterator orderedEnd() const;
00716
00718 ordered_exp_iterator orderedExpBegin() const;
00719
00721 ordered_exp_iterator orderedExpEnd() const;
00722
00724
00725 lex_iterator genericBegin(lex_tag) const;
00726 lex_iterator genericEnd(lex_tag) const;
00727 dlex_iterator genericBegin(dlex_tag) const;
00728 dlex_iterator genericEnd(dlex_tag) const;
00729 dp_asc_iterator genericBegin(dp_asc_tag) const;
00730 dp_asc_iterator genericEnd(dp_asc_tag) const;
00731 block_dlex_iterator genericBegin(block_dlex_tag) const;
00732 block_dlex_iterator genericEnd(block_dlex_tag) const;
00733 block_dp_asc_iterator genericBegin(block_dp_asc_tag) const;
00734 block_dp_asc_iterator genericEnd(block_dp_asc_tag) const;
00735
00736
00737 lex_exp_iterator genericExpBegin(lex_tag) const;
00738 lex_exp_iterator genericExpEnd(lex_tag) const;
00739 dlex_exp_iterator genericExpBegin(dlex_tag) const;
00740 dlex_exp_iterator genericExpEnd(dlex_tag) const;
00741 dp_asc_exp_iterator genericExpBegin(dp_asc_tag) const;
00742 dp_asc_exp_iterator genericExpEnd(dp_asc_tag) const;
00743 block_dlex_exp_iterator genericExpBegin(block_dlex_tag) const;
00744 block_dlex_exp_iterator genericExpEnd(block_dlex_tag) const;
00745 block_dp_asc_exp_iterator genericExpBegin(block_dp_asc_tag) const;
00746 block_dp_asc_exp_iterator genericExpEnd(block_dp_asc_tag) const;
00748
00750 navigator navigation() const { return m_dd.navigation(); }
00751
00753 navigator endOfNavigation() const { return navigator(); }
00754
00756 dd_type copyDiagram(){ return diagram(); }
00757
00759 operator set_type() const { return set(); };
00760
00761 size_type eliminationLength() const;
00762 size_type eliminationLengthWithDegBound(size_type garantied_deg_bound) const;
00764 void fetchTerms(termlist_type&) const;
00765
00767 termlist_type terms() const;
00768
00770 const dd_type& diagram() const { return m_dd; }
00771
00773 set_type set() const { return m_dd; }
00774
00776 bool_type isSingleton() const { return dd_is_singleton(navigation()); }
00777
00779 bool_type isSingletonOrPair() const {
00780 return dd_is_singleton_or_pair(navigation());
00781 }
00782
00784 bool_type isPair() const { return dd_is_pair(navigation()); }
00785
00787 ring_type ring() const { return ring_type(m_dd.manager()); }
00788
00789 protected:
00790
00792 dd_type& internalDiagram() { return m_dd; }
00793
00795 self leadFirst() const;
00796
00798 set_type firstDivisors() const;
00799
00800
00801 private:
00803 dd_type m_dd;
00804 };
00805
00806
00808 inline BoolePolynomial
00809 operator+(const BoolePolynomial& lhs, const BoolePolynomial& rhs) {
00810
00811 return BoolePolynomial(lhs) += rhs;
00812 }
00814 inline BoolePolynomial
00815 operator+(const BoolePolynomial& lhs, BooleConstant rhs) {
00816 return BoolePolynomial(lhs) += (rhs);
00817
00818 }
00819
00821 inline BoolePolynomial
00822 operator+(BooleConstant lhs, const BoolePolynomial& rhs) {
00823
00824 return BoolePolynomial(rhs) += (lhs);
00825 }
00826
00827
00829 template <class RHSType>
00830 inline BoolePolynomial
00831 operator-(const BoolePolynomial& lhs, const RHSType& rhs) {
00832
00833 return BoolePolynomial(lhs) -= rhs;
00834 }
00836 inline BoolePolynomial
00837 operator-(const BooleConstant& lhs, const BoolePolynomial& rhs) {
00838
00839 return -(BoolePolynomial(rhs) -= lhs);
00840 }
00841
00842
00844 #define PBORI_RHS_MULT(type) inline BoolePolynomial \
00845 operator*(const BoolePolynomial& lhs, const type& rhs) { \
00846 return BoolePolynomial(lhs) *= rhs; }
00847
00848 PBORI_RHS_MULT(BoolePolynomial)
00849 PBORI_RHS_MULT(BooleMonomial)
00850 PBORI_RHS_MULT(BooleExponent)
00851 PBORI_RHS_MULT(BooleConstant)
00852
00853
00854 #undef PBORI_RHS_MULT
00855
00857 #define PBORI_LHS_MULT(type) inline BoolePolynomial \
00858 operator*(const type& lhs, const BoolePolynomial& rhs) { return rhs * lhs; }
00859
00860 PBORI_LHS_MULT(BooleMonomial)
00861 PBORI_LHS_MULT(BooleExponent)
00862 PBORI_LHS_MULT(BooleConstant)
00863
00864 #undef PBORI_LHS_MULT
00865
00866
00868 template <class RHSType>
00869 inline BoolePolynomial
00870 operator/(const BoolePolynomial& lhs, const RHSType& rhs){
00871 return BoolePolynomial(lhs) /= rhs;
00872 }
00873
00875 template <class RHSType>
00876 inline BoolePolynomial
00877 operator%(const BoolePolynomial& lhs, const RHSType& rhs){
00878
00879 return BoolePolynomial(lhs) %= rhs;
00880 }
00881
00883 inline BoolePolynomial::bool_type
00884 operator==(BoolePolynomial::bool_type lhs, const BoolePolynomial& rhs) {
00885
00886 return (rhs == lhs);
00887 }
00888
00890 inline BoolePolynomial::bool_type
00891 operator!=(BoolePolynomial::bool_type lhs, const BoolePolynomial& rhs) {
00892
00893 return (rhs != lhs);
00894 }
00895
00897 BoolePolynomial::ostream_type&
00898 operator<<(BoolePolynomial::ostream_type&, const BoolePolynomial&);
00899
00900
00901 inline BoolePolynomial::bool_type
00902 BoolePolynomial::reducibleBy(const self& rhs) const {
00903
00904 PBORI_TRACE_FUNC( "BoolePolynomial::reducibleBy(const self&) const" );
00905
00906 if( rhs.isOne() )
00907 return true;
00908
00909 if( isZero() )
00910 return rhs.isZero();
00911
00912 return std::includes(firstBegin(), firstEnd(),
00913 rhs.firstBegin(), rhs.firstEnd());
00914
00915 }
00916
00917
00918 END_NAMESPACE_PBORI
00919
00920 #endif // of BoolePolynomial_h_