Skip to content

Commit

Permalink
WIP #914 restrict Solve expressions leafCount()
Browse files Browse the repository at this point in the history
- restrict Laguerre solver to max. 10000 iterations
- factor only expressions with leafs <
Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT / 2
- eliminate one variable expressions with leafs <
Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT / 2
  • Loading branch information
axkr committed Feb 26, 2024
1 parent 5944c34 commit 24905a0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4374,6 +4374,13 @@ private static IExpr e2ObjArg(IAST ast, final IExpr base, final IExpr exponent)
return F.NIL;
}

/**
* Evaluate expressions <code>Times(...) ^ exponent</code>.
*
* @param baseTimes
* @param exponent
* @return
*/
private static IExpr powerTimesN(IAST baseTimes, final IExpr exponent) {
// for non-rational exponents
IASTAppendable filterAST = baseTimes.copyHead();
Expand Down Expand Up @@ -4414,6 +4421,13 @@ private static IExpr powerTimesN(IAST baseTimes, final IExpr exponent) {
return F.NIL;
}

/**
* Evaluate expressions <code>Times(...) ^ (fractional-number)</code>
*
* @param baseTimes
* @param exponent
* @return
*/
private static IExpr powerTimesFraction(IAST baseTimes, final IFraction exponent) {
IASTAppendable result = F.TimesAlloc(baseTimes.argSize());
IASTAppendable rest = F.TimesAlloc(baseTimes.argSize());
Expand Down Expand Up @@ -4485,14 +4499,14 @@ private static IExpr powerTimesFraction(IAST baseTimes, final IFraction exponent
result.append(F.Power(rest.oneIdentity1(), exponent));
}
if (exponent2Times != null) {
for (Map.Entry<IFraction, IASTAppendable> entry : exponent2Times.entrySet()) {
IFraction exp = entry.getKey();
rest = entry.getValue();
if (rest.argSize() > 0) {
result.append(F.Power(rest.oneIdentity1(), exp));
for (Map.Entry<IFraction, IASTAppendable> entry : exponent2Times.entrySet()) {
IFraction exp = entry.getKey();
rest = entry.getValue();
if (rest.argSize() > 0) {
result.append(F.Power(rest.oneIdentity1(), exp));
}
}
}
}
return result;
}
return F.NIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,6 @@ public static IAST roots(final IExpr arg1, boolean numericSolutions, IAST variab
*/
protected static IAST findRoots(double... coefficients) {
try {
for (int j = 0; j < coefficients.length; j++) {
if (!Double.isFinite(coefficients[j])) {
return F.NIL;
}
}
org.hipparchus.complex.Complex[] complexRoots = allComplexRootsLaguerre(coefficients);
if (complexRoots == null) {
return F.NIL;
Expand Down Expand Up @@ -1025,7 +1020,7 @@ private static org.hipparchus.complex.Complex[] allComplexRootsLaguerre(
LaguerreSolver solver = new LaguerreSolver(Config.DEFAULT_ROOTS_CHOP_DELTA);
// see https://github.com/Hipparchus-Math/hipparchus/issues/177 for initial value
// https://stackoverflow.com/q/65960318
return solver.solveAllComplex(coefficients, 100_000, 1.0);
return solver.solveAllComplex(coefficients, 10_000, 1.0);
} catch (MathRuntimeException mre) {
// mre.printStackTrace();
// org.hipparchus.exception.MathIllegalStateException: maximal count (100,000) exceeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private static IExpr extractVariableRecursive(IExpr exprWithVariable, IExpr expr
}
} else {
int size = ast.size();
if (size > 2) {
if (size > 2 && ast.leafCount() < Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT / 2) {
if (exprWithoutVariable.isZero() && ast.isPlus()) {
IAST elimZeroPlus = F.binaryAST2(elimzeroplus, ast, variable);
IExpr result = zeroPlusMatcher().apply(elimZeroPlus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,12 @@ private IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZero,
}
}
}
termEQZero = S.Factor.of(engine, termEQZero);
if (termEQZero.isTimes()) {
solveTimesAST((IAST) termEQZero, termsEqualZero, inequationsList, numericFlag,
variables, multipleValues, engine, subSolutionSet, i);
if (termEQZero.leafCount() < Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT / 2) {
termEQZero = S.Factor.of(engine, termEQZero);
if (termEQZero.isTimes()) {
solveTimesAST((IAST) termEQZero, termsEqualZero, inequationsList, numericFlag,
variables, multipleValues, engine, subSolutionSet, i);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2010,8 +2010,8 @@ public void testIssue902() {
@Test
public void testIssue914() {
// TODO
// check("Solve(5==35500*(1+x/4)^(4*7),x)", //
// "");
check("Solve(5==35500*(1+x/4)^(4*7),x)", //
"Solve((1+x/4)^28==1/7100,x)");
check("Solve({A==20^300,A==26158/5},{A})", //
"{}");
}
Expand Down

0 comments on commit 24905a0

Please sign in to comment.