[poincare] Cap prime factorizations: cap only prime factors

Change-Id: I6384ced8021d264cbf3a903e1574aeb0a22b7c3f
This commit is contained in:
Émilie Feral
2017-11-28 13:29:27 +01:00
parent d886ce251e
commit 65a87d77d6
6 changed files with 10 additions and 13 deletions

View File

@@ -11,12 +11,11 @@ public:
static void PrimeFactorization(const Integer * i, Integer * outputFactors, Integer * outputCoefficients, int outputLength);
constexpr static int k_numberOfPrimeFactors = 1000;
constexpr static int k_maxNumberOfPrimeFactors = 32;
static const Integer k_biggestPrimeFactorizedInteger;
static const Integer k_primorial32;
private:
/* When decomposing an integer into primes factors, we look for its prime
* factors among integer from 2 to 100000000. */
constexpr static int k_biggestPrimeFactor = 1E8;
* factors among integer from 2 to 10000. */
constexpr static int k_biggestPrimeFactor = 10000;
};
}

View File

@@ -3,7 +3,6 @@
namespace Poincare {
const Integer Arithmetic::k_biggestPrimeFactorizedInteger("1000000000000"); // 1E12
const Integer Arithmetic::k_primorial32("525896479052627740771371797072411912900610967452630");
Integer Arithmetic::LCM(const Integer * a, const Integer * b) {

View File

@@ -235,7 +235,7 @@ void print_prime_factorization(Integer * outputFactors, Integer * outputCoeffici
std::cout << outputFactors[index].approximate<double>();
std::cout << "^";
std::cout << outputCoefficients[index].approximate<double>();
std::cout << "+";
std::cout << "*";
}
std::cout <<" "<< std::endl;
}

View File

@@ -148,10 +148,9 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co
return new Rational(0);
}
assert(!i.isOne());
if (Arithmetic::k_primorial32.isLowerThan(i) || Arithmetic::k_biggestPrimeFactorizedInteger.isLowerThan(i)) {
/* We do not want to break i in prime factor because
* - either it might be take too many factors... More than k_maxNumberOfPrimeFactors.
* - Or, it might takes too much time */
if (Arithmetic::k_primorial32.isLowerThan(i)) {
/* We do not want to break i in prime factor because it might be take too
* many factors... More than k_maxNumberOfPrimeFactors. */
Expression * e = clone();
e->replaceOperand(e->operand(0), new Rational(i), true);
if (!isDenominator) {

View File

@@ -426,10 +426,10 @@ Expression * Power::CreateSimplifiedIntegerRationalPower(Integer i, Rational * r
}
Integer absI = i;
absI.setNegative(false);
if (Arithmetic::k_primorial32.isLowerThan(absI) || Arithmetic::k_biggestPrimeFactorizedInteger.isLowerThan(absI)) {
if (Arithmetic::k_primorial32.isLowerThan(absI)) {
r->setSign(isDenominator ? Sign::Negative : Sign::Positive);
/* We do not want to break i in prime factor because it might be take too
* many factors... More than k_maxNumberOfPrimeFactors; or too much time!*/
* many factors... More than k_maxNumberOfPrimeFactors. */
return new Power(new Rational(i), r->clone(), false);
}
Integer factors[Arithmetic::k_maxNumberOfPrimeFactors];

View File

@@ -83,6 +83,6 @@ QUIZ_CASE(poincare_arithmetic) {
int coefficients2[3] = {2,4,2};
assert_prime_factorization_equals_to(Integer(122500), factors2, coefficients2, 3);
int factors3[8] = {3,7,11, 13, 19, 3607, 3803, 52579};
int coefficients3[8] = {4,2,2,2,2,2,2,2};
assert_prime_factorization_equals_to(Integer("15241578780673678515622620750190521"), factors3, coefficients3, 8);
int coefficients3[8] = {4,2,2,2,2,2,2,1};
assert_prime_factorization_equals_to(Integer("289879586539753105148873518899"), factors3, coefficients3, 8);
}