mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-22 07:10:40 +01:00
[poincare] Use shortcut rational construction
Change-Id: If7e40a694ab58cb4c31227ec739cc28f76ee78b5
This commit is contained in:
@@ -23,7 +23,7 @@ Expression * Division::clone() const {
|
||||
}
|
||||
|
||||
Expression * Division::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
Power * p = new Power(operand(1), new Rational(Integer(-1)), false);
|
||||
Power * p = new Power(operand(1), new Rational(-1), false);
|
||||
Multiplication * m = new Multiplication(operand(0), p, false);
|
||||
p->deepSimplify(context, angleUnit);
|
||||
detachOperands();
|
||||
@@ -57,14 +57,14 @@ Expression * Division::shallowBeautify(Context & context, AngleUnit angleUnit) {
|
||||
return replaceWith(o, true);
|
||||
} else {
|
||||
assert(operandIndex == 1);
|
||||
replaceOperand((Expression *)cos->parent(), new Rational(Integer(-1)), true);
|
||||
replaceOperand((Expression *)cos->parent(), new Rational(-1), true);
|
||||
}
|
||||
} else {
|
||||
if (operandIndex == 0) {
|
||||
return replaceWith(editableOperand(0), true);
|
||||
} else {
|
||||
assert(operandIndex == 1);
|
||||
replaceOperand(cos, new Rational(Integer(1)), true);
|
||||
replaceOperand(cos, new Rational(1), true);
|
||||
}
|
||||
}
|
||||
k++;
|
||||
|
||||
@@ -48,7 +48,7 @@ Expression * Logarithm::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
}
|
||||
// log(1) = 0;
|
||||
if (r->isOne()) {
|
||||
return replaceWith(new Rational(Integer(0)), true);
|
||||
return replaceWith(new Rational(0), true);
|
||||
}
|
||||
Expression * n = splitInteger(r->numerator(), false, context, angleUnit);
|
||||
Expression * d = splitInteger(r->denominator(), true, context, angleUnit);
|
||||
@@ -63,7 +63,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co
|
||||
assert(!i.isZero());
|
||||
assert(!i.isNegative());
|
||||
if (i.isOne()) {
|
||||
return new Rational(Integer(0));
|
||||
return new Rational(0);
|
||||
}
|
||||
assert(!i.isOne());
|
||||
if (Arithmetic::k_primorial32.isLowerThan(i)) {
|
||||
@@ -73,7 +73,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co
|
||||
if (!isDenominator) {
|
||||
return e;
|
||||
}
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e, false);
|
||||
Multiplication * m = new Multiplication(new Rational(-1), e, false);
|
||||
return m;
|
||||
}
|
||||
Integer factors[Arithmetic::k_maxNumberOfPrimeFactors];
|
||||
|
||||
@@ -115,7 +115,7 @@ Expression * Multiplication::shallowSimplify(Context& context, AngleUnit angleUn
|
||||
mergeOperands(static_cast<Multiplication *>(o));
|
||||
index = 0;
|
||||
} else if (o->type() == Type::Rational && static_cast<const Rational *>(o)->isZero()) {
|
||||
return replaceWith(new Rational(Integer(0)), true);
|
||||
return replaceWith(new Rational(0), true);
|
||||
}
|
||||
}
|
||||
factorize(context, angleUnit);
|
||||
@@ -273,7 +273,7 @@ Expression * Multiplication::distributeOnOperandAtIndex(int i, Context & context
|
||||
}
|
||||
|
||||
const Expression * Multiplication::CreateExponent(Expression * e) {
|
||||
Expression * n = e->type() == Type::Power ? e->operand(1)->clone() : new Rational(Integer(1));
|
||||
Expression * n = e->type() == Type::Power ? e->operand(1)->clone() : new Rational(1);
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -472,7 +472,7 @@ Expression * Multiplication::mergeNegativePower(Context & context, AngleUnit ang
|
||||
if (m->numberOfOperands() == 0) {
|
||||
return this;
|
||||
}
|
||||
Power * p = new Power(m, new Rational(Integer(-1)), false);
|
||||
Power * p = new Power(m, new Rational(-1), false);
|
||||
m->sortOperands(SimplificationOrder);
|
||||
m->squashUnaryHierarchy();
|
||||
const Expression * multOperand[1] = {p};
|
||||
|
||||
@@ -20,7 +20,7 @@ Expression * NthRoot::clone() const {
|
||||
}
|
||||
|
||||
Expression * NthRoot::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
Power * invIndex = new Power(operand(1), new Rational(Integer(-1)), false);
|
||||
Power * invIndex = new Power(operand(1), new Rational(-1), false);
|
||||
Power * p = new Power(operand(0), invIndex, false);
|
||||
invIndex->shallowSimplify(context, angleUnit);
|
||||
detachOperands();
|
||||
|
||||
@@ -31,7 +31,7 @@ Complex<T> Opposite::compute(const Complex<T> c, AngleUnit angleUnit) {
|
||||
Expression * Opposite::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
const Expression * op = operand(0);
|
||||
detachOperand(op);
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(-1)), op, false);
|
||||
Multiplication * m = new Multiplication(new Rational(-1), op, false);
|
||||
replaceWith(m, true);
|
||||
return m->shallowSimplify(context, angleUnit);
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
const Rational * b = static_cast<const Rational *>(operand(1));
|
||||
// x^0
|
||||
if (b->isZero()) {
|
||||
return replaceWith(new Rational(Integer(1)), true);
|
||||
return replaceWith(new Rational(1), true);
|
||||
}
|
||||
// x^1
|
||||
if (b->isOne()) {
|
||||
@@ -151,7 +151,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
// 0^x
|
||||
if (a->isZero()) {
|
||||
if (operand(1)->sign() == Sign::Positive) {
|
||||
return replaceWith(new Rational(Integer(0)), true);
|
||||
return replaceWith(new Rational(0), true);
|
||||
}
|
||||
if (operand(1)->sign() == Sign::Negative) {
|
||||
return replaceWith(new Undefined(), true);
|
||||
@@ -159,7 +159,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
}
|
||||
// 1^x
|
||||
if (a->isOne()) {
|
||||
return replaceWith(new Rational(Integer(1)), true);
|
||||
return replaceWith(new Rational(1), true);
|
||||
}
|
||||
// p^q with p, q rationals
|
||||
if (operand(1)->type() == Type::Rational) {
|
||||
@@ -189,7 +189,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
Expression * rCopy = r->clone();
|
||||
Expression * factor = m->editableOperand(i);
|
||||
if (factor->sign() == Sign::Negative) {
|
||||
m->replaceOperand(factor, new Rational(Integer(-1)), false);
|
||||
m->replaceOperand(factor, new Rational(-1), false);
|
||||
factor->setSign(Sign::Positive, context, angleUnit);
|
||||
} else {
|
||||
m->removeOperand(factor, false);
|
||||
@@ -267,7 +267,7 @@ Expression * Power::CreateSimplifiedIntegerRationalPower(Integer i, Rational * r
|
||||
assert(!i.isZero());
|
||||
assert(r->sign() == Sign::Positive);
|
||||
if (i.isOne()) {
|
||||
return new Rational(Integer(1));
|
||||
return new Rational(1);
|
||||
}
|
||||
if (Arithmetic::k_primorial32.isLowerThan(i)) {
|
||||
r->setSign(isDenominator ? Sign::Negative : Sign::Positive);
|
||||
@@ -318,7 +318,7 @@ Expression * Power::shallowBeautify(Context& context, AngleUnit angleUnit) {
|
||||
// X^-y -> 1/(X->shallowBeautify)^y
|
||||
if (operand(1)->sign() == Sign::Negative) {
|
||||
Expression * p = cloneDenominator(context, angleUnit);
|
||||
Division * d = new Division(new Rational(Integer(1)), p, false);
|
||||
Division * d = new Division(new Rational(1), p, false);
|
||||
p->shallowSimplify(context, angleUnit);
|
||||
replaceWith(d, true);
|
||||
return d->shallowBeautify(context, angleUnit);
|
||||
|
||||
@@ -29,7 +29,7 @@ Complex<T> Subtraction::compute(const Complex<T> c, const Complex<T> d) {
|
||||
}
|
||||
|
||||
Expression * Subtraction::shallowSimplify(Context& context, AngleUnit angleUnit) {
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(-1)), operand(1), false);
|
||||
Multiplication * m = new Multiplication(new Rational(-1), operand(1), false);
|
||||
Addition * a = new Addition(operand(0), m, false);
|
||||
m->shallowSimplify(context, angleUnit);
|
||||
detachOperands();
|
||||
|
||||
@@ -35,7 +35,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context
|
||||
if (e->type() == Expression::Type::Cosine) {
|
||||
return e->shallowSimplify(context, angleUnit);
|
||||
} else {
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false);
|
||||
Multiplication * m = new Multiplication(new Rational(-1), e->clone(), false);
|
||||
m->editableOperand(1)->shallowSimplify(context, angleUnit);
|
||||
return e->replaceWith(m, true)->shallowSimplify(context, angleUnit);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context
|
||||
unaryCoefficient *= -1;
|
||||
}
|
||||
Expression * simplifiedCosine = e->shallowSimplify(context, angleUnit); // recursive
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(unaryCoefficient)), simplifiedCosine->clone(), false);
|
||||
Multiplication * m = new Multiplication(new Rational(unaryCoefficient), simplifiedCosine->clone(), false);
|
||||
return simplifiedCosine->replaceWith(m, true)->shallowSimplify(context, angleUnit);
|
||||
}
|
||||
assert(r->sign() == Expression::Sign::Positive);
|
||||
@@ -121,12 +121,12 @@ Expression * Trigonometry::shallowSimplifyInverseFunction(Expression * e, Contex
|
||||
op->shallowSimplify(context, angleUnit);
|
||||
}
|
||||
if (e->type() == Expression::Type::ArcCosine) {
|
||||
Expression * pi = angleUnit == Expression::AngleUnit::Radian ? static_cast<Expression *>(new Symbol(Ion::Charset::SmallPi)) : static_cast<Expression *>(new Rational(Integer(180)));
|
||||
Expression * pi = angleUnit == Expression::AngleUnit::Radian ? static_cast<Expression *>(new Symbol(Ion::Charset::SmallPi)) : static_cast<Expression *>(new Rational(180));
|
||||
Subtraction * s = new Subtraction(pi, e->clone(), false);
|
||||
s->editableOperand(1)->shallowSimplify(context, angleUnit);
|
||||
return e->replaceWith(s, true)->shallowSimplify(context, angleUnit);
|
||||
} else {
|
||||
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false);
|
||||
Multiplication * m = new Multiplication(new Rational(-1), e->clone(), false);
|
||||
m->editableOperand(1)->shallowSimplify(context, angleUnit);
|
||||
return e->replaceWith(m, true)->shallowSimplify(context, angleUnit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user