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; }
Expression * BinomialCoefficient::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op0 = editableOperand(0); Expression * op1 = editableOperand(1); #if MATRIX_EXACT_REDUCING if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) { return replaceWith(new Undefined(), true); } #endif if (op0->type() == Type::Rational) { Rational * r0 = static_cast<Rational *>(op0); if (!r0->denominator().isOne() || r0->numerator().isNegative()) { return replaceWith(new Undefined(), true); } } if (op1->type() == Type::Rational) { Rational * r1 = static_cast<Rational *>(op1); if (!r1->denominator().isOne() || r1->numerator().isNegative()) { return replaceWith(new Undefined(), true); } } if (op0->type() != Type::Rational || op1->type() != Type::Rational) { return this; } Rational * r0 = static_cast<Rational *>(op0); Rational * r1 = static_cast<Rational *>(op1); Integer n = r0->numerator(); Integer k = r1->numerator(); if (n.isLowerThan(k)) { return replaceWith(new Undefined(), true); } /* if n is too big, we do not reduce to avoid too long computation. * The binomial coefficient will be evaluate approximatively later */ if (Integer(k_maxNValue).isLowerThan(n)) { return this; } Rational result(1); Integer kBis = Integer::Subtraction(n, k); k = kBis.isLowerThan(k) ? kBis : k; int clippedK = k.extractedInt(); // Authorized because k < n < k_maxNValue for (int i = 0; i < clippedK; i++) { Rational factor = Rational(Integer::Subtraction(n, Integer(i)), Integer::Subtraction(k, Integer(i))); result = Rational::Multiplication(result, factor); } return replaceWith(new Rational(result), true); }