Skip to content

Commit

Permalink
Better simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jan 18, 2018
1 parent 3b0ce8c commit 25c3224
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
14 changes: 7 additions & 7 deletions expreduce/resources.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions expreduce/resources/power.m
Expand Up @@ -59,6 +59,7 @@
Power[-1, 1/2] := I;
4^(-1/2) := 1/2;
16^(-1/2) := 1/4;
16^(1/2) := 4;
Power[Rational[a_?Positive,b_?Positive], 1/2] := Power[a, 1/2] * Power[b, -1/2];
Power[Power[x_, y_Rational], -1] := Power[x, -y];
(*We may want to deprecate this in favor of the general definition.*)
Expand Down Expand Up @@ -201,6 +202,7 @@
Attributes[Sqrt] = {Listable, NumericFunction, Protected};
Sqrt[a_Integer?Negative] := I*Sqrt[-a];
Sqrt[-a_?NumberQ] := I*Sqrt[a];
Sqrt[a_Integer*b_Plus] := Sqrt[Abs[a]]*Sqrt[(a/Abs[a])*b] /; a != 0;
Sqrt[a_Real?Positive] := a^.5;
Sqrt[x_] := Which[
(*Normally we would define these directly, but right now "x_" is
Expand All @@ -216,6 +218,8 @@
ESameTest[I * Sqrt[3], Sqrt[-3]],
ESameTest[1, Sqrt[1]],
ESameTest[0, Sqrt[0]]
], ETests[
ESameTest[Sqrt[2] Sqrt[-2-x^y], (-2)(x^y+2)//Sqrt],
]
};

Expand Down Expand Up @@ -259,8 +263,8 @@
]
};

ExpandAll::usage = "`Expand[expr]` attempts to expand `expr` at all levels.";
ExpandAll[a]:= Map[Expand, a, {0, Infinity}];
ExpandAll::usage = "`ExpandAll[expr]` attempts to expand `expr` at all levels.";
ExpandAll[a_] := Map[Expand, a, {0, Infinity}];
Attributes[ExpandAll] = {Protected};
Tests`ExpandAll = {
ESimpleExamples[
Expand Down
11 changes: 7 additions & 4 deletions expreduce/resources/simplify.m
Expand Up @@ -35,12 +35,14 @@
And[x1___, a_, x2___, Or[x3___, !a_, x4___], x5___] :> And[a, x1, x2, Or[x3, x4], x5],
And[x1___, Or[x2___, !a_, x3___], x4___, a_, x5___] :> And[a, x1, Or[x2, x3], x4, x5]
};
Simplify[exp_] := Module[{e = exp, expanded},
simplifyInner[exp_] := Module[{e = exp, tryVal},
e = booleanSimplify[e];
expanded = e // Expand;
If[LeafCount[expanded] < LeafCount[e], e = expanded];
tryVal = e // Expand;
If[LeafCount[tryVal] < LeafCount[e], e = tryVal];
e = Replace[e, Sqrt[inner_] :> Sqrt[FactorTerms[inner]]];
e
];
Simplify[exp_] := Map[simplifyInner, exp, {0, Infinity}];
Attributes[Simplify] = {Protected};
Tests`Simplify = {
ESimpleExamples[
Expand Down Expand Up @@ -79,7 +81,8 @@
ESameTest[a || c || Not[b], c || a || Not[b] // Simplify // Sort],
ESameTest[False, And[x1, a, x2, Not[Or[x3, a, x4]], x5] // Simplify],
ESameTest[a && x1 && x2 && x5, And[x1, a, x2, Or[x3, a, x4], x5] // Simplify],
ESameTest[a && b, a&&b&&a//Simplify]
ESameTest[a && b, a&&b&&a//Simplify],
ESameTest[(-32+32 x-16 x^2)^9, (16 + 8 (-6 + 4 x - 2 x^2))^9 // Simplify],
]
};

Expand Down
7 changes: 4 additions & 3 deletions expreduce/resources/solve.m
Expand Up @@ -122,7 +122,7 @@
];
isolateInEqn[eqn_Equal, var_Symbol] := Module[{isolated},
isolated = {#}& /@ isolate[Rule @@ eqn, var];
If[AllTrue[isolated, (Head[#[[1]]] == Rule)&], Return[isolated//Sort]];
If[AllTrue[isolated, (Head[#[[1]]] == Rule)&], Return[isolated//Simplify//Sort]];
Print["isolation procedure failed"];
isolated
];
Expand All @@ -149,7 +149,7 @@
collected
];

solveQuadratic[a_.*x_^2 + b_.*x_ + c_., x_] := {{x->(-b-Sqrt[b^2-4 a c])/(2 a)},{x->(-b+Sqrt[b^2-4 a c])/(2 a)}};
solveQuadratic[a_.*x_^2 + b_.*x_ + c_., x_] := {{x->(-b-Sqrt[b^2-4 a c])/(2 a)},{x->(-b+Sqrt[b^2-4 a c])/(2 a)}}/;FreeQ[{a,b,c},x];

(* Following method described in: *)
(*Sterling, L, Bundy, A, Byrd, L, O'Keefe, R & Silver, B 1982, Solving Symbolic Equations with PRESS. in*)
Expand All @@ -163,7 +163,7 @@
poly = eqn[[1]]-eqn[[2]];
If[PolynomialQ[poly, var],
degree = Exponent[poly, var];
If[degree === 2, Return[solveQuadratic[poly, var]]];
If[degree === 2, Return[solveQuadratic[poly//Expand, var]//Simplify//Sort]];
];

collected = collect[eqn, var];
Expand All @@ -189,6 +189,7 @@
ESimpleExamples[
ESameTest[{{x->Log[y]/Log[a+b]}}, Solve[(a+b)^x==y,x]],
ESameTest[{{x -> -Sqrt[-3 + y]}, {x -> Sqrt[-3 + y]}}, Solve[y == x^2 + 3, x]],
ESameTest[{{y->1-Sqrt[-2+2 x-x^2]},{y->1+Sqrt[-2+2 x-x^2]}}, Solve[2==x^2+y^2+(x-2)^2+(y-2)^2,y]],
(*ESameTest[{{x -> (-b - Sqrt[b^2 - 4 a c])/(2 a)}, {x -> (-b + Sqrt[b^2 - 4 a c])/(2 a)}}, Solve[a*x^2 + b*x + c == 0, x]],*)
(*ESameTest[{{x -> (-b - Sqrt[b^2 - 4 a c + 4 a d])/(2 a)}, {x -> (-b + Sqrt[b^2 - 4 a c + 4 a d])/(2 a)}}, Print[a,b,c,d,x];Solve[a*x^2 + b*x + c == d, x]]*)
], ETests[
Expand Down

0 comments on commit 25c3224

Please sign in to comment.