//deklarerar
Logarithm::Logarithm (const Expression& E_, const double c1_, const double c2_, const int b_)
{
    c1 = c1_;
    c2 = c2_;
    b = b_;
    E = E_.clone(); //klonar polynom til this-objekt
}
Exemplo n.º 2
0
    void Interpreter::setIndex(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 1, "set_index need exactly one operand");

        Expression* exp = this->resolveExpression(instruction->getOperand(0));
        exp->is<NumericExpression>().ensure("Index must be integer");

        this->pushStack(exp->clone());
    }
Exemplo n.º 3
0
Expression * Trigonometry::shallowReduceDirectFunction(Expression * e, Context& context, Expression::AngleUnit angleUnit) {
  assert(e->type() == Expression::Type::Sine || e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent);
  Expression * lookup = Trigonometry::table(e->operand(0), e->type(), context, angleUnit);
  if (lookup != nullptr) {
    return e->replaceWith(lookup, true);
  }
  Expression::Type correspondingType = e->type() == Expression::Type::Cosine ? Expression::Type::ArcCosine : (e->type() == Expression::Type::Sine ? Expression::Type::ArcSine : Expression::Type::ArcTangent);
  if (e->operand(0)->type() == correspondingType) {
    float trigoOp = e->operand(0)->operand(0)->approximateToScalar<float>(context, angleUnit);
    if (e->type() == Expression::Type::Tangent || (trigoOp >= -1.0f && trigoOp <= 1.0f)) {
      return e->replaceWith(e->editableOperand(0)->editableOperand(0), true);
    }
  }
  if (e->operand(0)->sign() == Expression::Sign::Negative) {
    Expression * op = e->editableOperand(0);
    Expression * newOp = op->setSign(Expression::Sign::Positive, context, angleUnit);
    newOp->shallowReduce(context, angleUnit);
    if (e->type() == Expression::Type::Cosine) {
      return e->shallowReduce(context, angleUnit);
    } else {
      Multiplication * m = new Multiplication(new Rational(-1), e->clone(), false);
      m->editableOperand(1)->shallowReduce(context, angleUnit);
      return e->replaceWith(m, true)->shallowReduce(context, angleUnit);
    }
  }
  if ((angleUnit == Expression::AngleUnit::Radian && e->operand(0)->type() == Expression::Type::Multiplication && e->operand(0)->numberOfOperands() == 2 && e->operand(0)->operand(1)->type() == Expression::Type::Symbol && static_cast<const Symbol *>(e->operand(0)->operand(1))->name() == Ion::Charset::SmallPi && e->operand(0)->operand(0)->type() == Expression::Type::Rational) || (angleUnit == Expression::AngleUnit::Degree && e->operand(0)->type() == Expression::Type::Rational)) {
    Rational * r = angleUnit == Expression::AngleUnit::Radian ? static_cast<Rational *>(e->editableOperand(0)->editableOperand(0)) : static_cast<Rational *>(e->editableOperand(0));
    int unaryCoefficient = 1; // store 1 or -1
    // Replace argument in [0, Pi/2[ or [0, 90[
    Integer divisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(90));
    Integer dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(r->numerator(), r->numerator()) : r->numerator();
    if (divisor.isLowerThan(dividand)) {
      Integer piDivisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(180));
      IntegerDivision div = Integer::Division(r->numerator(), piDivisor);
      dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(div.remainder, div.remainder) : div.remainder;
      if (divisor.isLowerThan(dividand)) {
        div.remainder = Integer::Subtraction(piDivisor, div.remainder);
        if (e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent) {
          unaryCoefficient *= -1;
        }
      }
      Rational * newR = new Rational(div.remainder, r->denominator());
      Expression * rationalParent = angleUnit == Expression::AngleUnit::Radian ? e->editableOperand(0) : e;
      rationalParent->replaceOperand(r, newR, true);
      e->editableOperand(0)->shallowReduce(context, angleUnit);
      if (Integer::Division(div.quotient, Integer(2)).remainder.isOne() && e->type() != Expression::Type::Tangent) {
        unaryCoefficient *= -1;
      }
      Expression * simplifiedCosine = e->shallowReduce(context, angleUnit); // recursive
      Multiplication * m = new Multiplication(new Rational(unaryCoefficient), simplifiedCosine->clone(), false);
      return simplifiedCosine->replaceWith(m, true)->shallowReduce(context, angleUnit);
    }
    assert(r->sign() == Expression::Sign::Positive);
    assert(!divisor.isLowerThan(dividand));
  }
  return e;
}
Exemplo n.º 4
0
void testConstante()
{
    // c = 5
    Expression * c = new Constante(5);
    cout << "c = "<< *c << endl;
    Expression * cbis = c->clone();
    cout << "clone de c = " << *cbis << endl;
    delete c;
    delete cbis;
}
Exemplo n.º 5
0
    void Interpreter::fetch(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 2, "fetch need exactly two operands");

        Expression* exp = this->resolveVariable(instruction->getOperand(0));
        Expression* idx = this->resolveExpression(instruction->getOperand(1));

        const u32_t index = idx->is<NumericExpression>().ensure("Fetch-Index must be numeric")->getAs<u32_t>();
        Expression* val = exp->is<ArrayExpression>().ensure("Need an array to fetch")->fetch(index);

        this->pushStack(val->clone());
    }
