Skip to content

Commit d7c3903

Browse files
committed
WIP #1146 add ConditionalExpression solutions without cross-check
- see `SolveData#solveTimesAST()`
1 parent 56cb17c commit d7c3903

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.function.Predicate;
66
import java.util.function.Supplier;
77
import org.matheclipse.core.basic.Config;
8-
import org.matheclipse.core.basic.OperationSystem;
98
import org.matheclipse.core.builtin.Algebra;
109
import org.matheclipse.core.builtin.BooleanFunctions;
1110
import org.matheclipse.core.builtin.RootsFunctions;

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ private static IExpr solveNumeric(IExpr expr, boolean isNumeric, EvalEngine engi
708708
*
709709
* @param termsEqualZeroList a list of expressions which equals zero.
710710
* @param variable the variable which should be eliminated in the term
711+
* @param multipleValues if <code>true</code> multiple results are returned as list of values
711712
* @param numeric evaluate in numericMode
712713
* @param engine
713714
* @return
@@ -984,7 +985,7 @@ private IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZero,
984985
IExpr termEQZero = termsEqualZero.get(i);
985986
if (termEQZero.isTimes()) {
986987
solveTimesAST((IAST) termEQZero, termsEqualZero, inequationsList, numericFlag,
987-
variables, multipleValues, engine, subSolutionSet, i);
988+
variables, multipleValues, subSolutionSet, i, engine);
988989
} else {
989990
if (termEQZero.isAST()) {
990991
// try factoring
@@ -1006,7 +1007,7 @@ private IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZero,
10061007
termEQZero = S.Factor.of(engine, termEQZero);
10071008
if (termEQZero.isTimes()) {
10081009
solveTimesAST((IAST) termEQZero, termsEqualZero, inequationsList, numericFlag,
1009-
variables, multipleValues, engine, subSolutionSet, i);
1010+
variables, multipleValues, subSolutionSet, i, engine);
10101011
}
10111012
}
10121013
}
@@ -1106,9 +1107,24 @@ private static IASTMutable crossChecking(IASTMutable termsEqualZero, IASTMutable
11061107
return result;
11071108
}
11081109

1110+
/**
1111+
* Analyze the <code>Time(..., ...)</code> expression in the given list of equations. If the
1112+
* expression is of the form <code>Times(..., ...) == 0</code>, set each factor equal to
1113+
* <code>0</code> and solve the resulting equations recursively.
1114+
*
1115+
* @param times the <code>Times(..., ...)</code> expression
1116+
* @param termsEqualZeroList the list of expressions, which should equal <code>0</code>
1117+
* @param inequationsList a list of inequality constraints
1118+
* @param numericFlag if <code>true</code>, try to find a numeric solution
1119+
* @param variables the variables for which the equations should be solved
1120+
* @param multipleValues if <code>true</code> multiple results are returned as list of values
1121+
* @param subSolutionSet a set of rules which should solve the terms
1122+
* @param i the index of the current equation in the list
1123+
* @param engine the evaluation engine
1124+
*/
11091125
private void solveTimesAST(IAST times, IAST termsEqualZeroList, IAST inequationsList,
1110-
boolean numericFlag, IAST variables, boolean multipleValues, EvalEngine engine,
1111-
Set<IExpr> subSolutionSet, int i) {
1126+
boolean numericFlag, IAST variables, boolean multipleValues, Set<IExpr> subSolutionSet,
1127+
int i, EvalEngine engine) {
11121128
IAST temp;
11131129
for (int j = 1; j < times.size(); j++) {
11141130
if (!times.get(j).isFree(Predicates.in(variables), true)) {
@@ -1127,6 +1143,9 @@ private void solveTimesAST(IAST times, IAST termsEqualZeroList, IAST inequations
11271143
if (replaceAll.isPlusTimesPower() && //
11281144
S.PossibleZeroQ.ofQ(engine, replaceAll)) {
11291145
subSolutionSet.add(solution);
1146+
} else if (!solution.isFree(S.ConditionalExpression)) {
1147+
// TODO create some cross-check for C(1),... variables?
1148+
subSolutionSet.add(solution);
11301149
}
11311150
}
11321151
}

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,8 @@ public void testSolveHO4() {
15111511
// TODO check result
15121512
check(
15131513
"Solve(Cos(x)+ Cos(x)^3 + Cos(x)^5 - 3*Cos(x)*Sin(x)^2 - 10*Cos(x)^3*Sin(x)^2 + 5*Cos(x)*Sin(x)^4 ==0,x)", //
1514-
"{{x->ConditionalExpression(Pi/8+2*Pi*C(1),C(1)∈Integers)}}");
1514+
"{{x->ConditionalExpression(-Pi/2+2*Pi*C(1),C(1)∈Integers)},{x->ConditionalExpression(Pi/\n"
1515+
+ "8+2*Pi*C(1),C(1)∈Integers)},{x->ConditionalExpression(Pi/2+2*Pi*C(1),C(1)∈Integers)}}");
15151516
}
15161517

15171518
@Test
@@ -1924,11 +1925,13 @@ public void testSolveTrigs() {
19241925
// \end{aligned}
19251926
checkSolveLatex("9 \\sin x+6 \\cos x-3 \\sin 2 x+\\cos 2 x=8",
19261927
"Cos(2*x)+6*Cos(x)+9*Sin(x)-3*Sin(2*x)==8",
1927-
"{{x->ConditionalExpression(-ArcTan(3)+2*Pi*C(1),C(1)∈Integers)}}");
1928+
"{{x->ConditionalExpression(Pi*(1/2+2*C(1)),C(1)∈Integers)},{x->ConditionalExpression(-ArcTan(\n" //
1929+
+ "3)+2*Pi*C(1),C(1)∈Integers)}}");
19281930

19291931
checkSolveLatex("\\sin 2 x-\\cos 2 x=3 \\sin x+\\cos x-2",
19301932
"-Cos(2*x)+Sin(2*x)==-2+Cos(x)+3*Sin(x)",
1931-
"{{x->ConditionalExpression(Pi*(-1/4+2*C(1)),C(1)∈Integers)}}");
1933+
"{{x->ConditionalExpression(Pi*(-1/4+2*C(1)),C(1)∈Integers)},{x->ConditionalExpression(Pi*(\n"
1934+
+ "1/6+2*C(1)),C(1)∈Integers)},{x->ConditionalExpression(Pi*(5/6+2*C(1)),C(1)∈Integers)}}");
19321935

19331936
// TODO: wrong parsed input
19341937
// checkSolveLatex("1+\\cot 2 x=\\frac{1-\\cos 2 x}{\\sin ^2 2 x}",
@@ -2275,6 +2278,16 @@ public void testSolveInverseMultivaluedArcSin() {
22752278
"{{x->0}}");
22762279
}
22772280

2281+
@Test
2282+
public void testSolveGithub1147() {
2283+
// issue 1147
2284+
check("Solve(-2*x*Sin(x^2)==0, x)", //
2285+
"{{x->0},{x->ConditionalExpression(Sqrt(2*Pi)*Sqrt(C(1)),C(1)∈Integers)},{x->ConditionalExpression(-Sqrt(\n" //
2286+
+ "2*Pi)*Sqrt(C(1)),C(1)∈Integers)},{x->ConditionalExpression(-Sqrt(Pi+2*Pi*C(1)),C(\n" //
2287+
+ "1)∈Integers)},{x->ConditionalExpression(Sqrt(Pi+2*Pi*C(1)),C(1)∈Integers)}}");
2288+
2289+
}
2290+
22782291

22792292
/** The JUnit setup method */
22802293
@Override

0 commit comments

Comments
 (0)