mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[poincare] Rational constructor: avoid useless copy
This commit is contained in:
@@ -73,10 +73,10 @@ class Rational : public Number {
|
||||
public:
|
||||
/* The constructor build a irreductible fraction */
|
||||
Rational(const RationalNode * node) : Number(node) {}
|
||||
Rational(Integer num, Integer den);
|
||||
Rational(Integer & num, Integer & den);
|
||||
Rational(const Integer & numerator);
|
||||
Rational(native_int_t i);
|
||||
Rational(native_int_t i, native_int_t j) : Rational(Integer(i), Integer(j)) {}
|
||||
Rational(native_int_t i, native_int_t j);
|
||||
|
||||
// TreeNode
|
||||
RationalNode * node() const { return static_cast<RationalNode *>(Number::node()); }
|
||||
|
||||
@@ -103,7 +103,9 @@ Expression BinomialCoefficient::shallowReduce(Context & context, Preferences::An
|
||||
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)));
|
||||
Integer nMinusI = Integer::Subtraction(n, Integer(i));
|
||||
Integer kMinusI = Integer::Subtraction(k, Integer(i));
|
||||
Rational factor = Rational(nMinusI, kMinusI);
|
||||
result = Rational::Multiplication(result, factor);
|
||||
}
|
||||
// As we cap the n < k_maxNValue = 300, result < binomial(300, 150) ~2^89
|
||||
|
||||
@@ -48,7 +48,8 @@ Expression FracPart::shallowReduce(Context & context, Preferences::AngleUnit ang
|
||||
Rational r = static_cast<Rational &>(c);
|
||||
IntegerDivision div = Integer::Division(r.signedIntegerNumerator(), r.integerDenominator());
|
||||
assert(!div.remainder.isInfinity());
|
||||
Expression result = Rational(div.remainder, r.integerDenominator());
|
||||
Integer rDenominator = r.integerDenominator();
|
||||
Expression result = Rational(div.remainder, rDenominator);
|
||||
replaceWithInPlace(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -741,13 +741,15 @@ Expression Power::CreateSimplifiedIntegerRationalPower(Integer i, Rational r, bo
|
||||
return Power(Rational(i), r.clone());
|
||||
}
|
||||
Rational p1 = Rational(r2);
|
||||
Integer one = isDenominator ? Integer(-1) : Integer(1);
|
||||
Rational p2 = Rational(one, r.integerDenominator());
|
||||
Integer oneExponent = isDenominator ? Integer(-1) : Integer(1);
|
||||
Integer rDenominator = r.integerDenominator();
|
||||
Rational p2 = Rational(oneExponent, rDenominator);
|
||||
Power p = Power(p1, p2);
|
||||
if (r1.isEqualTo(Integer(1)) && !i.isNegative()) {
|
||||
return p;
|
||||
}
|
||||
Rational r3 = isDenominator ? Rational(Integer(1), r1) : Rational(r1);
|
||||
Integer one(1);
|
||||
Rational r3 = isDenominator ? Rational(one, r1) : Rational(r1);
|
||||
Multiplication m;
|
||||
m.addChildAtIndexInPlace(r3, 0, 0);
|
||||
if (!r2.isOne()) {
|
||||
@@ -783,10 +785,11 @@ Expression Power::removeSquareRootsFromDenominator(Context & context, Preference
|
||||
return result;
|
||||
}
|
||||
Power sqrt = Power(Rational(pq), Rational(1, 2));
|
||||
Integer one(1);
|
||||
if (castedChild1.isHalf()) {
|
||||
result = Multiplication(Rational(Integer(1), q), sqrt);
|
||||
result = Multiplication(Rational(one, q), sqrt);
|
||||
} else {
|
||||
result = Multiplication(Rational(Integer(1), p), sqrt); // We use here the assertion that p != 0
|
||||
result = Multiplication(Rational(one, p), sqrt); // We use here the assertion that p != 0
|
||||
}
|
||||
sqrt.shallowReduce(context, angleUnit);
|
||||
}
|
||||
@@ -857,7 +860,8 @@ Expression Power::removeSquareRootsFromDenominator(Context & context, Preference
|
||||
return result; // Escape
|
||||
}
|
||||
numerator = numerator.deepReduce(context, angleUnit);
|
||||
result = Multiplication(numerator, Rational(Integer(1), denominator));
|
||||
Integer one(1);
|
||||
result = Multiplication(numerator, Rational(one, denominator));
|
||||
}
|
||||
|
||||
if (!result.isUninitialized()) {
|
||||
|
||||
@@ -79,7 +79,9 @@ Expression PredictionInterval::shallowReduce(Context & context, Preferences::Ang
|
||||
}
|
||||
/* [r0-1.96*sqrt(r0*(1-r0)/r1), r0+1.96*sqrt(r0*(1-r0)/r1)]*/
|
||||
// Compute numerator = r0*(1-r0)
|
||||
Rational numerator = Rational::Multiplication(r0, Rational(Integer::Subtraction(r0.integerDenominator(), r0.unsignedIntegerNumerator()), r0.integerDenominator()));
|
||||
Integer factorNumerator = Integer::Subtraction(r0.integerDenominator(), r0.unsignedIntegerNumerator());
|
||||
Integer factorDenominator = r0.integerDenominator();
|
||||
Rational numerator = Rational::Multiplication(r0, Rational(factorNumerator, factorDenominator));
|
||||
if (numerator.numeratorOrDenominatorIsInfinity()) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ Expression RationalNode::denominator(Context & context, Preferences::AngleUnit a
|
||||
|
||||
// Constructors
|
||||
|
||||
Rational::Rational(Integer num, Integer den) : Number() {
|
||||
Rational::Rational(Integer & num, Integer & den) : Number() {
|
||||
assert(!den.isZero());
|
||||
if (!num.isOne() && !den.isOne()) {
|
||||
// Avoid computing GCD if possible
|
||||
@@ -172,6 +172,12 @@ Rational::Rational(native_int_t i) : Number() {
|
||||
new (this) Rational(&absI, 1, &one, 1, i < 0);
|
||||
}
|
||||
|
||||
Rational::Rational(native_int_t i, native_int_t j) : Number() {
|
||||
Integer iInteger(i);
|
||||
Integer jInteger(j);
|
||||
new (this) Rational(iInteger, jInteger);
|
||||
}
|
||||
|
||||
bool Rational::numeratorOrDenominatorIsInfinity() const {
|
||||
return signedIntegerNumerator().isInfinity() || integerDenominator().isInfinity();
|
||||
}
|
||||
|
||||
@@ -60,7 +60,8 @@ Expression Round::shallowReduce(Context & context, Preferences::AngleUnit angleU
|
||||
Rational mult = Rational::Multiplication(r1, err);
|
||||
IntegerDivision d = Integer::Division(mult.signedIntegerNumerator(), mult.integerDenominator());
|
||||
Integer rounding = d.quotient;
|
||||
if (Rational::NaturalOrder(Rational(d.remainder, mult.integerDenominator()), Rational(1,2)) >= 0) {
|
||||
Integer multDenominator = mult.integerDenominator();
|
||||
if (Rational::NaturalOrder(Rational(d.remainder, multDenominator), Rational(1,2)) >= 0) {
|
||||
rounding = Integer::Addition(rounding, Integer(1));
|
||||
}
|
||||
Rational result = Rational::Multiplication(rounding, Rational::IntegerPower(Rational(1,10), r2.signedIntegerNumerator()));
|
||||
|
||||
@@ -115,7 +115,8 @@ Expression Trigonometry::shallowReduceDirectFunction(Expression & e, Context& co
|
||||
return e;
|
||||
}
|
||||
// Step 4.5. Build the new result.
|
||||
Expression newR = Rational(div.remainder, r.integerDenominator());
|
||||
Integer rDenominator = r.integerDenominator();
|
||||
Expression newR = Rational(div.remainder, rDenominator);
|
||||
Expression rationalParent = angleUnit == Preferences::AngleUnit::Radian ? e.childAtIndex(0) : e;
|
||||
rationalParent.replaceChildAtIndexInPlace(0, newR);
|
||||
newR.shallowReduce(context, angleUnit);
|
||||
|
||||
Reference in New Issue
Block a user