Exemplo n.º 6
0
    void Interpreter::assign(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 2, "assign need exactly two operands");
        enforce(instruction->getOperand(0)->getType() == OpCode::VARIABLE, "Expected a variable");

        const size_t vi = this->getIndexOf(instruction->getOperand(0));
        debug("assign variable ", vi);
        Expression* exp = this->resolveExpression(instruction->getOperand(1));
#if DEBUG
        ::print(exp);
#endif
        this->assignVariable(vi, exp->clone());
    }
Exemplo n.º 7
0
    void Interpreter::append(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 2, "append need exactly two operands");

        const size_t vi = this->getIndexOf(instruction->getOperand(0));
        Expression* exp = this->fetchVariable(vi);
        if (!exp) {
            debug("No variable found, gen a new one");
            exp = new ArrayExpression();
            this->assignVariable(vi, exp);
        }

        Expression* val = this->resolveExpression(instruction->getOperand(1));
#if DEBUG
        ::print(val);
#endif
        exp->is<ArrayExpression>().ensure("Can only append on an array")->append(val->clone());
    }
Exemplo n.º 8
0
void testVariable1()
{
    // x = 3
    Variable x("x", 3.0);
    // y = 0
    Variable y("y");
    cout << x << " = " << x.eval() << endl;
    cout << y << " = " << y.eval() << endl;

    // exp = 1 + 2 * x
    Expression * exp = new Somme(new Constante(1.0), new Produit(new Constante(2.0), &x));
    // a = (y <- exp)
    Affectation * a = new Affectation(new Variable("y"), exp->clone());
    cout << *a << " = " << a->eval() << endl;
    cout << y << " = " << y.eval() << endl;

    Variable::effacerMemoire();
    delete exp; // OK car il existe un clone
    delete a;
    cout << "destruction automatique des variables locales allouees sur la PILE: ICI X et Y" << endl;
}
Exemplo n.º 9
0
 virtual AddExpression<T>* clone() const override
 {
     return new AddExpression<T>(e1->clone(), e2->clone());
 }
Exemplo n.º 10
0
    Expression* Interpreter::makeExpression(Instruction* instruction) {
        switch (instruction->getType()) {
            case Instruction::ADD: {
                Expression* lhs = this->resolveExpression(instruction->getOperand(0));
                Expression* rhs = this->resolveExpression(instruction->getOperand(1));

                return new AddExpression(lhs->clone(), rhs->clone());
            }
            case Instruction::SUB: {
                Expression* lhs = this->resolveExpression(instruction->getOperand(0));
                Expression* rhs = this->resolveExpression(instruction->getOperand(1));

                return new SubtractExpression(lhs->clone(), rhs->clone());
            }
            case Instruction::MUL: {
                Expression* lhs = this->resolveExpression(instruction->getOperand(0));
                Expression* rhs = this->resolveExpression(instruction->getOperand(1));

                return new MultiplyExpression(lhs->clone(), rhs->clone());
            }
            case Instruction::DIV: {
                Expression* lhs = this->resolveExpression(instruction->getOperand(0));
                Expression* rhs = this->resolveExpression(instruction->getOperand(1));

                return new DivideExpression(lhs->clone(), rhs->clone());
            }
            case Instruction::MOD: {
                Expression* lhs = this->resolveExpression(instruction->getOperand(0));
                Expression* rhs = this->resolveExpression(instruction->getOperand(1));

                return new ModuloExpression(lhs->clone(), rhs->clone());
            }
            case Instruction::NOT: {
                Expression* val = this->resolveExpression(instruction->getOperand(0));

                return new NotExpression(val->clone());
            }
            case Instruction::NEG: {
                Expression* val = this->resolveExpression(instruction->getOperand(0));

                return new NegateExpression(val->clone());
            }
            case Instruction::INC: {
                Expression* val = this->resolveExpression(instruction->getOperand(0));

                return new IncrementExpression(val->clone());
            }
            case Instruction::DEC: {
                Expression* val = this->resolveExpression(instruction->getOperand(0));

                return new DecrementExpression(val->clone());
            }
            default:
                error("Invalid math expression");
        }

        return nullptr;
    }
Exemplo n.º 11
0
    void Interpreter::push(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 1, "push need exactly one operand");

        Expression* exp = this->resolveExpression(instruction->getOperand(0));
        this->pushStack(exp->clone());
    }
Exemplo n.º 12
0
    void Interpreter::emplace(Instruction* instruction) {
        enforce(instruction->getOperandAmount() == 2, "emplace need exactly two operands");

        Expression* exp = this->resolveVariable(instruction->getOperand(0));
        Expression* val = this->resolveExpression(instruction->getOperand(1));

        auto idx = this->popStack();
        const u32_t index = idx->is<NumericExpression>().ensure("Index must be numeric")->getAs<u32_t>();

        exp->is<ArrayExpression>().ensure("Need an array for emplace")->emplace(index, val->clone());
    }
Exemplo n.º 13
0
Arquivo: qm.hpp Projeto: bindung/utils
	void add(const Expression &exp) {
		_exps.push_back(exp.clone());
	}