Skip to content

Commit

Permalink
study 2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
buptkang committed Jun 17, 2016
1 parent 9fbcd06 commit 178c3ea
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 123 deletions.
8 changes: 8 additions & 0 deletions Equation/Equation.Eval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private Equation EvalTermInEquation(Equation rootEq, bool isLhs)
do
{
hasChange = false;

Equation localEq00 = currentEq.EvalTermInEquation(rootEq, true);
if (!localEq00.Equals(currentEq))
{
Expand All @@ -233,6 +234,13 @@ private Equation EvalTermInEquation(Equation rootEq, bool isLhs)
return satisfiable.Value;
}

var localEq000 = currentEq.ApplyTransitive2(rootEq, withEqRule, lineCheck);
if (!localEq000.Equals(currentEq))
{
hasChange = true;
currentEq = localEq000 as Equation;
}

var localObj1 = currentEq.ApplyTransitive(rootEq, withEqRule, lineCheck);

var localEq1 = localObj1 as Equation;
Expand Down
72 changes: 63 additions & 9 deletions Equation/Equation.Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static object ApplyTransitive(this Equation currentEq, Equation rootEq, b

var cloneEq = currentEq.Clone();

var rhsTerm = new Term(Expression.Add, new List<object>() {cloneEq.Rhs});
var rhsTerm = new Term(Expression.Add, new List<object>() { cloneEq.Rhs });

var lhsTerm = cloneEq.Lhs as Term;
Debug.Assert(lhsTerm != null);
Expand All @@ -164,7 +164,7 @@ public static object ApplyTransitive(this Equation currentEq, Equation rootEq, b
bool isNumber = LogicSharp.IsNumeric(temp);
if (isNumber)
{
var inverseRhs = new Term(Expression.Multiply, new List<object>() {-1, temp});
var inverseRhs = new Term(Expression.Multiply, new List<object>() { -1, temp });
lst.Remove(temp);
var rhsArgLst = rhsTerm.Args as List<object>;
Debug.Assert(rhsArgLst != null);
Expand All @@ -175,7 +175,7 @@ public static object ApplyTransitive(this Equation currentEq, Equation rootEq, b
var term = temp as Term;
if (term != null && !term.ContainsVar())
{
var inverseRhs = new Term(Expression.Multiply, new List<object>() {-1, temp});
var inverseRhs = new Term(Expression.Multiply, new List<object>() { -1, temp });
lst.Remove(i);
var rhsArgLst = rhsTerm.Args as List<object>;
Debug.Assert(rhsArgLst != null);
Expand Down Expand Up @@ -244,7 +244,7 @@ public static object ApplyTransitive(this Equation currentEq, Equation rootEq, b
rootEq._innerLoop.Add(traceStep);
localEq = cloneEq;
#endregion
}
}
}

//Mutliply Inverse
Expand Down Expand Up @@ -364,11 +364,65 @@ public static object ApplyTransitive(this Equation currentEq, Equation rootEq, b
#endregion
}


return localEq;
}

public static object ApplyTransitive2(this Equation currentEq,
Equation rootEq, bool withEqRule, bool lineCheck = false)
{
Equation localEq = currentEq;
object lhs = currentEq.Lhs;
object rhs = currentEq.Rhs;

if (!withEqRule) return localEq;

//Divide Transform
object newLhs, newRhs;
if (SatisfyTransitiveCondition5(lhs, rhs, out newLhs, out newRhs))
{
var cloneEq = currentEq.Clone();
var newEq = new Equation(newLhs, newRhs);
string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
string appliedRule = EquationsRule.Rule(
EquationsRule.EquationRuleType.Transitive,
localEq, newEq);

string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule);
rootEq._innerLoop.Add(traceStep);
localEq = newEq;
return localEq;
}
return localEq;
}

private static bool SatisfyTransitiveCondition5(
object lhs, object rhs, out object newLhs, out object newRhs)
{
newLhs = null;
newRhs = null;
var lhsTerm = lhs as Term;
if (lhsTerm != null && lhsTerm.Op.Method.Name.Equals("Divide"))
{
var lst = lhsTerm.Args as List<object>;
Debug.Assert(lst != null);
Debug.Assert(lst.Count == 2);
newLhs = lst[0];
newRhs = new Term(Expression.Multiply, new List<object> { lst[1], rhs });
return true;
}
var rhsTerm = rhs as Term;
if (rhsTerm != null && rhsTerm.Op.Method.Name.Equals("Divide"))
{
var lst = rhsTerm.Args as List<object>;
newLhs = new Term(Expression.Multiply, new List<object> { lst[1], lhs });
newRhs = lst[0];
return true;
}
return false;
}

/*
* Move sqare root head to the rhs
* E.g x^2=4 -> x=4^(0.5)
Expand Down Expand Up @@ -465,10 +519,10 @@ private static bool SatifyTransitiveCondition0(object lhs, object rhs)

private static bool SatisfyTransitiveCondition1(object lhs, object rhs)
{
if (!0.0.Equals(rhs) && !0.Equals(rhs))
{
return true;
}
if (!0.0.Equals(rhs) && !0.Equals(rhs))
{
return true;
}
return false;
}
}
Expand Down
Loading

0 comments on commit 178c3ea

Please sign in to comment.