Skip to content

Commit

Permalink
Make Solve more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Feb 16, 2018
1 parent b6501e1 commit acd0c34
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
5 changes: 1 addition & 4 deletions expreduce/builtin_simplify.go
Expand Up @@ -2,9 +2,6 @@ package expreduce

func GetSimplifyDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "Simplify"})
defs = append(defs, Definition{
Name: "FullSimplify",
OmitDocumentation: true,
})
defs = append(defs, Definition{Name: "FullSimplify"})
return
}
1 change: 1 addition & 0 deletions expreduce/evalstate.go
Expand Up @@ -147,6 +147,7 @@ func (es *EvalState) Init(loadAllDefs bool) {
es.MarkSeen("System`CosIntegral")
es.MarkSeen("System`EllipticE")
es.MarkSeen("System`EllipticF")
es.MarkSeen("System`ProductLog")

es.MarkSeen("System`Cosh")
es.MarkSeen("System`Sinh")
Expand Down
8 changes: 4 additions & 4 deletions expreduce/resources.go

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion expreduce/resources/simplify.m
Expand Up @@ -86,5 +86,14 @@
]
};

FullSimplify[e_] := Simplify[e];
FullSimplify::usage = "`FullSimplify[expr]` attempts to perform full simplification operations on `expr`.";
FullSimplify[e_] := Simplify[e] //. {
E^(-x_Symbol)+E^x_Symbol:>2 Cosh[x]
};
Attributes[FullSimplify] = {Protected};
Tests`FullSimplify = {
ESimpleExamples[
ESameTest[2 Cosh[x], E^-x+E^x//FullSimplify],
]
};

18 changes: 15 additions & 3 deletions expreduce/resources/solve.m
Expand Up @@ -33,6 +33,9 @@
Switch[pow,
-1, {base -> 1/rhs},
2, {base -> -Sqrt[rhs]//PowerExpand, base -> Sqrt[rhs]//PowerExpand},
3, {base->rhs^(1/3),base->-(-1)^(1/3) rhs^(1/3),base->(-1)^(2/3) rhs^(1/3)},
4, {base->-rhs^(1/4),base->-I rhs^(1/4),base->I rhs^(1/4),base->rhs^(1/4)},
5, {base->rhs^(1/5),base->-(-1)^(1/5) rhs^(1/5),base->(-1)^(2/5) rhs^(1/5),base->-(-1)^(3/5) rhs^(1/5),base->(-1)^(4/5) rhs^(1/5)},
(* Not implemented yet *)
_Integer, SolveError,
(* Similar to the general case but without the message.*)
Expand Down Expand Up @@ -101,7 +104,11 @@
{lhs -> ConditionalExpression[
Tan[rhs], (Re[rhs] == -(Pi/2) && Im[rhs] < 0) || -(Pi/2) <
Re[rhs] < Pi/2 || (Re[rhs] == Pi/2 && Im[rhs] > 0)]};

applyInverse[Cosh[lhs_] -> rhs_, var_Symbol] :=
{lhs -> ConditionalExpression[-ArcCosh[rhs] + 2 I \[Pi] C[1],
C[1] \[Element] Integers], lhs ->
ConditionalExpression[ArcCosh[rhs] + 2 I \[Pi] C[1],
C[1] \[Element] Integers]}

(* Base case: *)

Expand Down Expand Up @@ -167,7 +174,7 @@
(*Sterling, L, Bundy, A, Byrd, L, O'Keefe, R & Silver, B 1982, Solving Symbolic Equations with PRESS. in*)
(*Computer Algebra - Lecture Notes in Computer Science. vol. 7. DOI: 10.1007/3-540-11607-9_13*)
(* Available at: http://www.research.ed.ac.uk/portal/files/413486/Solving_Symbolic_Equations_%20with_PRESS.pdf *)
Solve[eqn_Equal, var_Symbol] := Module[{degree, collected},
Solve[eqn_Equal, var_Symbol] := Module[{degree, collected, fullSimplified},
If[containsZeroOccurrences[eqn, var], Return[{}]];
(* Attempt isolation *)
If[containsOneOccurrence[eqn, var], Return[isolateInEqn[eqn, var]]];
Expand All @@ -182,6 +189,10 @@
collected = collect[eqn, var];
If[containsOneOccurrence[collected, var], Return[isolateInEqn[collected, var]]];

(*Try FullSimplify then solve.*)
fullSimplified = eqn // FullSimplify;
If[fullSimplified =!= eqn, Return[Solve[fullSimplified, var]]];

Print["Solve found no solutions"];
SolveFailed
];
Expand Down Expand Up @@ -211,7 +222,7 @@
Solve[False, _] := {};
Solve[True, _] := {{}};
Solve[{}, _] := {{}};
Solve[x_Symbol+E^x_Symbol==0, x_Symbol] := {{x->-ProductLog[1]}};
Solve[x_Symbol+a_.*E^x_Symbol==0, x_Symbol] := {{x->-ProductLog[a]}};
(* Currently needed for Apart: *)
(*Orderless matching would be nice here*)
Solve[{a_.*x_Symbol+b_.*y_Symbol==c_,d_.*x_Symbol+e_.*y_Symbol==f_},{x_Symbol,y_Symbol}] := {{x->-((c e-b f)/(b d-a e)),y->-((-c d+a f)/(b d-a e))}} /;FreeQ[{a,b,c,d,e,f},x]&&FreeQ[{a,b,c,d,e,f},y]
Expand Down Expand Up @@ -296,6 +307,7 @@

ExpreduceTestSolve[fn_] := Module[{testproblems, testi, runSolveTest, testp, res, nCorrect, isCorrect},
testproblems = ReadList[fn];
testproblems = Select[testproblems, (MatchQ[#[[2]],_Symbol])&];
Print[Length[testproblems]];

testi = 1;
Expand Down

0 comments on commit acd0c34

Please sign in to comment.