Skip to content

Commit ac0a4a0

Browse files
committed
WIP #947: Factor: check if degree is <= Config.MAX_POLYNOMIAL_DEGREE
1 parent f9ed1ee commit ac0a4a0

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/Algebra.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -5214,11 +5214,11 @@ private static IExpr factorComplex(IExpr expr, List<IExpr> varList, ISymbol head
52145214
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
52155215
JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
52165216
GenPolynomial<Complex<BigRational>> polyRat = jas.expr2JAS(expr, numeric2Rational);
5217-
return factorComplex(expr, polyRat, jas, head, cfac).eval(engine);
5217+
return factorComplex(polyRat, jas, head, cfac, expr).eval(engine);
52185218
} else {
52195219
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
52205220
GenPolynomial<BigRational> polyRat = jas.expr2JAS(expr, numeric2Rational);
5221-
return factorRational(polyRat, jas, head, expr);
5221+
return factorRational(polyRat, jas, head);
52225222
}
52235223
} catch (RuntimeException rex) {
52245224
LOGGER.debug("Algebra.factorComplex() failed", rex);
@@ -5237,10 +5237,16 @@ public static IExpr factor(IExpr arg1, EvalEngine engine) {
52375237
* @param head the head of the factorization result AST (typically <code>F.Times</code> or <code>
52385238
* F.List</code>)
52395239
* @param cfac
5240+
* @param original the original expression
52405241
* @return
52415242
*/
5242-
private static IExpr factorComplex(IExpr expr, GenPolynomial<Complex<BigRational>> polynomial,
5243-
JASConvert<? extends RingElem<?>> jas, ISymbol head, ComplexRing<BigRational> cfac) {
5243+
private static IExpr factorComplex(GenPolynomial<Complex<BigRational>> polynomial,
5244+
JASConvert<? extends RingElem<?>> jas, ISymbol head, ComplexRing<BigRational> cfac,
5245+
IExpr original) {
5246+
if (polynomial.degree() > Config.MAX_POLYNOMIAL_DEGREE) {
5247+
// Exponent ist out of bounds for function `1`.
5248+
return Errors.printMessage(S.Factor, "lrgexp", F.List(S.Factor));
5249+
}
52445250
FactorComplex<BigRational> factorAbstract = new FactorComplex<BigRational>(cfac);
52455251
SortedMap<GenPolynomial<Complex<BigRational>>, Long> map = factorAbstract.factors(polynomial);
52465252

@@ -5253,7 +5259,7 @@ private static IExpr factorComplex(IExpr expr, GenPolynomial<Complex<BigRational
52535259
if (entry.getValue().equals(1L) && map.size() <= 2
52545260
&& (key.equals(F.CNI) || key.equals(F.CI))) {
52555261
// hack: factoring -I and I out of an expression should give no new factorized expression
5256-
return expr;
5262+
return original;
52575263
}
52585264
result.append(F.Power(jas.complexPoly2Expr(entry.getKey()), F.ZZ(entry.getValue())));
52595265
}
@@ -5307,7 +5313,11 @@ public static IAST factorModulus(JASModInteger jas, ModLongRing modIntegerRing,
53075313
}
53085314

53095315
public static IAST factorRational(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas,
5310-
ISymbol head, IExpr original) {
5316+
ISymbol head) {
5317+
if (polyRat.degree() > Config.MAX_POLYNOMIAL_DEGREE) {
5318+
// Exponent ist out of bounds for function `1`.
5319+
return Errors.printMessage(S.Factor, "lrgexp", F.List(S.Factor));
5320+
}
53115321
Object[] objects = jas.factorTerms(polyRat);
53125322
GenPolynomial<edu.jas.arith.BigInteger> poly =
53135323
(GenPolynomial<edu.jas.arith.BigInteger>) objects[2];

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/RootsFunctions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ public static IAST rootsOfVariable(final IExpr expr, final IExpr denominator,
917917
}
918918
// }
919919
IASTAppendable newResult = F.ListAlloc(8);
920-
IAST factorRational = Algebra.factorRational(polyRat, jas, S.List, null);
920+
IAST factorRational = Algebra.factorRational(polyRat, jas, S.List);
921921
if (factorRational.isNIL()) {
922922
factorRational = F.Times(expr);
923923
}
@@ -936,7 +936,7 @@ public static IAST rootsOfVariable(final IExpr expr, final IExpr denominator,
936936
}
937937
} else {
938938
polyRat = jas.expr2JAS(temp, numericSolutions);
939-
IAST factorComplex = Algebra.factorRational(polyRat, jas, S.List, null);
939+
IAST factorComplex = Algebra.factorRational(polyRat, jas, S.List);
940940
if (factorComplex.isNIL()) {
941941
factorComplex = F.Times(expr);
942942
}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/Errors.java

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public static void initGeneralMessages() {
186186
"locked", "Symbol `1` is locked.", //
187187
"lowlen", "Required length `1` is smaller than maximum `2` of support of `3`.", //
188188
"lpsnf", "No solution can be found that satisfies the constraints.", //
189+
"lrgexp", "Exponent ist out of bounds for function `1`.", //
189190
"lslc", "Coefficient matrix and target vector or matrix do not have the same dimensions.", //
190191
"lstpat", "List or pattern matching a list expected at position `1` in `2`.", //
191192
"lvlist", "Local variable specification `1` is not a List.", //

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/ExprEvaluatorTestCase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void setUp() {
253253
Config.MAX_AST_SIZE = 20000;
254254
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
255255
Config.MAX_BIT_LENGTH = 200000;
256-
Config.MAX_POLYNOMIAL_DEGREE = 100;
256+
Config.MAX_POLYNOMIAL_DEGREE = 150;
257257
Config.FILESYSTEM_ENABLED = false;
258258
Config.ROUNDING_MODE = RoundingMode.HALF_EVEN;
259259
// fScriptEngine = fScriptManager.getEngineByExtension("m");

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -8627,9 +8627,9 @@ public void testFibonacci() {
86278627
// Fibonacci(11,{13,2,3,a})
86288628
check("Fibonacci(11,{13,2,3,a})", //
86298629
"{145336221161,5741,141481,1+15*a^2+35*a^4+28*a^6+9*a^8+a^10}");
8630-
// message Polynomial degree 101 exceeded
8631-
check("Fibonacci(101,11/9223372036854775807)", //
8632-
"Fibonacci(101,11/9223372036854775807)");
8630+
// message Polynomial degree 151 exceeded
8631+
check("Fibonacci(151,11/9223372036854775807)", //
8632+
"Fibonacci(151,11/9223372036854775807)");
86338633

86348634
// iter limit
86358635
check("Fibonacci(2147483647)", //
@@ -14485,8 +14485,8 @@ public void testLucasL() {
1448514485

1448614486
// slow
1448714487
// message Polynomial degree 101 exceeded
14488-
check("LucasL(101,1/1317624576693539401)", //
14489-
"LucasL(101,1/1317624576693539401)");
14488+
check("LucasL(151,1/1317624576693539401)", //
14489+
"LucasL(151,1/1317624576693539401)");
1449014490
check("LucasL(Quantity(1.2,\"m\"),2.718281828459045)", //
1449114491
"LucasL(1.2[m],2.71828)");
1449214492

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/PolynomialFunctionsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public void testGegenbauerC() {
9292

9393
// message Polynomial degree 101 exceeded
9494
check(
95-
"GegenbauerC(101,-9223372036854775807/9223372036854775808-I*9223372036854775808/9223372036854775807,z)", //
96-
"GegenbauerC(101,-9223372036854775807/9223372036854775808-I*9223372036854775808/\n"
95+
"GegenbauerC(151,-9223372036854775807/9223372036854775808-I*9223372036854775808/9223372036854775807,z)", //
96+
"GegenbauerC(151,-9223372036854775807/9223372036854775808-I*9223372036854775808/\n"
9797
+ "9223372036854775807,z)");
9898

9999
check("GegenbauerC(3, l, z)", //

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/SolveTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ public void testSolve() {
789789
"{{x->-81.08825721072805},{x->81.08825721072805}}");
790790
}
791791

792+
792793
@Test
793794
public void testNSolve() {
794795
check("NSolve(2*x^(x-3)==3^(x-2),x)", //
@@ -2065,6 +2066,13 @@ public void testIssue939() {
20652066
+ "3)*5^(2/3))/(3*Pi)^(1/3)}}");
20662067
}
20672068

2069+
@Test
2070+
public void testIssue947() {
2071+
// message: Exponent ist out of bounds for function Factor.
2072+
check("Solve(2^(1250000/x) == 500, x)", //
2073+
"{{x->(1250000*Log(2))/Log(500)}}");
2074+
}
2075+
20682076
/** The JUnit setup method */
20692077
@Override
20702078
public void setUp() {

0 commit comments

Comments
 (0)