Skip to content

Commit ae97418

Browse files
committed
WIP #918 Solve adds wrong zero solution for equation-systems
- Solve#crossChecking() wasn't called for all solution paths
1 parent b3f090e commit ae97418

File tree

2 files changed

+33
-10
lines changed
  • symja_android_library/matheclipse-core/src

2 files changed

+33
-10
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/reflection/system/Solve.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ protected IASTAppendable analyzeSublistRecursive(ArrayList<ExprAnalyzer> analyze
214214
} else if (numericFlag) {
215215
listOfRules = findRoot(exprAnalyzer, engine);
216216
if (listOfRules.isPresent()) {
217-
listOfRules =
218-
exprAnalyzer.mapOnOriginal(exprAnalyzer.getOriginalExpr(), listOfRules);
217+
listOfRules = exprAnalyzer.mapOnOriginal(exprAnalyzer.getOriginalExpr(), listOfRules);
219218
}
220219
}
221220
if (listOfRules.isPresent()) {
@@ -618,22 +617,23 @@ public IExpr solveRecursive(IASTMutable termsEqualZeroList, IAST inequationsList
618617
// expensive recursion try
619618
IExpr firstEquation = termsEqualZeroList.arg1();
620619

620+
IASTMutable reducedEqualZeroList = termsEqualZeroList.copyAppendable();
621621
for (int i = 1; i < variables.size(); i++) {
622622
IExpr variable = variables.get(i);
623623

624624
IAST[] reduced = Eliminate.eliminateOneVariable(F.list(F.Equal(firstEquation, F.C0)),
625625
variable, true, engine);
626626
if (reduced != null) {
627627
variables = variables.splice(i);
628-
termsEqualZeroList = termsEqualZeroList.removeAtCopy(1);
628+
reducedEqualZeroList = reducedEqualZeroList.removeAtCopy(1);
629629
// oneVariableRule = ( firstVariable -> reducedExpression )
630630
IAST oneVariableRule = reduced[1];
631-
IExpr replaced = termsEqualZeroList.replaceAll(oneVariableRule);
631+
IExpr replaced = reducedEqualZeroList.replaceAll(oneVariableRule);
632632
if (replaced.isList()) {
633633
IExpr subResult = solveRecursive((IASTMutable) replaced, inequationsList, numericFlag,
634634
variables, engine);
635635
if (subResult.isListOfLists()) {
636-
return F.mapList((IAST) subResult, t -> {
636+
IASTMutable result = F.mapList((IAST) subResult, t -> {
637637
final IAST listOfRules = (IAST) t;
638638
IExpr replaceAllExpr = oneVariableRule.second().replaceAll(listOfRules);
639639
if (replaceAllExpr.isPresent()) {
@@ -642,12 +642,13 @@ public IExpr solveRecursive(IASTMutable termsEqualZeroList, IAST inequationsList
642642
}
643643
return F.NIL;
644644
});
645+
return crossChecking(termsEqualZeroList, result, engine);
645646
} else if (subResult.isList()) { // important for NSolve
646647
replaced = oneVariableRule.second().replaceAll((IAST) subResult);
647648
if (replaced.isPresent()) {
648-
IASTAppendable appendable = ((IAST) subResult).copyAppendable();
649-
appendable.append(F.Rule(variable, replaced));
650-
return appendable;
649+
IASTAppendable result = ((IAST) subResult).copyAppendable();
650+
result.append(F.Rule(variable, replaced));
651+
return crossChecking(termsEqualZeroList, result, engine);
651652
}
652653
}
653654
}
@@ -941,7 +942,6 @@ private IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZero,
941942
solveTimesAST((IAST) termEQZero, termsEqualZero, inequationsList, numericFlag,
942943
variables, multipleValues, engine, subSolutionSet, i);
943944
}
944-
945945
}
946946
}
947947
}
@@ -1019,6 +1019,10 @@ private static IASTMutable crossChecking(IASTMutable termsEqualZero, IASTMutable
10191019
break;
10201020
}
10211021
} else {
1022+
if (possibleZero.isIndeterminate()) {
1023+
removedPositions[untilPosition++] = j;
1024+
break;
1025+
}
10221026
if (!engine.evalTrue(F.PossibleZeroQ(replaceAll))) {
10231027
removedPositions[untilPosition++] = j;
10241028
break;

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ public void testSolveIssue130() {
14131413
public void testSolveIssue413() {
14141414
// eval quiet without message
14151415
check("Solve({8.0*E - 9 == x0/x1, x0==x1^4.0},{x0, x1}) ", //
1416-
"{{x1->-1.16797+I*2.02298,x0->-14.88721+I*25.78541},{x1->-1.16797+I*(-2.02298),x0->-14.88721+I*(-25.78541)},{x1->0.0,x0->0.0},{x1->2.33594,x0->29.77443}}");
1416+
"{{x1->-1.16797+I*2.02298,x0->-14.88721+I*25.78541},{x1->-1.16797+I*(-2.02298),x0->-14.88721+I*(-25.78541)},{x1->2.33594,x0->29.77443}}");
14171417
// eval quiet without message
14181418
check("Solve({x0^2.0*Sin(x1)==5.0,x1^3.0*Cos(x0)==5.0},{x0,x1}) ", //
14191419
"Solve({x0^2.0*Sin(x1)==5.0,x1^3.0*Cos(x0)==5.0},{x0,x1})");
@@ -2015,10 +2015,29 @@ public void testIssue902() {
20152015

20162016
@Test
20172017
public void testIssue914() {
2018+
// TODO
2019+
// check("Solve(5==35500*(1+x/4)^(4*7),x)", //
2020+
// "");
20182021
check("Solve({A==20^300,A==26158/5},{A})", //
20192022
"{}");
20202023
}
20212024

2025+
// @Test
2026+
// public void testIssue916() {
2027+
// TODO
2028+
// check("ProductLog(-Log(7)/5243338316756303634461458718861951455543)//N", //
2029+
// "-3.7112*10^-40");
2030+
// check("Solve(7^x-x==47, x)", //
2031+
// "{{x->-47},{x->-(47*Log(7)+ProductLog(-1,-Log(7)/\n" //
2032+
// + "5243338316756303634461458718861951455543))/Log(7)}}");
2033+
// }
2034+
2035+
@Test
2036+
public void testIssue918() {
2037+
check("Solve({x==Pi*y^2, 5==x/y}, {x,y})", //
2038+
"{{y->5/Pi,x->25/Pi}}");
2039+
}
2040+
20222041

20232042
/** The JUnit setup method */
20242043
@Override

0 commit comments

Comments
 (0)