Beispiel #1
0
 // Multiplies number by this and returns the product
 //
 // Parameters:
 // shared_ptr<AbstractNumber> number	number being multiplied
 //
 // Returns:
 // shared_ptr<AbstractNumber> 			resulting product of multiplication
tr1::shared_ptr<AbstractNumber>  Exponent::multiply(tr1::shared_ptr<AbstractNumber> number){
	// Checks for simplification if both exponents
	number = number->simplify();

	if(number->getName() == "Exponent"){
        tr1::shared_ptr<Exponent> givenNumber = tr1::static_pointer_cast<Exponent>(number);
		if(abs(givenNumber->getValue("base")->toDouble() - base->toDouble()) < 0.000001){
			tr1::shared_ptr<AbstractNumber> r(new Exponent(base, power->add(givenNumber->getValue("power")), this->calcSign(number)));
		    return r;
		}
	else if (number->getName() == "Radical") {
		 if (abs(number->getValue("value")->toDouble() - base->toDouble()) < 0.000001 )
		 {
			 std::vector< tr1::shared_ptr<AbstractNumber> > SumVector;
			 tr1::shared_ptr<AbstractNumber> one(new SmartInteger(1));
			 tr1::shared_ptr<AbstractNumber> invertedRoot(new MultExpression(one, number->getValue("root")->noSign(), number->getValue("root")->getSign()));
			 SumVector.push_back(power);
			 SumVector.push_back(invertedRoot);
			 tr1::shared_ptr<AbstractNumber> power(new SumExpression(SumVector));
			 tr1::shared_ptr<AbstractNumber> output(new Exponent(number->getValue("value")->noSign(), power, sign));
			 return output;
		 }
	}
        else{
            tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
            tr1::shared_ptr<AbstractNumber> r(new MultExpression(me, number, this->calcSign(number)));
            return r;
        }
	}

	// Checks for simplification if number = base
	// Adds 1 to exponent
	else if(abs(number->toDouble() - base->toDouble()) < 0.000001 ){
		tr1::shared_ptr<AbstractNumber> c(new SmartInteger(1));
		tr1::shared_ptr<AbstractNumber> r(new Exponent(base, power->add(c), this->calcSign(number)));
	    return r;
	}
	else if(number->getName() == "SumExpression" || number->getName() == "MultExpression")
    {
        return number->multiply(shared_from_this());
    }

    vector< tr1::shared_ptr<AbstractNumber> > MultVector;
    tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
    MultVector.push_back(me);
    MultVector.push_back(number);
    tr1::shared_ptr<AbstractNumber> r(new MultExpression(MultVector, '+'));

    return r;
}
Beispiel #2
0
// Adds number to this and returns the sum
//
// Parameters:
// shared_ptr<AbstractNumber> number	number being added
//
// Returns:
// shared_ptr<AbstractNumber> 			resulting sum of addition
 tr1::shared_ptr<AbstractNumber> Exponent::add(tr1::shared_ptr<AbstractNumber> number){
 	// Checks for cancellation
 	number = number->simplify();
 	if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 	   this->getSign() != number->getSign()){
 		tr1::shared_ptr<AbstractNumber> r(new SmartInteger(0));

 	    return r;
 	}
 	// Checks for duplication/simplification
 	else if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 			this->getSign() == number->getSign()){
 		vector< tr1::shared_ptr<AbstractNumber> > MultVector;
		tr1::shared_ptr<AbstractNumber> i(new SmartInteger(2));
		MultVector.push_back(i);
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		MultVector.push_back(me);
		tr1::shared_ptr<AbstractNumber> r(new MultExpression(MultVector, '+'));

	    return r;
  	}

    //Can't tell if they add or not
 	else if(number->getName() == "SumExpression" ||
            number->getName() == "MultExpression"){
 	    return number->add(shared_from_this());
 	}
    // Duplication necessary for simplification
 	// Assuming number is in simplest form

 	// No simplification possible
	else{
		vector< tr1::shared_ptr<AbstractNumber> > SumVector;
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		SumVector.push_back(me);
		SumVector.push_back(number);
		tr1::shared_ptr<AbstractNumber> r(new SumExpression(SumVector));

	    return r;
	}

}