PolyBoRi
BooleMonomial.h
Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //*****************************************************************************
00016 //*****************************************************************************
00017 
00018 #ifndef BooleMonomial_h_
00019 #define BooleMonomial_h_
00020 
00021 // include basic definitions
00022 #include "pbori_defs.h"
00023 
00024 // get definition of BoolePolynomial and BooleVariable
00025 #include "BoolePolynomial.h"
00026 #include "BooleVariable.h"
00027 // get standard map functionality
00028 #include <map>
00029 
00030 // get variable iterator
00031 #include "CVariableIter.h"
00032 
00033 // get variable iterator
00034 #include "PBoRiError.h"
00035 
00036 BEGIN_NAMESPACE_PBORI
00037 
00038 class BooleVariable;
00039 class BooleExponent;
00040 template <class DDType, class MonomType> class CDDOperations;
00041 
00050 class BooleMonomial:
00051   public CAuxTypes {
00052 
00054   typedef BooleMonomial self;
00055 
00056 public:
00057   template <class, class> friend class CDDOperations;
00058   friend class COrderingBase;
00059   template <class> friend class CTermGeneratorBase;
00060   template <class, class> friend class CTermGeneratorBase__;
00061 
00063   typedef BoolePolynomial poly_type;
00064 
00066 
00067   typedef poly_type::dd_type dd_type;
00068   typedef poly_type::integer_type integer_type;
00069   typedef poly_type::ostream_type ostream_type;
00071 
00073   typedef poly_type::var_type var_type;
00074 
00076   typedef poly_type::constant_type constant_type;
00077 
00079   typedef poly_type::set_type set_type;
00080 
00082   typedef poly_type::exp_type exp_type;
00083 
00085   typedef poly_type::ring_type ring_type;
00086 
00088   typedef poly_type::first_iterator const_iterator;
00089 
00091   typedef CVariableIter<const_iterator, var_type> variable_iterator;
00092 
00094   //  typedef generate_index_map<self>::type idx_map_type;
00095 
00096   typedef std::map<self, idx_type, symmetric_composition<
00097     std::less<poly_type::navigator>, 
00098     navigates<poly_type> > > idx_map_type;
00099 
00101   typedef dd_type::easy_equality_property easy_equality_property;
00102 
00104   BooleMonomial():
00105     m_poly( BooleEnv::one() )  { }
00106 
00108   BooleMonomial(const self& rhs):  
00109     m_poly(rhs.m_poly) {}
00110 
00112   BooleMonomial(const var_type& rhs);  // not inlined to avoid dependency loop
00113                                        // (both depend on poly_type)
00114 
00116   BooleMonomial(const exp_type& rhs, const ring_type& ring): 
00117     m_poly(rhs, ring) { }
00118 
00120   BooleMonomial(const ring_type& ring): 
00121     m_poly(ring.one()) {}
00122 
00124   ~BooleMonomial() {}
00125 
00127   operator const BoolePolynomial&() const { return m_poly; }
00128 
00130   exp_type exp() const;
00131 
00132 
00134   const_iterator begin() const { return m_poly.firstBegin(); }
00135 
00137   const_iterator end() const { return m_poly.firstEnd(); }
00138 
00140   variable_iterator variableBegin() const { 
00141     return variable_iterator(begin(), ring()); 
00142   }
00143 
00145   variable_iterator variableEnd() const { 
00146     return variable_iterator(end(), ring()); 
00147   }
00148 
00150   deg_type deg() const {
00152     #if 0
00153     return m_poly.nNodes(); 
00154     #else
00155     return std::distance(m_poly.firstBegin(),m_poly.firstEnd());
00156     #endif
00157     }
00158 
00160   size_type size() const { return (size_type)deg(); }  // always nonnegative
00161 
00163   set_type divisors() const { return m_poly.leadDivisors(); }
00164 
00166   set_type multiples(const self&) const; 
00167 
00169   hash_type stableHash() const {
00170     return stable_first_hash_range(m_poly.navigation());
00171   }
00172 
00174   hash_type hash() const {  return m_poly.hash(); }
00175 
00177   self change(idx_type) const;
00178 
00179 
00181 
00182   self& operator*=(const self&);
00183   self& operator/=(const self&);
00184   self& operator*=(const var_type&);
00185   self& operator/=(const var_type&);
00187 
00189 
00190   bool_type operator==(const self& rhs) const { return m_poly == rhs.m_poly; }
00191   bool_type operator!=(const self& rhs) const { return m_poly != rhs.m_poly; }
00192   bool_type operator==(constant_type rhs) const { return m_poly == rhs; }
00193   bool_type operator!=(constant_type rhs) const { return m_poly != rhs; }
00194   bool_type isOne() const { return m_poly.isOne(); }
00195   bool_type isConstant() const { return m_poly.isConstant(); }
00197 
00199   bool_type reducibleBy(const self& rhs) const { 
00200     return m_poly.firstReducibleBy(rhs); }
00201   bool_type reducibleBy(const var_type& rhs) const;
00202 
00204   comp_type compare(const self&) const;
00205 
00207   deg_type LCMDeg(const self&) const;
00208 
00210   self& LCMAssign(const self&);
00211 
00213   self LCM(const self&) const;
00214 
00216   self& GCDAssign(const self&);
00217 
00219   self GCD(const self&) const;
00220  
00222   const dd_type& diagram() const { return m_poly.diagram(); }
00223 
00225   set_type set() const { return m_poly.set(); }
00226 
00228   self& popFirst() { 
00229     assert(!m_poly.isConstant());
00230     return *this = set_type( dd_type(m_poly.ring(),
00231                                      m_poly.navigation().thenBranch()) ); 
00232   }
00233 
00235   var_type firstVariable() const;
00236 
00238   idx_type firstIndex() const {
00239     assert(!m_poly.isConstant());
00240     return *begin();
00241   }
00242 
00244   const ring_type& ring() const { return m_poly.ring(); } 
00245 
00246 protected:
00248   dd_type& internalDiagram() { return m_poly.internalDiagram(); }
00249 
00251   //  BooleMonomial(const dd_type& rhs): m_poly(rhs) {}
00253   BooleMonomial(const set_type& rhs): m_poly(rhs.diagram()) {
00254     assert(!m_poly.isZero());
00255   }
00256 
00257 private:
00258   BoolePolynomial m_poly;
00259 };
00260 
00262 inline BooleMonomial
00263 operator*(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00264   return BooleMonomial(lhs) *= rhs;
00265 }
00267 inline BooleMonomial
00268 operator*(const BooleMonomial& lhs, const BooleVariable& rhs) {
00269   return BooleMonomial(lhs) *= rhs;
00270 }
00272 inline BoolePolynomial
00273 operator*(const BooleMonomial& lhs, BooleConstant rhs) {
00274   return BoolePolynomial(lhs) *= rhs;
00275 }
00276 
00278 inline BoolePolynomial
00279 operator*(BooleConstant lhs, const BooleMonomial& rhs) {
00280   return rhs * lhs;
00281 }
00282 
00284 inline BooleMonomial
00285 operator/(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00286   return BooleMonomial(lhs) /= rhs;
00287 }
00288 
00290 inline BooleMonomial
00291 operator/(const BooleMonomial& lhs, const BooleVariable& rhs) {
00292   return  lhs / BooleMonomial(rhs);
00293 }
00294 
00296 inline BooleMonomial::bool_type
00297 operator<(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00298 
00299   return (lhs.compare(rhs) == CTypes::less_than);
00300 }
00301 
00303 inline BooleMonomial::bool_type
00304 operator>(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00305 
00306   return (lhs.compare(rhs) == CTypes::greater_than);
00307 }
00308 
00310 inline BooleMonomial::bool_type
00311 operator<=(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00312 
00313   return (lhs.compare(rhs) <= CTypes::less_or_equal_max);
00314 }
00315 
00317 inline BooleMonomial::bool_type
00318 operator>=(const BooleMonomial& lhs, const BooleMonomial& rhs) {
00319 
00320   return (lhs.compare(rhs) >= CTypes::greater_or_equal_min);
00321 }
00322 
00323 
00325 inline BooleMonomial
00326 GCD(const BooleMonomial& lhs, const BooleMonomial& rhs ){
00327 
00328   return lhs.GCD(rhs);
00329 }
00330 
00332 inline BooleMonomial
00333 LCM(const BooleMonomial& lhs, const BooleMonomial& rhs ){
00334 
00335   return lhs.LCM(rhs);
00336 }
00337 
00338 // Anyone need this?
00341 // BooleMonomial::bool_type
00342 // greater_variable(BooleMonomial::idx_type lhs, BooleMonomial::idx_type rhs);
00343 
00344 
00346 inline BoolePolynomial
00347 operator*(const BooleVariable& lhs, const BooleConstant& rhs){
00348 
00349   return BooleMonomial(lhs) * rhs;
00350 }
00351 
00353 inline BoolePolynomial
00354 operator*(const BooleConstant& lhs, const BooleVariable& rhs){
00355 
00356   return rhs * lhs;
00357 }
00358 
00360 inline BoolePolynomial
00361 operator*(const BooleVariable& lhs, 
00362           const BoolePolynomial& rhs){
00363 
00364   return BoolePolynomial(rhs) *= BooleMonomial(lhs);
00365 }
00366 
00368 inline BooleMonomial
00369 operator*(const BooleVariable& lhs, 
00370           const BooleMonomial& rhs){
00371 
00372   return BooleMonomial(lhs) * rhs;
00373 }
00374 
00376 inline BoolePolynomial&
00377 operator*=(BoolePolynomial& lhs, 
00378            const BooleVariable& rhs){
00379 
00380   return lhs *= BooleMonomial(rhs);
00381 }
00382 
00384 inline BooleMonomial
00385 operator*(const BooleVariable& lhs, 
00386           const BooleVariable& rhs){
00387 
00388   return BooleMonomial(lhs) *= BooleMonomial(rhs);
00389 }
00390 
00392 inline BoolePolynomial
00393 operator*(const BoolePolynomial& lhs, 
00394           const BooleVariable& rhs){
00395 
00396   return BoolePolynomial(lhs) *= BooleMonomial(rhs);
00397 }
00398 
00400 inline BoolePolynomial
00401 operator/(const BoolePolynomial& lhs, 
00402           const BooleVariable& rhs){
00403 
00404   return lhs / BooleMonomial(rhs);
00405 }
00406 
00407 
00409 inline BoolePolynomial
00410 operator%(const BoolePolynomial& lhs, 
00411           const BooleVariable& rhs){
00412 
00413   return lhs % BooleMonomial(rhs);
00414 }
00415 
00416 
00417 END_NAMESPACE_PBORI
00418 
00419 
00420 #endif // of BooleMonomial_h_