public void Equation_EqGoal_Satisfy() { //2=2 var eq = new Equation(2, 2); object obj; bool result = eq.IsEqGoal(out obj); Assert.False(result); //3=4 eq = new Equation(3, 4); result = eq.IsEqGoal(out obj); Assert.False(result); //3=5-2 var term = new Term(Expression.Add, new List<object>() { 5, -2 }); eq = new Equation(3, term); result = eq.IsEqGoal(out obj); Assert.False(result); //x = x //TODO var variable = new Var('x'); eq = new Equation(variable, variable); /* result = eq.IsEqGoal(out obj); Assert.True(result);*/ //x = 2x-x //TODO term = new Term(Expression.Multiply, new List<object>() { 2, variable }); var term0 = new Term(Expression.Multiply, new List<object>() { -1, variable }); var term1 = new Term(Expression.Add, new List<object>() { term, term0 }); eq = new Equation(variable, term1); /* result = eq.IsEqGoal(out obj); Assert.True(result);*/ }
private static bool SatisfyGeneralForm(Equation equation, out Circle circle) { circle = null; var term = equation.Lhs as Term; if (term != null && term.Op.Method.Name.Equals("Add")) { var lst = term.Args as List<object>; if (lst != null && lst.Count == 3) { bool isNum = LogicSharp.IsNumeric(lst[2]); if (isNum) { double dNum; LogicSharp.IsDouble(lst[2], out dNum); dNum *= -1; object coeffX, coeffY; bool xTerm1 = IsXSquareTerm(lst[0], out coeffX); bool yTerm1 = IsYSquareTerm(lst[1], out coeffY); if (xTerm1 && yTerm1) { var pt = new Point(coeffX, coeffY); circle = new Circle(pt, Math.Pow(dNum, 0.5)); return true; } } } } return false; }
//copy constructor public Equation(Equation eq) { EqLabel = eq.EqLabel; var lhsVar = eq.Lhs as Var; var lhsTerm = eq.Lhs as Term; if (lhsVar != null) { Lhs = lhsVar.Clone(); } else if (lhsTerm != null) { Lhs = lhsTerm.Clone(); } else { Lhs = eq.Lhs; } var rhsVar = eq.Rhs as Var; var rhsTerm = eq.Rhs as Term; if (rhsVar != null) { Rhs = rhsVar.Clone(); } else if (rhsTerm != null) { Rhs = rhsTerm.Clone(); } else { Rhs = eq.Rhs; } IsGenerated = eq.IsGenerated; }
public EqGoal(Equation eq) : base(eq) { Debug.Assert(Lhs is Var); Debug.Assert(Rhs != null); Functor = LogicSharp.Equal()(Lhs, Rhs); }
public void Test_Unify_3() { // a=2, b=a var a = new Var("a"); var b = new Var("b"); var eqGoal = new EqGoal(a, 2); var goalNode = new GoalNode(eqGoal); var equation = new Equation(b, a); object obj; bool result = RelationLogic.ConstraintCheck(goalNode, equation, null, out obj); Assert.True(result); Assert.NotNull(obj); }
public void Test_Reify_1() { //a = 1, a*b = -1; //true positive var graph = new RelationGraph(); var a = new Var("a"); var b = new Var("b"); var eqGoal = new EqGoal(a, 1); var lhsTerm = new Term(Expression.Multiply, new List<object>() { a, b }); var equation = new Equation(lhsTerm, -1); graph.AddNode(eqGoal); var en = graph.AddNode(equation) as EquationNode; Assert.NotNull(en); Assert.True(en.Equation.CachedEntities.Count == 1); Assert.True(graph.Nodes.Count == 3); }
public void Test_Algebra_OneVariable_Linear_Goal_Gen_1() { //x = 2+3 var x = new Var('x'); var term = new Term(Expression.Add, new List<object>() { 2, 3 }); var eq = new Equation(x, term); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var eqGoal = obj as EqGoal; Assert.NotNull(eqGoal); Assert.True(eqGoal.Traces.Count == 1); Assert.True(eqGoal.Lhs.Equals(x)); Assert.True(eqGoal.Rhs.Equals(5)); Assert.True(result); }
/// <summary> /// a+1=, 2+3+5= /// </summary> /// <param name="term"></param> /// <param name="obj"></param> /// <returns></returns> public bool ConstraintSatisfy(Term term, out object obj) { var evalObj = term.Eval(); var generatedEq = new Equation(term, evalObj); generatedEq.ImportTrace(term); var evalTerm = evalObj as Term; var evalVar = evalObj as Var; var dict = new Dictionary<object, object>(); var connectLst = new List<GraphNode>(); if (evalTerm != null) { for (var i = 0; i < _nodes.Count; i++) { var goalNode = _nodes[i] as GoalNode; if (goalNode != null) { if (evalTerm.ContainsVar((EqGoal)goalNode.Goal)) { connectLst.Add(goalNode); } } } } if (evalVar != null) { for (var i = 0; i < _nodes.Count; i++) { var goalNode = _nodes[i] as GoalNode; if (goalNode != null) { var eqGoal = goalNode.Goal as EqGoal; Debug.Assert(eqGoal != null); var lhsVar = eqGoal.Lhs as Var; Debug.Assert(lhsVar != null); if (lhsVar.Equals(evalVar)) { connectLst.Add(goalNode); } } } } dict.Add(connectLst, generatedEq); obj = dict; return true; }
public void Test_Arith_3() { //1+2+3=6 var lhs = new Term(Expression.Add, new List<object>() { 1, 2, 3 }); var equation = new Equation(lhs, 6); bool result = equation.ContainsVar(); Assert.False(result); Assert.True(equation.ToString().Equals("1+2+3=6")); object outputEq; bool? evalResult = equation.Eval(out outputEq); Assert.NotNull(evalResult); Assert.True(evalResult.Value); Assert.NotNull(outputEq); Assert.True(outputEq.ToString().Equals("6=6")); Assert.True(equation.Traces.Count == 1); }
public void Test_Algebra_OneVariable_Linear_1() { //1+2=x var x = new Var('x'); var lhs = new Term(Expression.Add, new List<object>() { 1, 2 }); var equation = new Equation(lhs, x); bool result = equation.ContainsVar(); Assert.True(result); Assert.True(equation.ToString().Equals("1+2=x")); equation.Eval(); Assert.True(equation.CachedEntities.Count == 1); var outputEq = equation.CachedEntities.ToList()[0] as Equation; Assert.NotNull(outputEq); Assert.True(outputEq.ToString().Equals("x=3")); Assert.True(outputEq.Traces.Count == 1); }
public void Test_Unify_1() { //a = 1, a*b = -1; //true positive var a = new Var("a"); var b = new Var("b"); var eqGoal = new EqGoal(a, 1); var lhsTerm = new Term(Expression.Multiply, new List<object>() {a, b}); var equation = new Equation(lhsTerm, -1); var eqNode = new EquationNode(equation); object obj; bool result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj); Assert.True(result); Assert.NotNull(obj); }
/// <summary> /// Trace from Line General Form to Slope-Intercept Form /// ax+by+c=0 => y=mx+b /// </summary> /// <param name="target"></param> /// <returns></returns> public static void FromLineGeneralFormToSlopeIntercept(LineSymbol ls) { string strategy = "Given a general form line ax+by+c=0, the slope-intercept form of it is y = -(a/b)x-c/b."; var line = ls.Shape as Line; Debug.Assert(line != null); var newLS = new LineSymbol(line); newLS.OutputType = LineType.SlopeIntercept; var x = new Var("x"); var y = new Var("y"); var xTerm = new Term(Expression.Multiply, new List<object>() { line.A, x }); var yTerm = new Term(Expression.Multiply, new List<object>() { line.B, y }); var oldEqLeft = new Term(Expression.Add, new List<object>() { xTerm, yTerm, line.C }); var oldEq = new Equation(oldEqLeft, 0); var invertXTerm = new Term(Expression.Multiply, new List<object>() { -1, xTerm }); var intertZ = new Term(Expression.Multiply, new List<object>() { -1, line.C }); var internalRight = new Term(Expression.Add, new List<object>() { invertXTerm, intertZ }); var internalEq = new Equation(yTerm, internalRight); var finalXTerm = new Term(Expression.Multiply, new List<object>() { line.Slope, x }); var finalRight = new Term(Expression.Add, new List<object>() { finalXTerm, line.Intercept }); var lastEq = new Equation(yTerm, finalRight); string step1metaRule = "Given the line general form ax+by+c=0, move x term and Constant term to the right side of equation."; string step1AppliedRule = String.Format("Move {0}x and {1} to right side of equation.", ls.SymA, ls.SymC); string kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var ts0 = new TraceStep(null, internalEq, kc, step1metaRule, step1AppliedRule); ls._innerLoop.Add(ts0); string appliedRule = string.Format("divide coefficient b in both side of equation."); kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Inverse); var ts1 = new TraceStep(null, lastEq, kc, AlgebraRule.AlgebraicStrategy, appliedRule); ls._innerLoop.Add(ts1); ls.GenerateATrace(strategy); }
public void Test_Arith_2() { //1+2=4 var lhs = new Term(Expression.Add, new List<object>() { 1, 2 }); var equation = new Equation(lhs, 4); bool result = equation.ContainsVar(); Assert.False(result); Assert.True(equation.ToString().Equals("1+2=4")); object obj = equation.Eval(); Assert.NotNull(obj); var satisfied = obj as bool?; Assert.NotNull(satisfied); Assert.False(satisfied.Value); Assert.True(equation.CachedEntities.Count == 1); var cachedEq = equation.CachedEntities.ToList()[0] as Equation; Assert.NotNull(cachedEq); Assert.True(cachedEq.ToString().Equals("3=4")); Assert.True(cachedEq.Traces.Count == 1); }
public void Test_Algebra_OneVariable_Linear_Goal_Gen_1() { //x = 2+3 var x = new Var('x'); var term = new Term(Expression.Add, new List <object>() { 2, 3 }); var eq = new Equation(x, term); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var eqGoal = obj as EqGoal; Assert.NotNull(eqGoal); Assert.True(eqGoal.Traces.Count == 1); Assert.True(eqGoal.Lhs.Equals(x)); Assert.True(eqGoal.Rhs.Equals(5)); Assert.True(result); }
public void Test_TwoVariables_Linear_5() { //y=(1/3)x var y = new Var('y'); var x = new Var('x'); var term1 = new Term(Expression.Divide, new List <object>() { 1, 3 }); var rhs = new Term(Expression.Multiply, new List <object>() { term1, x }); var eq = new Equation(y, rhs); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); Assert.True(obj.ToString().Equals("-x+3y=0")); }
public void Equation_Var_Substitution_1() { //a = 1, a*b = -1; var a = new Var("a"); var b = new Var("b"); var eqGoal = new EqGoal(a, 1); var lhsTerm = new Term(Expression.Multiply, new List<object>() { a, b }); var equation = new Equation(lhsTerm, -1); bool result = equation.Reify(eqGoal); Assert.True(result); Assert.True(equation.CachedEntities.Count == 1); var cachedEq = equation.CachedEntities.ToList()[0] as Equation; object obj; result = cachedEq.IsEqGoal(out obj); Assert.True(result); var gGoal = obj as EqGoal; Assert.NotNull(gGoal); Assert.True(gGoal.Rhs.Equals(-1)); }
public Equation Generate(Equation currentEq, object source, object target, bool isLhs) { Equation cloneEq = currentEq.Clone(); if (isLhs) { if (!source.Equals(cloneEq.Lhs)) { //throw new Exception("Equation.Trace.cs 1: Must be equal."); } cloneEq.Lhs = target; } else { if (!source.Equals(cloneEq.Rhs)) { //throw new Exception("Equation.Trace.cs 2: Must be equal."); } cloneEq.Rhs = target; } return cloneEq; }
public void Goal_Gen_12() { //-1*v = 2 var v = new Var('v'); var term = new Term(Expression.Multiply, new List <object>() { -1, v }); var eq = new Equation(term, 2); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var gGoal = obj as EqGoal; Assert.NotNull(gGoal); Assert.True(gGoal.Rhs.Equals(-2)); Assert.True(gGoal.Traces.Count == 1); }
public void Equation_Var_Substitution_2() { //14: a = 2, b=a var a = new Var("a"); var b = new Var("b"); var eqGoal = new EqGoal(a, 2); var equation = new Equation(b, a); bool result = equation.Reify(eqGoal); Assert.True(result); Assert.True(equation.CachedEntities.Count == 1); var cachedEq = equation.CachedEntities.ToList()[0] as Equation; object obj; result = cachedEq.IsEqGoal(out obj); Assert.True(result); var gGoal = obj as EqGoal; Assert.NotNull(gGoal); Assert.True(gGoal.Lhs.Equals(b)); Assert.True(gGoal.Rhs.Equals(2)); }
public Equation Generate(Equation currentEq, object source, object target, bool isLhs) { Equation cloneEq = currentEq.Clone(); if (isLhs) { if (!source.Equals(cloneEq.Lhs)) { //throw new Exception("Equation.Trace.cs 1: Must be equal."); } cloneEq.Lhs = target; } else { if (!source.Equals(cloneEq.Rhs)) { //throw new Exception("Equation.Trace.cs 2: Must be equal."); } cloneEq.Rhs = target; } return(cloneEq); }
public void Test_Arith_4() { //1*2*3=7 var lhs = new Term(Expression.Multiply, new List <object>() { 1, 2, 3 }); var equation = new Equation(lhs, 7); bool result = equation.ContainsVar(); Assert.False(result); //Assert.True(equation.ToString().Equals("2*3=7")); object outputEq; bool? evalResult = equation.Eval(out outputEq); Assert.NotNull(evalResult); Assert.False(evalResult.Value); Assert.NotNull(outputEq); Assert.True(outputEq.ToString().Equals("6=7")); Assert.True(equation.Traces.Count == 1); }
public void Test_Arith_3() { //1+2+3=6 var lhs = new Term(Expression.Add, new List <object>() { 1, 2, 3 }); var equation = new Equation(lhs, 6); bool result = equation.ContainsVar(); Assert.False(result); Assert.True(equation.ToString().Equals("1+2+3=6")); object outputEq; bool? evalResult = equation.Eval(out outputEq); Assert.NotNull(evalResult); Assert.True(evalResult.Value); Assert.NotNull(outputEq); Assert.True(outputEq.ToString().Equals("6=6")); Assert.True(equation.Traces.Count == 1); }
public void Test_Algebra_OneVariable_Linear_1() { //1+2=x var x = new Var('x'); var lhs = new Term(Expression.Add, new List <object>() { 1, 2 }); var equation = new Equation(lhs, x); bool result = equation.ContainsVar(); Assert.True(result); Assert.True(equation.ToString().Equals("1+2=x")); equation.Eval(); Assert.True(equation.CachedEntities.Count == 1); var outputEq = equation.CachedEntities.ToList()[0] as Equation; Assert.NotNull(outputEq); Assert.True(outputEq.ToString().Equals("x=3")); Assert.True(outputEq.Traces.Count == 1); }
public void Test_TwoVariables_Linear_2() { //25: -2x+3y+9=0 var x = new Var('x'); var term1 = new Term(Expression.Multiply, new List <object>() { -2, x }); var y = new Var('y'); var term2 = new Term(Expression.Multiply, new List <object>() { 3, y }); var lhs = new Term(Expression.Add, new List <object>() { term1, term2, 9 }); var eq = new Equation(lhs, 0); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); }
public Equation FindCurrentEq(Equation rootEq) { Equation currentEq; if (rootEq._innerLoop.Count != 0) { currentEq = rootEq._innerLoop[rootEq._innerLoop.Count - 1].Target as Equation; if (currentEq == null) throw new Exception("Must be equation here"); } else { if (rootEq.Traces.Count == 0) { currentEq = rootEq; } else { var lastTrace = rootEq.Traces[rootEq.Traces.Count - 1].Item2 as List<TraceStep>; Debug.Assert(lastTrace != null); currentEq = lastTrace[lastTrace.Count - 1].Target as Equation; } } return currentEq; }
public void Test_TwoVariables_Linear_3() { //25: 3y=2x-9 var y = new Var('y'); var lhs = new Term(Expression.Multiply, new List <object>() { 3, y }); var x = new Var('x'); var term1 = new Term(Expression.Multiply, new List <object>() { 2, x }); var rhs = new Term(Expression.Add, new List <object>() { term1, -9 }); var eq = new Equation(lhs, rhs); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); Assert.True(obj.ToString().Equals("-2x+3y+9=0")); }
/// <summary> /// if x = y, then y = x /// </summary> /// <param name="currentEq"></param> /// <param name="rootEq"></param> /// <returns></returns> public static Equation ApplySymmetric(this Equation currentEq, Equation rootEq) { Equation localEq = currentEq; object lhs = currentEq.Lhs; object rhs = currentEq.Rhs; if (SatisfySymmetricCondition(lhs, rhs)) { var cloneEq = currentEq.Clone(); object tempObj = cloneEq.Lhs; cloneEq.Lhs = cloneEq.Rhs; cloneEq.Rhs = tempObj; string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Symmetric); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Symmetric, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Symmetric); var ts = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(ts); localEq = cloneEq; } return localEq; }
public void Test_TwoVariables_Linear_5() { //y=(1/3)x var y = new Var('y'); var x = new Var('x'); var term1 = new Term(Expression.Divide, new List<object>() { 1, 3 }); var rhs = new Term(Expression.Multiply, new List<object>() {term1, x}); var eq = new Equation(y, rhs); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); Assert.True(obj.ToString().Equals("-x+3y=0")); }
public void Test_TwoVariables_Linear_3() { //25: 3y=2x-9 var y = new Var('y'); var lhs = new Term(Expression.Multiply, new List<object>() {3, y}); var x = new Var('x'); var term1 = new Term(Expression.Multiply, new List<object>() {2, x}); var rhs = new Term(Expression.Add, new List<object>() {term1, -9}); var eq = new Equation(lhs, rhs); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); Assert.True(obj.ToString().Equals("-2x+3y+9=0")); }
public void Test_TwoVariables_Linear_2() { //25: -2x+3y+9=0 var x = new Var('x'); var term1 = new Term(Expression.Multiply, new List<object>() { -2, x }); var y = new Var('y'); var term2 = new Term(Expression.Multiply, new List<object>() { 3, y }); var lhs = new Term(Expression.Add, new List<object>() { term1, term2, 9 }); var eq = new Equation(lhs, 0); object obj; bool? result = eq.EvalEquation(out obj, true, true); Assert.Null(result); }
private Equation EvalTermInEquation(Equation rootEq, bool isLhs) { Equation localEq = this; object obj = isLhs ? Lhs : Rhs; var term = obj as Term; if (term == null) { return(localEq); } object evalResult = term.Eval(); if (evalResult.Equals(term)) { return(localEq); } var cloneEq = Clone(); if (isLhs) { cloneEq.Lhs = evalResult; } else { cloneEq.Rhs = evalResult; } Equation currentEq = FindCurrentEq(rootEq); #region Trace Transformation from term to Equation if (term.Traces.Count != 0) { localEq = currentEq; foreach (var trace in term.Traces) { var strategy = trace.Item1 as string; var lst = trace.Item2 as List <TraceStep>; foreach (var ts in lst) { var cloneEq2 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq2, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq2; } //GenerateATrace(strategy); } } if (term._innerLoop.Count != 0) { foreach (var ts in term._innerLoop) { var cloneEq1 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq1, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq1; } } #endregion return(cloneEq); }
private bool? EquationLaws(Equation rootEq, bool withEqRule, out object outputEq, bool lineCheck = false) { /* Equation currentEq; if (Traces.Count == 0) { currentEq = this; } else { var tuple = Traces[Traces.Count - 1]; var lst = tuple.Item2 as List<TraceStep>; Debug.Assert(lst != null); currentEq = lst[lst.Count - 1].Target as Equation; }*/ Equation currentEq = FindCurrentEq(rootEq); outputEq = currentEq; Debug.Assert(currentEq != null); bool hasChange; do { hasChange = false; Equation localEq00 = currentEq.EvalTermInEquation(rootEq, true); if (!localEq00.Equals(currentEq)) { hasChange = true; currentEq = localEq00; } Equation localEq0 = currentEq.EvalTermInEquation(rootEq, false); if (!localEq0.Equals(currentEq)) { hasChange = true; currentEq = localEq0; } bool? satisfiable = Satisfy(currentEq); if (satisfiable != null) { if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = currentEq; 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; var localEqLst = localObj1 as List<Equation>; if (localEqLst != null) { if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = localEqLst; return null; } if (localEq1 != null && !localEq1.Equals(currentEq)) { hasChange = true; currentEq = localEq1; } var localEq2 = localEq1.ApplySymmetric(rootEq); if (!localEq2.Equals(localEq1)) { hasChange = true; currentEq = localEq2; } } while (hasChange); if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = currentEq; return null; }
public void Goal_Gen_3() { //x=(-1)*0 var x = new Var('x'); var term = new Term(Expression.Multiply, new List<object>() { -1, 0 }); var eq = new Equation(x, term); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var goal = obj as EqGoal; Assert.NotNull(goal); Assert.True(goal.Rhs.Equals(0)); }
/// <summary> /// if x = y and y = z, then x = z /// if x = y, then x + a = y + a /// if x^2 = y^2, then x = y /// if x = y, then ax = ay /// ax = ay -> x=y /// </summary> /// <param name="goal"></param> /// <param name="gGoal"></param> /// <returns></returns> public static object ApplyTransitive(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); } //Power Inverse if (SatisfyTransitiveCondition2(lhs, rhs)) { #region Condition2 var cloneEq = currentEq.Clone(); var cloneEq2 = currentEq.Clone(); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var cloneLst = lhsTerm.Args as List <object>; Debug.Assert(cloneLst != null); cloneEq.Lhs = cloneLst[0]; cloneEq.Rhs = new Term(Expression.Power, new List <object>() { cloneEq.Rhs, 0.5 }); var lhsTerm2 = cloneEq2.Lhs as Term; Debug.Assert(lhsTerm2 != null); var cloneLst2 = lhsTerm2.Args as List <object>; Debug.Assert(cloneLst2 != null); cloneEq2.Lhs = cloneLst2[0]; var internal1 = new Term(Expression.Power, new List <object>() { cloneEq2.Rhs, 0.5 }); cloneEq2.Rhs = new Term(Expression.Multiply, new List <object>() { -1, internal1 }); string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var ts = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(ts); //localEq = cloneEq; var lst = new List <Equation>(); lst.Add(cloneEq); lst.Add(cloneEq2); return(lst); #endregion } if (!lineCheck) { //Add Inverse if (SatifyTransitiveCondition0(lhs, rhs)) { #region condition0 var cloneEq = currentEq.Clone(); var rhsTerm = new Term(Expression.Add, new List <object>() { cloneEq.Rhs }); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List <object>; Debug.Assert(lst != null); for (int i = 0; i < lst.Count; i++) { var temp = lst[i]; bool isNumber = LogicSharp.IsNumeric(temp); if (isNumber) { 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); rhsArgLst.Add(inverseRhs); break; } var term = temp as Term; if (term != null && !term.ContainsVar()) { 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); rhsArgLst.Add(inverseRhs); break; } } cloneEq.Rhs = rhsTerm; if (lst.Count == 1) { cloneEq.Lhs = lst[0]; } string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; return(localEq); #endregion } } else { if (SatisfyTransitiveCondition1(lhs, rhs)) { #region Condition1 var cloneEq = currentEq.Clone(); var inverseRhs = new Term(Expression.Multiply, new List <object>() { -1, rhs }); var lhsTerm = cloneEq.Lhs as Term; if (lhsTerm != null) { var cloneLst = lhsTerm.Args as List <object>; Debug.Assert(cloneLst != null); if (lhsTerm.Op.Method.Name.Equals("Add")) { cloneLst.Add(inverseRhs); } else { cloneEq.Lhs = new Term(Expression.Add, new List <object>() { lhs, inverseRhs }); } } else { cloneEq.Lhs = new Term(Expression.Add, new List <object>() { lhs, inverseRhs }); } cloneEq.Rhs = 0; string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; #endregion } } //Mutliply Inverse if (SatisfyTransitiveCondition3(lhs, rhs)) { #region condition3 var cloneEq = currentEq.Clone(); var rhsTerm = new Term(Expression.Multiply, new List <object>() { cloneEq.Rhs }); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List <object>; Debug.Assert(lst != null); for (int i = 0; i < lst.Count; i++) { var temp = lst[i]; bool isNumber = LogicSharp.IsNumeric(temp); if (isNumber) { var inverseRhs = new Term(Expression.Divide, new List <object>() { 1, temp }); lst.Remove(temp); var rhsArgLst = rhsTerm.Args as List <object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } var term = temp as Term; if (term != null && !term.ContainsVar()) { var inverseRhs = new Term(Expression.Divide, new List <object>() { 1, temp }); lst.Remove(i); var rhsArgLst = rhsTerm.Args as List <object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } } cloneEq.Rhs = rhsTerm; if (lst.Count == 1) { cloneEq.Lhs = lst[0]; } string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; return(localEq); #endregion } //Divide Inverse if (SatisfyTransitiveCondition4(lhs, rhs)) { #region condition4 var cloneEq = currentEq.Clone(); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List <object>; Debug.Assert(lst != null); Debug.Assert(lst.Count == 2); bool numerator = LogicSharp.IsNumeric(lst[0]); bool deNumerator = LogicSharp.IsNumeric(lst[1]); if (deNumerator) { var rhsTerm = new Term(Expression.Multiply, new List <object>() { lst[1], cloneEq.Rhs }); var newEq = new Equation(lst[0], rhsTerm); 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); } if (numerator) { var rhsTerm = new Term(Expression.Divide, new List <object>() { lst[0], cloneEq.Rhs }); var newEq = new Equation(lst[1], rhsTerm); 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); } #endregion } return(localEq); }
public void Goal_Gen_6() { // d^2 = (1.0-4.0)^2+(2.0-6.0)^2 // d>0 object pt1XCoord = 1.0; object pt1YCoord = 2.0; object pt2XCoord = 4.0; object pt2YCoord = 6.0; var variable = new Var('d'); var lhs = new Term(Expression.Power, new List<object>() { variable, 2.0 }); var term1 = new Term(Expression.Subtract, new List<object>() { pt1XCoord, pt2XCoord }); var term11 = new Term(Expression.Power, new List<object>() { term1, 2.0 }); var term2 = new Term(Expression.Subtract, new List<object>() { pt1YCoord, pt2YCoord }); var term22 = new Term(Expression.Power, new List<object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); //var obj = rhs.Eval(); //Assert.True(obj.Equals(25)); var eq = new Equation(lhs, rhs); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var lst = obj as List<object>; Assert.NotNull(lst); Assert.True(lst.Count == 2); //Trace Verify var gGoal = lst[0] as EqGoal; Assert.NotNull(gGoal); Assert.True(gGoal.Traces.Count == 1); var steps = gGoal.Traces[0].Item2 as List<TraceStep>; Assert.NotNull(steps); Assert.True(steps.Count == 7); var lastStep = steps[6]; Assert.NotNull(lastStep); }
public Query(Equation eq) : base(eq) { }
private bool?EquationLaws(Equation rootEq, bool withEqRule, out object outputEq, bool lineCheck = false) { /* Equation currentEq; * if (Traces.Count == 0) * { * currentEq = this; * } * else * { * var tuple = Traces[Traces.Count - 1]; * var lst = tuple.Item2 as List<TraceStep>; * Debug.Assert(lst != null); * currentEq = lst[lst.Count - 1].Target as Equation; * }*/ Equation currentEq = FindCurrentEq(rootEq); outputEq = currentEq; Debug.Assert(currentEq != null); bool hasChange; do { hasChange = false; Equation localEq00 = currentEq.EvalTermInEquation(rootEq, true); if (!localEq00.Equals(currentEq)) { hasChange = true; currentEq = localEq00; } Equation localEq0 = currentEq.EvalTermInEquation(rootEq, false); if (!localEq0.Equals(currentEq)) { hasChange = true; currentEq = localEq0; } bool?satisfiable = Satisfy(currentEq); if (satisfiable != null) { if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = currentEq; 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; var localEqLst = localObj1 as List <Equation>; if (localEqLst != null) { if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = localEqLst; return(null); } if (localEq1 != null && !localEq1.Equals(currentEq)) { hasChange = true; currentEq = localEq1; } var localEq2 = localEq1.ApplySymmetric(rootEq); if (!localEq2.Equals(localEq1)) { hasChange = true; currentEq = localEq2; } } while (hasChange); if (rootEq._innerLoop.Count != 0) { rootEq.GenerateATrace(EquationsRule.EqStrategy); } outputEq = currentEq; return(null); }
public void Test_Algebra_TwoVariables_Quadratic_1() { //a^2+b^2=25 object pt1XCoord = new Var("a"); object pt2XCoord = new Var('b'); //var term1 = new Term(Expression.Add, new List<object>() { pt1XCoord, pt2XCoord }); var term11 = new Term(Expression.Power, new List<object>() { pt1XCoord, 2.0 }); //var term2 = new Term(Expression.Add, new List<object>() { pt1YCoord, pt2YCoord }); var term22 = new Term(Expression.Power, new List<object>() { pt2XCoord, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); var eq = new Equation(rhs, 25); eq.Eval(); Assert.True(eq.CachedEntities.Count == 1); var outputEq = eq.CachedEntities.ToList()[0]; Assert.True(outputEq.Equals(eq)); }
public void Goal_Gen_7() { //a^2 = 25 // 25 = (a-3.0)^2+(6.0-3.0)^2 object pt1XCoord = new Var("a"); object pt1YCoord = 6.0; object pt2XCoord = -3.0; object pt2YCoord = -3.0; var term1 = new Term(Expression.Add, new List<object>() { pt1XCoord, pt2XCoord }); var term11 = new Term(Expression.Power, new List<object>() { term1, 2.0 }); var term2 = new Term(Expression.Add, new List<object>() { pt1YCoord, pt2YCoord }); var term22 = new Term(Expression.Power, new List<object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); var eq = new Equation(rhs, 25); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var lst = obj as List<object>; Assert.NotNull(lst); Assert.True(lst.Count == 2); var gGoal1 = lst[0] as EqGoal; Assert.NotNull(gGoal1); Assert.True(gGoal1.Traces.Count == 1); }
public void Goal_Gen_6() { // d^2 = (1.0-4.0)^2+(2.0-6.0)^2 // d>0 object pt1XCoord = 1.0; object pt1YCoord = 2.0; object pt2XCoord = 4.0; object pt2YCoord = 6.0; var variable = new Var('d'); var lhs = new Term(Expression.Power, new List <object>() { variable, 2.0 }); var term1 = new Term(Expression.Subtract, new List <object>() { pt1XCoord, pt2XCoord }); var term11 = new Term(Expression.Power, new List <object>() { term1, 2.0 }); var term2 = new Term(Expression.Subtract, new List <object>() { pt1YCoord, pt2YCoord }); var term22 = new Term(Expression.Power, new List <object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List <object>() { term11, term22 }); //var obj = rhs.Eval(); //Assert.True(obj.Equals(25)); var eq = new Equation(lhs, rhs); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var lst = obj as List <object>; Assert.NotNull(lst); Assert.True(lst.Count == 2); //Trace Verify var gGoal = lst[0] as EqGoal; Assert.NotNull(gGoal); Assert.True(gGoal.Traces.Count == 1); var steps = gGoal.Traces[0].Item2 as List <TraceStep>; Assert.NotNull(steps); Assert.True(steps.Count == 7); var lastStep = steps[6]; Assert.NotNull(lastStep); }
public void Goal_Gen_8() { double value = 5.0d; var v = new Var("v"); var term1_1 = new Term(Expression.Multiply, new List<object>() { -1, 5.0 }); var term1 = new Term(Expression.Add, new List<object>() { 2.0, term1_1 }); var term11 = new Term(Expression.Power, new List<object>() { term1, 2.0 }); var term2_2 = new Term(Expression.Multiply, new List<object>() { -1, v }); var term2 = new Term(Expression.Add, new List<object>() { 4, term2_2 }); var term22 = new Term(Expression.Power, new List<object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); var lhs = new Term(Expression.Power, new List<object>() { value, 2.0 }); var eq = new Equation(lhs, rhs); object obj; bool result = eq.IsEqGoal(out obj); Assert.True(result); var lst = obj as List<object>; Assert.NotNull(lst); Assert.True(lst.Count == 2); }
private Equation EvalTermInEquation(Equation rootEq, bool isLhs) { Equation localEq = this; object obj = isLhs ? Lhs : Rhs; var term = obj as Term; if (term == null) return localEq; object evalResult = term.Eval(); if (evalResult.Equals(term)) return localEq; var cloneEq = Clone(); if (isLhs) { cloneEq.Lhs = evalResult; } else { cloneEq.Rhs = evalResult; } Equation currentEq = FindCurrentEq(rootEq); #region Trace Transformation from term to Equation if (term.Traces.Count != 0) { localEq = currentEq; foreach (var trace in term.Traces) { var strategy = trace.Item1 as string; var lst = trace.Item2 as List<TraceStep>; foreach (var ts in lst) { var cloneEq2 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq2, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq2; } //GenerateATrace(strategy); } } if (term._innerLoop.Count != 0) { foreach (var ts in term._innerLoop) { var cloneEq1 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq1, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq1; } } #endregion return cloneEq; }
public void Goal_Gen_9() { double value = 5.0d; var v = new Var("v"); var term1_1 = new Term(Expression.Multiply, new List<object>() { -1, 5.0 }); var term1 = new Term(Expression.Subtract, new List<object>() { 2.0, 5.0 }); var term11 = new Term(Expression.Power, new List<object>() { term1, 2.0 }); var term2_2 = new Term(Expression.Multiply, new List<object>() { -1, v }); var term2 = new Term(Expression.Subtract, new List<object>() { 4, v }); var term22 = new Term(Expression.Power, new List<object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); var lhs = new Term(Expression.Power, new List<object>() { value, 2.0 }); var eq = new Equation(lhs, rhs); object obj; bool result = eq.IsEqGoal(out obj); Assert.False(result); //do not use substract when dealing with variable. use add instead. }
/// <summary> /// The Reflexive Property states that for every real number x, x = x. /// </summary> /// <param name="equation"></param> /// <returns>True: Satisfy, False: Un-satisfy, null: </returns> private bool? Satisfy(Equation equation) { bool lhsNumeric = LogicSharp.IsNumeric(equation.Lhs); bool rhsNumeric = LogicSharp.IsNumeric(equation.Rhs); if (lhsNumeric && rhsNumeric) { return equation.Lhs.Equals(equation.Rhs); } var leftVar = equation.Lhs as Var; var rightVar = equation.Rhs as Var; if (leftVar != null && rightVar != null) { bool result = leftVar.Equals(rightVar); if (result) return true; return null; } /* var leftTerm = equation.Lhs as Term; var rightTerm = equation.Rhs as Term; if (leftTerm != null && rightTerm != null) { return leftTerm.Equals(rightTerm); }*/ return null; }
public void Test_TwoVariables_Linear_1() { //2x+3y-1=0 var x = new Var("x"); var term1 = new Term(Expression.Multiply, new List<object>() { 2, x }); var y = new Var("y"); var term2 = new Term(Expression.Multiply, new List<object>() { 3, y }); var term = new Term(Expression.Add, new List<object>() { term1, term2, -1 }); var equation = new Equation(term, 0); object obj; bool? result = equation.Eval(out obj, false); Assert.Null(result); var outputEq = obj as Equation; Assert.NotNull(outputEq); Assert.True(outputEq.Equals(equation)); result = equation.Eval(out obj, true); Assert.Null(result); outputEq = obj as Equation; Assert.NotNull(outputEq); Assert.False(outputEq.Equals(equation)); Assert.True(outputEq.ToString().Equals("2x+3y=1")); }
/// <summary> /// Inverse Properties /// 1. For any number a, there exists a number x such that a+x=0. /// 2. If a is any number except 0, there exists a number x such that ax = 1. /// </summary> /// <param name="currentEq"></param> /// <param name="rootEq"></param> /// <returns></returns> public static bool ApplyInverse(this Equation currentEq, Equation rootEq) { return(false); }
protected Shape(Equation equation) : base(equation) { Coordinate = CoordinateSystemType.Cartesian; Repr = RepresentationType.Explicit; }