Beispiel #1
0
Polynomial* Polynomial::addPoly(Polynomial *b){
    Term* af = first;
    Term* bf = b->first;
    Polynomial *c = new Polynomial();
    while(af&&bf){
        if(af->exp==bf->exp){
            int sum = af->coef + bf->coef;
            if(sum) c->addTerm(sum, af->exp );
            af = af->link; bf = bf->link;
        }
        else if(af->exp<bf->exp){
            c->addTerm(bf->coef, bf->exp);
            bf = bf->link;
        }
        else{
            c->addTerm(af->coef, af->exp);
            af = af->link;
        }
    }
    while(af){
        c->addTerm(af->coef, af->exp);
        af = af->link;
    }
    while(bf){
        c->addTerm(bf->coef, bf->exp);
        bf = bf->link;
    }
    return c;
}
/* add two polynomials together */
const Polynomial Polynomial::add( const Polynomial& rhs ) const {
  Polynomial retVal;

  for( itr_t it = _list.begin(); it != _list.end(); ++it )
    retVal.addTerm( it.getData().coeff, it.getData().exp );

  for( itr_t it = rhs._list.begin(); it != rhs._list.end(); ++it )
    retVal.addTerm( it.getData().coeff, it.getData().exp );
  return retVal;
}
Beispiel #3
0
Polynomial get_eps22_term(int a1,int a2,int a3,int dir) {
	Polynomial ret;
	if (dir!=1) return ret;
	PolynomialTerm T;
	if (a2==0) T.coefficient=-1; else T.coefficient=1;
	for (int j=0; j<6; j++) T.exponents << 0;
	if (a1==0) T.exponents[1]=1; else T.exponents[0]=1;
	if (a3==0) T.exponents[5]=1; else T.exponents[4]=1;
	ret.addTerm(T);
	return ret;
}
Beispiel #4
0
Polynomial Polynomial::operator*(const Polynomial &P) {
	Polynomial ret;
	for (int i=0; i<termCount(); i++)
		for (int j=0; j<P.termCount(); j++) {
			PolynomialTerm T;
			T.exponents.clear();
			for (int k=0; k<6; k++) {
				T.exponents << term(i).exponents[k]+P.term(i).exponents[k];
			}
			T.coefficient=term(i).coefficient*P.term(i).coefficient;
			ret.addTerm(T);
		}
	return ret;
}
/* exponent and divide the coefficient by it */
const Polynomial Polynomial::integrate() {
  Polynomial retVal;

  for( itr_t it = _list.begin(); it != _list.end(); ++it )
    {
      int exp = it.getData().exp;
      exp++;
      double coeff = it.getIter()->_data->coeff / static_cast<double>(exp);
      retVal.addTerm( coeff, exp );
      // no way for a term to now be zero, so don't need to check
    }
  
  return retVal;
}
Beispiel #6
0
// Gets user input and turns it into a polynomail
Polynomial getPolynomial(Polynomial& p)
{
	p.clear();
	
	// Temp term to put into a list
	Term temp;

	// Line of input poly from user 
	string line;

	// Lists of terms and strings to build a poly
	list<Term> terms;
	list<string> strings;

	// User input
	cin.ignore();
	getline(cin, line);

	// Put + in front of any - as a delimiter for terms in a poly
	int stringSearchPosition(0);
	while (line.find('-', stringSearchPosition) != string::npos)
	{
		line.insert(line.find('-', stringSearchPosition), 1, '+');
		stringSearchPosition = line.find('-', stringSearchPosition) + 1;
	}

	// Position markers
	size_t prev(0), pos;

	// Break up the user input into substrings with + as the delimeter of terms
	while ((pos = line.find_first_of("+", prev)) != string::npos)
	{
		if (pos > prev)strings.push_back(line.substr(prev, pos - prev));
		prev = pos + 1;
	}

	// Take care of the end of the line
	if (prev < line.length())strings.push_back(line.substr(prev, std::string::npos));

	// Put the list of substrings into terms then into a poly
	for (std::list<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i)
	{
		Term t(*i);
		p.addTerm(t);
	}

	// Return the poly
	return p;
}
// doesn't return a remainder
// N.B. this function will break if there is a remainder
const Polynomial Polynomial::div( const Polynomial& rhs ) const {
  Polynomial retVal;

  if( degree() < rhs.degree() )
    {
      return retVal; // return 0
    }

  Polynomial rSide( *this );
  int rDeg = rhs.degree();
  double rCoeff = rhs._list.begin().getData().coeff;
  
  itr_t it = rSide._list.begin();
  while( 1 )
    {
      if( it == rSide._list.end() ) break;
      int deg_diff = it.getData().degree() - rDeg;
      if( deg_diff < 0 ) break; // TODO: check this condition, maybe need to put rest into remainder?
      
      double coeff = it.getData().coeff / rCoeff;
      Polynomial tmp;
      Term multiplier( coeff, deg_diff );
      retVal.addTerm( multiplier );
      
      for( itr_t itt = rhs._list.begin(); itt != rhs._list.end(); ++itt )
	{
	  Term res = itt.getData() * multiplier;
	  tmp.addTerm( res );
	}

      rSide = rSide.sub( tmp );
      it = rSide._list.begin();
    }
  
  return retVal;
}
/* multiply two polynomials together */
const Polynomial Polynomial::mult( const Polynomial& rhs ) const {
  Polynomial retVal;
  for( itr_t it = _list.begin(); it != _list.end(); ++it )
    {
      for( itr_t rit = rhs._list.begin(); rit != rhs._list.end(); ++rit )
	{
	  Term aterm = it.getData() * rit.getData();
	  if( aterm.coeff != 0 ) // don't have to have this check, since addTerm() already does it, but it can save a function jump
	    {
	      retVal.addTerm( aterm.coeff, aterm.exp );
	    }
	}
    }
  return retVal;
}
/* decrement the coefficient                */
const Polynomial Polynomial::differentiate() {
  Polynomial retVal;

  for( itr_t it = _list.begin(); it != _list.end(); ++it )
    {
      int exp = it.getData().exp;
      double coeff = it.getIter()->_data->coeff * exp;

      if( (coeff != 0) && (exp != 0) )
	{
	  exp--;
	  retVal.addTerm( coeff, exp );
	}
    }

  return retVal;
}
Beispiel #10
0
int main(int argc, const char * argv[]) {
    // insert code here...
    Polynomial* a = new Polynomial();
    Polynomial* b = new Polynomial();
    a->addTerm(2, 7);
    a->addTerm(2, 3);
    a->addTerm(5, 2);
    a->addTerm(-3, 1);
    a->addTerm(10, 0);
    b->addTerm(5, 5);
    b->addTerm(-2, 3);
    b->addTerm(-3, 2);
    b->addTerm(7, 1);
    b->addTerm(-8, 0);
    
    Polynomial* c= a->addPoly(b);
    a->printPoly();
    b->printPoly();
    c->printPoly();
    return 0;
}