Esempio n. 1
0
Polynomial&
Polynomial::operator+=(const Monomial& m)
{ 
  if (m.degree() <= this->degree())
    {
      Monomial currentMonomial = mMonomials[m.degree()];

      if (currentMonomial.isZero())   
	{
	  mMonomials[m.degree()] = m;
	}
      else
	{
	  mMonomials[m.degree()] = Monomial(currentMonomial.coefficientReal() + m.coefficientReal(), 
					    currentMonomial.coefficientImag() + m.coefficientImag(),
					    m.degree());
	}
    }
  else
    {
      mMonomials.resize(m.degree() + 1);
      mMonomials[m.degree()] = m;
    }

  /* Possibly deflate the polynomial, if necessary */
  while (mMonomials[degree()].isZero() && degree() > 0)
    {
      mMonomials.resize(degree());
    }

  return *this;
}
Esempio n. 2
0
void MatcherMonomial::overlapMatch(const Monomial & mona,int a,
     const Monomial & monb,int b, list<Match> & result) {
   int leni = mona.numberOfFactors();
   int lenj = monb.numberOfFactors();
   int spread = (leni<lenj) ? leni-1 : lenj-1;
   subMonomial INodei(mona,1,0);
   subMonomial INodej(monb,1,0);
   for(int tempk=1;tempk<=spread;++tempk) {
     INodei.length(0);
     if(tempk>1) {
        INodei.decrementStart();
     } else {
        INodei.start(leni);
     }
     INodei.length(tempk); 
     INodej.length(tempk); 
     // special meaning here
     if(INodei==INodej) {
        Match match;
        constructMatch(mona,a,monb,b,
               INodei,INodej,OVERLAP_MATCH,match);
        result.push_back(match);
     }
   }   
};
Esempio n. 3
0
void MatcherMonomial::subMatch(const Monomial & mona,int a,
   const Monomial & monb, int b, GBList<Match> & result)  {
  if(mona==monb) {
    Match match;
    match.firstGBData = a;
    match.secondGBData = b;
    match.subsetMatch = true;
    match.overlapMatch = false;
    result.push_back(match);
  } else {
    int leni = mona.numberOfFactors();
    int lenj = monb.numberOfFactors();
    if(leni<lenj) {
      subMonomial INodei(mona,1,0);
      subMonomial INodej(monb,1,0);
  
      const int spread = lenj - leni + 1;
  
      INodei.length(leni);
      INodej.length(leni);
      for(int k=1;k<=spread;++k) {
         if(k>1) INodej.incrementStart();
         if(INodei==INodej) {
            Match match;
            constructMatch(mona,a,monb,b,
               INodei,INodej,SUBSET_MATCH,match);
            result.push_back(match);
         }
      }
    }
  }
};
Esempio n. 4
0
Monomial Wreath::grabAtLevel(int aLevel,const Monomial & first) const {
  Monomial result;
  MonomialIterator w = first.begin();
  const int sz = first.numberOfFactors();
  for(int i=1;i<=sz;++i,++w) {
    const Variable & v = *w;
    if(level(v)==aLevel) result *= v;
  };
  return result;
};
Esempio n. 5
0
void removePowers(const Monomial & m,Monomial & result) {
  if(m.numberOfFactors()!=0) {
    Variable v;
    result.setToOne();
    MonomialIterator w = m.begin(), e = m.end();
    while(w!=e) {
      if(v!=*w) {
        v = *w;
        result *= v;
      };
      ++w;
    };
  };
};
Esempio n. 6
0
inline
subMonomial::subMonomial(const Monomial & theMonomial,int st,int len) :
    _mono(theMonomial), _start(1), _length(0),
    start_iterator(theMonomial.begin()) {
   start(st);
   length(len);
};
Esempio n. 7
0
void IISource::get(Monomial& x) {
#if 1
  x.setToOne();
  Variable v;
  int type = getType();
  if(type==GBInputNumbers::s_IOFUNCTION) {
    pair<bool,Alias<ISource> > pr(queryNamedFunction("NonCommutativeMultiply"));
    if(pr.first) {
      while(!pr.second.access().eoi()) {
        pr.second.access().get(v);
        x *= v;
      };
    } else {
      get(v);
      x *= v;
    };
  } else if(type==GBInputNumbers::s_IOINTEGER) {
    int i;
    ((ISource *)this)->get(i);
    if(i!=1) DBG();
  } else if(type==GBInputNumbers::s_IOSYMBOL) {
    get(v);
    x *= v;
  } else {
    TellHead(*this);
    DBG();
  };
#else
  DBG();
#endif
};
Esempio n. 8
0
void NCASink::put(const Monomial& x) {
  int len = x.numberOfFactors();
  if(len==0) {
    d_ofs << '1';
  } else if(len==1) {
    put(*x.begin());
  } else {
    MonomialIterator w = x.begin();
    put(*w);
    --len;++w;
    while(len) {
      d_ofs << " ** ";
      put(*w);
      --len;++w;
    };
  };
};
Esempio n. 9
0
bool MatcherMonomial::matchExistsNoRecord(const Monomial & mona,
                                     const Monomial & monb) {
  k = 0; // mutable class variable
  bool result = mona==monb;
  if(!result) {
    int leni = mona.numberOfFactors();
    int lenj = monb.numberOfFactors();
    if(leni<lenj) {
      subMonomial INodei(mona,1,0);
      subMonomial INodej(monb,1,0);

      const int spread = lenj - leni + 1;

      INodei.length(leni);
      INodej.length(leni);
#ifdef NEWCODE
      if(spread>0) {
        Variable v = * mona.begin();
#endif
        for(int tempk = 1; tempk <= spread && !result; ++tempk) {
          if(tempk!=1) {
            INodej.incrementStart();
          }
#ifdef NEWCODE
          tempk = INodej.findVariableForward(spread-tempk+1,v);
          if(tempk==-1) {
            tempk = spread+1;
          } else
#endif
          {
            result = INodei==INodej;
            if(result) k = tempk;
          }
        }
#ifdef NEWCODE
      } else {
        k = 1; // deal with matching against 1
      }
#endif
    }
  }
  return result;
};
Esempio n. 10
0
void MatcherMonomial::overlapMatch(const Monomial & mona,
     const Monomial & monb, list<int> & result) {
   int leni = mona.numberOfFactors();
   int lenj = monb.numberOfFactors();
   int spread = (leni<lenj) ? leni-1 : lenj-1;
   subMonomial INodei(mona,1,0);
   subMonomial INodej(monb,1,0);
//mxs redundent   INodej.start(1);
   for(int tempk=1;tempk<=spread;++tempk) {
     INodei.length(0);
     if(tempk>1) {
        INodei.decrementStart();
     } else {
        INodei.start(leni);
     }
     INodei.length(tempk); 
     INodej.length(tempk); 
     // special meaning here
     if(INodei==INodej) {
        result.push_back(tempk);
     }
   }   
};
Esempio n. 11
0
bool MatcherMonomial::matchExists(const Monomial & mona,
                             const Monomial & monb) {
  setInternalMatch();
  bool result = matchExistsNoRecord(mona,monb);
  if(result && k > 0) {
    const int beginNum = k-1;
    MonomialIterator iter = monb.begin();
    for(int i = 1; i <= beginNum; ++i,++iter) {
      _theMatch->left1() *= (*iter);
    }
    const int numberFactors = monb.numberOfFactors();
    const int temp2 = k + mona.numberOfFactors();
    if(temp2<=numberFactors) {
      MonomialIterator iter2 = monb.begin();
      int jj = temp2-1;
      while(jj) {++iter2;--jj;};
      for(int j=temp2;j<=numberFactors;++j,++iter2) {
        _theMatch->right1() *= (*iter2);
      }
    }
  }
  return result;
};
Esempio n. 12
0
void GrbSource::get(Monomial& x) {
  x.setToOne();
  Variable v;
  char s[2];
  s[1]='\0';
  d_so.getCharacter(s[0],"\n *");
  while(('a'<=*s && *s<='z') || ('A'<=*s&& *s<='Z')) {
    v.assign(s);
    x *= v;
    d_so.getCharacter(s[0]);
  };
  d_so.unGetCharacter(s[0]);
  d_eoi = d_so.eof();
};
Esempio n. 13
0
 void MmaSink::put(const Monomial& x) {
#ifdef DEBUG_MMASINK
   GBStream << "sink:monomial " << this << ' ' << x << '\n';
#endif
  int len = x.numberOfFactors();
  if(len==0) {
    MLPutInteger(d_mlink,1);
    ++d_count;
  } else if(len==1) {
    put(*x.begin());
  } else {
    MLPutFunction(d_mlink,"NonCommutativeMultiply",len);
    ++d_count;
    d_count -= len;
    MonomialIterator w = x.begin();
    while(len) {
      put(*w);
      --len;++w;
    };
  };
#ifdef DEBUG_MMASINK
  checkforerror();
#endif
};
Esempio n. 14
0
typename Polynomial<CoefficientType>::Monomial
Polynomial<CoefficientType>::Monomial::factor(const Monomial& divisor) const {
  Monomial error, result;
  error.coefficient = 0;
  result.coefficient = coefficient / divisor.coefficient;
  for (const Term& term : terms) {
    const PowerType divisor_power = divisor.getDegreeOf(term.var);
    if (term.power < divisor_power) { return error; }
    Term new_term;
    new_term.var = term.var;
    new_term.power = term.power - divisor_power;
    if (new_term.power > 0) {
      result.terms.push_back(new_term);
    }
  }
  for (const Term& divisor_term : divisor.terms) {
    if (!getDegreeOf(divisor_term.var)) { return error; }
  }
  return result;
}
Esempio n. 15
0
int
polyjam::core::Monomial::grevlexComparison(
    const Monomial & operant ) const
{
  if( isIncompatible(operant) )
    return 0;

  unsigned int thisDegree = degree();
  unsigned int thatDegree = operant.degree();

  if( thisDegree == thatDegree )
    return revlexComparison(operant);
  else
  {
    if( thisDegree > thatDegree )
      return 1;
    else
      return -1;
  }

  return 0;
}
Esempio n. 16
0
void MatcherMonomial::constructMatch( const Monomial & mona,int a,
       const Monomial & monb,int b, const subMonomial & aINode,
       const subMonomial & bINode, MATCHING_TYPES type,
       Match & match ) const {
  match.firstGBData = a;
  match.secondGBData = b;

  const int bStartm1 = bINode.start()-1;
  MonomialIterator iter = monb.begin();
  int i=1;
  for(;i<=bStartm1;++i,++iter) {
    match.left1() *= (* iter);
  }
  const int bNumFactor = monb.numberOfFactors();
  int temp2 = bINode.start()+bINode.length();
  if(temp2<=bNumFactor) {
    MonomialIterator iter2 = monb.begin();
    int jj = temp2-1;
    while(jj) { ++iter2;--jj;};
    for(int j=temp2;j<=bNumFactor;++j,++iter2) {
      match.right1() *= (* iter2);
    }
  }
  if(type==SUBSET_MATCH) {
    match.subsetMatch = false;
    match.overlapMatch = true;  
  } else if(type==OVERLAP_MATCH) {
    match.subsetMatch = false;
    match.overlapMatch = true;
    const int aStartm1 = aINode.start()-1;
    MonomialIterator iter3 = mona.begin(); 
    for(i=1;i<=aStartm1;++i,++iter3) {   
       match.left2() *= (* iter3);
    }   
    MonomialIterator iter4 = mona.begin(); 
    const int temp4 = aINode.start()+aINode.length();
    int jj = temp4-1;
    while(jj) { ++iter4;--jj;};
    for(int j=temp4;j<=mona.numberOfFactors();++j,++iter4) {   
       match.right2() *= (* iter4);
    }   
  }
};
Esempio n. 17
0
Polynomial::Polynomial(Monomial m)
{
  mMonomials.resize(m.degree() + 1, Monomial("0", 0));
  mMonomials[m.degree()] = m;
}
Esempio n. 18
0
Monomial::Monomial(const Monomial& rhs)
{
  mCoeffR = rhs.coefficientReal();
  mCoeffI = rhs.coefficientImag();
  mDegree = rhs.degree();
}
Esempio n. 19
0
bool PseudoBooleanProcessor::decomposeAssertion(Node assertion, bool negated)
{
  if (assertion.getKind() != kind::GEQ)
  {
    return false;
  }
  Assert(assertion.getKind() == kind::GEQ);

  Debug("pbs::rewrites") << "decomposeAssertion" << assertion << std::endl;

  Node l = assertion[0];
  Node r = assertion[1];

  if (r.getKind() != kind::CONST_RATIONAL)
  {
    Debug("pbs::rewrites") << "not rhs constant" << assertion << std::endl;
    return false;
  }
  // don't bother matching on anything other than + on the left hand side
  if (l.getKind() != kind::PLUS)
  {
    Debug("pbs::rewrites") << "not plus" << assertion << std::endl;
    return false;
  }

  if (!Polynomial::isMember(l))
  {
    Debug("pbs::rewrites") << "not polynomial" << assertion << std::endl;
    return false;
  }

  Polynomial p = Polynomial::parsePolynomial(l);
  clear();
  if (negated)
  {
    // (not (>= p r))
    // (< p r)
    // (> (-p) (-r))
    // (>= (-p) (-r +1))
    d_off = (-r.getConst<Rational>());

    if (d_off.value().isIntegral())
    {
      d_off = d_off.value() + Rational(1);
    }
    else
    {
      d_off = Rational(d_off.value().ceiling());
    }
  }
  else
  {
    // (>= p r)
    d_off = r.getConst<Rational>();
    d_off = Rational(d_off.value().ceiling());
  }
  Assert(d_off.value().isIntegral());

  int adj = negated ? -1 : 1;
  for (Polynomial::iterator i = p.begin(), end = p.end(); i != end; ++i)
  {
    Monomial m = *i;
    const Rational& coeff = m.getConstant().getValue();
    if (!(coeff.isOne() || coeff.isNegativeOne()))
    {
      return false;
    }
    Assert(coeff.sgn() != 0);

    const VarList& vl = m.getVarList();
    Node v = vl.getNode();

    if (!isPseudoBoolean(v))
    {
      return false;
    }
    int sgn = adj * coeff.sgn();
    if (sgn > 0)
    {
      d_pos.push_back(v);
    }
    else
    {
      d_neg.push_back(v);
    }
  }
  // all of the variables are pseudoboolean
  // with coefficients +/- and the offsetoff
  return true;
}