mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[poincare] Power: do no simplify Power whose result is too big (fix bug
1.00666666666667^60 = undef)
This commit is contained in:
@@ -25,7 +25,7 @@ public:
|
||||
template<typename T> static Complex<T> compute(const Complex<T> c, const Complex<T> d);
|
||||
private:
|
||||
constexpr static int k_maxNumberOfTermsInExpandedMultinome = 25;
|
||||
constexpr static int k_maxIntegerPower = 100;
|
||||
constexpr static int k_maxExactPowerMatrix = 100;
|
||||
/* Property */
|
||||
Expression * setSign(Sign s, Context & context, AngleUnit angleUnit) override;
|
||||
/* Layout */
|
||||
@@ -52,10 +52,9 @@ private:
|
||||
static bool TermIsARationalSquareRootOrRational(const Expression * e);
|
||||
static const Rational * RadicandInExpression(const Expression * e);
|
||||
static const Rational * RationalFactorInExpression(const Expression * e);
|
||||
static bool RationalExponentShouldNotBeReduced(const Rational * r);
|
||||
static bool RationalExponentShouldNotBeReduced(const Rational * b, const Rational * r);
|
||||
/* Evaluation */
|
||||
constexpr static int k_maxApproximatePowerMatrix = 1000;
|
||||
constexpr static int k_maxExactPowerMatrix = 100;
|
||||
template<typename T> static Matrix * computeOnComplexAndMatrix(const Complex<T> * c, const Matrix * n) { return nullptr; }
|
||||
template<typename T> static Matrix * computeOnMatrixAndComplex(const Matrix * m, const Complex<T> * d);
|
||||
template<typename T> static Matrix * computeOnMatrices(const Matrix * m, const Matrix * n) { return nullptr; }
|
||||
|
||||
@@ -282,9 +282,7 @@ Expression * Power::shallowReduce(Context& context, AngleUnit angleUnit) {
|
||||
// p^q with p, q rationals
|
||||
if (!letPowerAtRoot && operand(1)->type() == Type::Rational) {
|
||||
Rational * exp = static_cast<Rational *>(editableOperand(1));
|
||||
/* First, we check that the simplification does not involve too complex power
|
||||
* of integers (ie 3^999) that would take too much time to compute. */
|
||||
if (RationalExponentShouldNotBeReduced(exp)) {
|
||||
if (RationalExponentShouldNotBeReduced(a, exp)) {
|
||||
return this;
|
||||
}
|
||||
return simplifyRationalRationalPower(this, a, exp, context, angleUnit);
|
||||
@@ -368,9 +366,9 @@ Expression * Power::shallowReduce(Context& context, AngleUnit angleUnit) {
|
||||
Addition * a = static_cast<Addition *>(editableOperand(1));
|
||||
// Check is b is rational
|
||||
if (a->operand(0)->type() == Type::Rational) {
|
||||
/* First, we check that the simplification does not involve too complex power
|
||||
* of integers (ie 3^999) that would take too much time to compute. */
|
||||
if (RationalExponentShouldNotBeReduced(static_cast<const Rational *>(a->operand(0)))) {
|
||||
const Rational * rationalBase = static_cast<const Rational *>(operand(0));
|
||||
const Rational * rationalIndex = static_cast<const Rational *>(a->operand(0));
|
||||
if (RationalExponentShouldNotBeReduced(rationalBase, rationalIndex)) {
|
||||
return this;
|
||||
}
|
||||
Power * p1 = static_cast<Power *>(clone());
|
||||
@@ -810,10 +808,25 @@ bool Power::isNthRootOfUnity() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Power::RationalExponentShouldNotBeReduced(const Rational * r) {
|
||||
bool Power::RationalExponentShouldNotBeReduced(const Rational * b, const Rational * r) {
|
||||
/* We check that the simplification does not involve too complex power of
|
||||
* integers (ie 3^999, 120232323232^50) that would take too much time to
|
||||
* compute:
|
||||
* - we cap the exponent at k_maxExactPowerMatrix
|
||||
* - we cap the resulting power at DBL_MAX
|
||||
* The complexity of computing a power of rational is mainly due to computing
|
||||
* the GCD of the resulting numerator and denominator. Euclide algorithm's
|
||||
* complexity is apportionned to the number of decimal digits in the smallest
|
||||
* integer. */
|
||||
Integer maxIntegerExponent = r->numerator();
|
||||
maxIntegerExponent.setNegative(false);
|
||||
if (Integer::NaturalOrder(maxIntegerExponent, Integer(k_maxIntegerPower)) > 0) {
|
||||
if (Integer::NaturalOrder(maxIntegerExponent, Integer(k_maxExactPowerMatrix)) > 0) {
|
||||
return true;
|
||||
}
|
||||
double index = maxIntegerExponent.approximate<double>();
|
||||
double powerNumerator = std::pow(std::fabs(b->numerator().approximate<double>()), index);
|
||||
double powerDenominator = std::pow(std::fabs(b->denominator().approximate<double>()), index);
|
||||
if (std::isnan(powerNumerator) || std::isnan(powerDenominator) || std::isinf(powerNumerator) || std::isinf(powerDenominator)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -35,11 +35,12 @@ Expression * Round::shallowReduce(Context& context, AngleUnit angleUnit) {
|
||||
if (!r2->denominator().isOne()) {
|
||||
return replaceWith(new Undefined(), true);
|
||||
}
|
||||
if (Power::RationalExponentShouldNotBeReduced(r2)) {
|
||||
const Rational ten(10);
|
||||
if (Power::RationalExponentShouldNotBeReduced(&ten, r2)) {
|
||||
return this;
|
||||
}
|
||||
Rational err = Rational::Power(Rational(10), r2->numerator());
|
||||
Rational mult = Rational::Multiplication(*r1, Rational(err));
|
||||
Rational err = Rational::Power(ten, r2->numerator());
|
||||
Rational mult = Rational::Multiplication(*r1, err);
|
||||
IntegerDivision d = Integer::Division(mult.numerator(), mult.denominator());
|
||||
Integer rounding = d.quotient;
|
||||
if (Rational::NaturalOrder(Rational(d.remainder, mult.denominator()), Rational(1,2)) >= 0) {
|
||||
|
||||
@@ -27,6 +27,12 @@ QUIZ_CASE(poincare_power_evaluate) {
|
||||
|
||||
Complex<double> f[1] = {Complex<double>::Float(std::exp(-M_PI_2))};
|
||||
assert_parsed_expression_evaluates_to("I^I", f);
|
||||
|
||||
Complex<float> g[1] = {Complex<float>::Float(1.489846)};
|
||||
assert_parsed_expression_evaluates_to("1.006666666666667^60", g);
|
||||
|
||||
Complex<double> h[1] = {Complex<double>::Float(1.48984570830164)};
|
||||
assert_parsed_expression_evaluates_to("1.006666666666667^60", h);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_power_simplify) {
|
||||
@@ -84,4 +90,5 @@ QUIZ_CASE(poincare_power_simplify) {
|
||||
assert_parsed_expression_simplify_to("(5*P+R(2))^(-5)", "1/(4*R(2)+100*P+500*R(2)*P^2+2500*P^3+3125*R(2)*P^4+3125*P^5)");
|
||||
assert_parsed_expression_simplify_to("(1+R(2)+R(3))^5", "296+224*R(2)+184*R(3)+120*R(6)");
|
||||
assert_parsed_expression_simplify_to("(P+R(2)+R(3)+x)^(-3)", "1/(11*R(2)+9*R(3)+15*x+6*R(6)*x+3*R(2)*x^2+3*R(3)*x^2+x^3+15*P+6*R(6)*P+6*R(2)*x*P+6*R(3)*x*P+3*x^2*P+3*R(2)*P^2+3*R(3)*P^2+3*x*P^2+P^3)");
|
||||
assert_parsed_expression_simplify_to("1.006666666666667^60", "(1006666666666667/1000000000000000)^60");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user