Exemple #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;
}
Exemple #2
0
tr1::shared_ptr<AbstractNumber> E::add(tr1::shared_ptr<AbstractNumber>number){

	if (number -> getName() == "E")
	{
		if (number -> getSign() == '+' && getSign() == '+')
		{
			vector<tr1::shared_ptr<AbstractNumber> > M;
			tr1::shared_ptr<AbstractNumber> two(new SmartInteger(2));
			M.push_back(two);
			M.push_back(shared_from_this());
			tr1::shared_ptr<AbstractNumber> output(new MultExpression(M, '+'));
			return output;
		}

		else if (number -> getSign() == '-' && getSign() == '-')
		{
			vector<tr1::shared_ptr<AbstractNumber> > N;
			tr1::shared_ptr<AbstractNumber> twoN(new SmartInteger(-2));
			N.push_back(twoN);
			tr1::shared_ptr<AbstractNumber> me(new E());
			N.push_back(me);
			tr1::shared_ptr<AbstractNumber> output1(new MultExpression(N, '+'));
			return output1;
		}

		else
		{
			tr1::shared_ptr<AbstractNumber> zero(new SmartInteger(0));
			return zero;
		}

	}
	else if(number->getName() == "MultExpression" || number->getName() == "SumExpression")
    {
        return number->add(shared_from_this());
    }

    vector<tr1::shared_ptr<AbstractNumber> > N;
    N.push_back(number);
    N.push_back(shared_from_this());
    tr1::shared_ptr<AbstractNumber> output1(new SumExpression(N));
    return output1;

}
Exemple #3
0
// Divides this by number and returns the product
//
// Parameters:
// shared_ptr<AbstractNumber> number	divisor
//
// Returns:
// shared_ptr<AbstractNumber> 			resulting quotient of division
tr1::shared_ptr<AbstractNumber>  Exponent::divide(tr1::shared_ptr<AbstractNumber>number){
	// Reverses sign if exponent and multiplies
	if(number->getName()=="Exponent"){
        tr1::shared_ptr<Exponent> givenNumber = tr1::static_pointer_cast<Exponent>(number);
		if(givenNumber->getSign()=='-'){
			tr1::shared_ptr<AbstractNumber> r(new Exponent(givenNumber->getValue("base"), givenNumber->getValue("power")));
			return multiply(r);
		}
		else{
            tr1::shared_ptr<AbstractNumber> nOne(new SmartInteger(-1));
            tr1::shared_ptr<AbstractNumber> newPower = givenNumber->getValue("power")->multiply(nOne);
			tr1::shared_ptr<AbstractNumber> r(new Exponent(givenNumber->getValue("base"), newPower, givenNumber->getSign()));
			return multiply(r);
		}
	}

	// Subtracts 1 from power if base = number
	else if(number->toDouble() == base->toDouble()){
		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() == "MultExpression")
    {
        tr1::shared_ptr<MultExpression> MultE = tr1::static_pointer_cast<MultExpression>(number);
        vector<tr1::shared_ptr<AbstractNumber> > MultENum = MultE->getNumerator();
        vector<tr1::shared_ptr<AbstractNumber> > MultEDem = MultE->getDenominator();
        if (MultEDem.size() == 0)
        {
            tr1::shared_ptr<AbstractNumber> one(new SmartInteger(1));
            MultEDem.push_back(one);
        }
        tr1::shared_ptr<AbstractNumber> reversedMultE(new MultExpression(MultEDem, MultENum, '+'));
        return reversedMultE->multiply(shared_from_this());
    }

    vector< tr1::shared_ptr<AbstractNumber> > NumVector;
    tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
    NumVector.push_back(me);
    vector< tr1::shared_ptr<AbstractNumber> > DenVector;
    DenVector.push_back(number);
    tr1::shared_ptr<AbstractNumber> r(new MultExpression(NumVector, DenVector, this->calcSign(number)));
    return r;
}
Exemple #4
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;
	}

}
Exemple #5
0
tr1::shared_ptr<AbstractNumber> E::divide(tr1::shared_ptr<AbstractNumber>number){
    char newSign = '-';
    if (getSign() == number->getSign())
    {
        newSign = '+';
    }
		if (number -> getName() == "E")
		{

			if (newSign == '+')
			{
				tr1::shared_ptr<AbstractNumber> output(new SmartInteger(1));
				return output;
			}

			else
			{
				tr1::shared_ptr<AbstractNumber> output(new SmartInteger(-1));
				return output;
			}

		}

		else if (number -> getName() == "Exponent")
		{
		    tr1::shared_ptr<Exponent> numExp = tr1::static_pointer_cast<Exponent>(number);
			if (numExp -> getValue("base") -> getName() == "E")
			{
				tr1::shared_ptr<AbstractNumber> num(new SmartInteger(1));
				tr1::shared_ptr<AbstractNumber> exp = number->getValue("power");
				tr1::shared_ptr<AbstractNumber> exp2(new SmartInteger(-1));
				tr1::shared_ptr<AbstractNumber> me(new E());
				tr1::shared_ptr<AbstractNumber> ans2(new Exponent(me, exp -> add(exp2), newSign));
				tr1::shared_ptr<AbstractNumber> output2(new MultExpression(num, ans2, '+'));
				return output2;
			}
		}
        else if(number->getName() == "MultExpression")
        {
            tr1::shared_ptr<MultExpression> MultE = tr1::static_pointer_cast<MultExpression>(number);
            vector<tr1::shared_ptr<AbstractNumber> > MultENum = MultE->getNumerator();
            vector<tr1::shared_ptr<AbstractNumber> > MultEDem = MultE->getDenominator();
            if (MultEDem.size() == 0)
            {
                tr1::shared_ptr<AbstractNumber> one(new SmartInteger(1));
                MultEDem.push_back(one);
            }
            tr1::shared_ptr<AbstractNumber> reversedMultE(new MultExpression(MultEDem, MultENum, number->getSign()));
            return reversedMultE->multiply(shared_from_this());
        }

        tr1::shared_ptr<AbstractNumber> output2(new MultExpression(shared_from_this(), number, newSign));
        return output2;
}
Exemple #6
0
tr1::shared_ptr<AbstractNumber> E::multiply(tr1::shared_ptr<AbstractNumber>number){
    char newSign = '-';
    if (getSign() == number->getSign())
    {
        newSign = '+';
    }

    if(number -> getName() == "E")
	{
	    if (newSign == '+')
        {
            tr1::shared_ptr<AbstractNumber> exp(new SmartInteger(2));
            tr1::shared_ptr<AbstractNumber> me(new E());
            tr1::shared_ptr<AbstractNumber> ans(new Exponent(me, exp));
            return ans;
        }
        else
        {
            tr1::shared_ptr<AbstractNumber> exp(new SmartInteger(-2));
            tr1::shared_ptr<AbstractNumber> me(new E());
            tr1::shared_ptr<AbstractNumber> ans(new Exponent(me, exp));
            return ans;
        }
	}

	else if (number -> getName() == "Exponent")
	{
	    tr1::shared_ptr<Exponent> numExp = tr1::static_pointer_cast<Exponent>(number);
		if (numExp -> getValue("base") -> getName() == "E")
		{
			tr1::shared_ptr<AbstractNumber> exp = numExp->getValue("power");
			tr1::shared_ptr<AbstractNumber> exp2(new SmartInteger(1));
			tr1::shared_ptr<AbstractNumber> me(new E());

			tr1::shared_ptr<AbstractNumber> ans2(new Exponent(me, exp -> add(exp2), newSign));
			return ans2;
		}
	}
	else if (number->getName() == "Radical") {
		 if (abs(number->getValue("value")->toDouble() - toDouble()) < 0.000001 )
		 {
			 tr1::shared_ptr<AbstractNumber> one(new SmartInteger(1));
			 tr1::shared_ptr<AbstractNumber> invertedRoot(new MultExpression(one, number->getValue("root"), '+'));
			 tr1::shared_ptr<AbstractNumber> me(new E());
			 tr1::shared_ptr<AbstractNumber> output(new Exponent(me, invertedRoot->add(one), newSign));
			 return output;
		 }
		 else
         {
            vector<tr1::shared_ptr<AbstractNumber> > M;
            M.push_back(number);
            M.push_back(shared_from_this());
            tr1::shared_ptr<AbstractNumber> ans3(new MultExpression(M, '+'));
            return ans3;
         }
	 }

    else if(number->getName() == "MultExpression")
    {
        return number->multiply(shared_from_this());
    }
    vector<tr1::shared_ptr<AbstractNumber> > M;
    M.push_back(number);
    M.push_back(shared_from_this());
    tr1::shared_ptr<AbstractNumber> ans3(new MultExpression(M, '+'));
    return ans3;

}