/* * given m=2, k=3, y=3x+2 */ public static void FromSlopeInterceptToLineSlopeIntercept(EqGoal slopeGoal, EqGoal interceptGoal, LineSymbol ls) { //1. Substitute slope and intercept properties into the line slope-intercept form y=mx+b. //////////////////////////////////////////////////////// var ts0 = new TraceStep(null, slopeGoal, GeometryScaffold.KC_LineSlopeForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(slopeGoal)); var ts1 = new TraceStep(null, interceptGoal, GeometryScaffold.KC_LineInterceptForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(interceptGoal)); ls._innerLoop.Add(ts0); var abstractLs = new Line(ls.Shape.Label, slopeGoal.Lhs, interceptGoal.Lhs); var abstractLss = new LineSymbol(abstractLs); var internalLs = new Line(ls.Shape.Label, ls.SymSlope, interceptGoal.Lhs); var internalLss = new LineSymbol(internalLs); var traceStep1 = new TraceStep(abstractLss, internalLss, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(abstractLss, slopeGoal)); ls._innerLoop.Add(traceStep1); ls._innerLoop.Add(ts1); /* * var rule = "Substitute given property to line slope-intercept form."; * var appliedRule1 = string.Format("Substitute slope={0} into y=mx+b", ls.SymSlope); * var appliedRule2= string.Format("Substitute intercept={0} into y=mx+b", ls.SymIntercept);*/ var traceStep2 = new TraceStep(internalLss, ls, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(internalLss, interceptGoal)); ls._innerLoop.Add(traceStep2); string strategy = "Substitute slope and intercept properties into the line slope-intercept form y = mx + b."; ls.GenerateATrace(strategy); }
public static void FromPointPointToLine(PointSymbol ps1, PointSymbol ps2, LineSymbol ls) { var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List <object>() { m, x }); var term1 = new Term(Expression.Add, new List <object>() { term, k }); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List <object>() { m, ps1.SymXCoordinate }); var term22 = new Term(Expression.Add, new List <object>() { term2, k }); var eqPattern1 = new Equation(ps1.SymYCoordinate, term22); var term3 = new Term(Expression.Multiply, new List <object>() { m, ps2.SymXCoordinate }); var term33 = new Term(Expression.Add, new List <object>() { term3, k }); var eqPattern2 = new Equation(ps2.SymYCoordinate, term33); string strategy = "Generate a line by substituting two given points into the line slope-intercept form y=mx+k."; var ts0 = new TraceStep(eqPattern, eqPattern1, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps1)); var ts1 = new TraceStep(eqPattern, eqPattern2, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps2)); string kc = GeometryScaffold.KC_LineSlopeForm; var ts2 = new TraceStep(null, ls, kc, "calculate m and k through the above two linear equations.", "calculate m and k through linear equation and retrieve y=mx+k line form."); ls._innerLoop.Add(ts0); ls._innerLoop.Add(ts1); ls._innerLoop.Add(ts2); ls.GenerateATrace(strategy); }
public static void FromPointSlopeToLine(PointSymbol ps, EqGoal goal, LineSymbol ls) { string strategy = "Substitute point and slope property into line form."; // 1. Substitute slope property into the line slope-intercept form. // 2. Calculate the line intercept by substituting the point into line pattern. ////////////////////////////////////////////////////////////////////// string strategy1 = "Substitute slope property into the line slope-intercept form."; var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List <object>() { m, x }); var term1 = new Term(Expression.Add, new List <object>() { term, k }); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List <object>() { goal.Rhs, x }); var term3 = new Term(Expression.Add, new List <object>() { term2, k }); var eqInternal1 = new Equation(y, term3); var appliedRule1 = SubstitutionRule.ApplySubstitute(eqPattern, goal); var ts0 = new TraceStep(eqPattern, eqInternal1, SubstitutionRule.SubstituteKC(), strategy1, appliedRule1); ls._innerLoop.Add(ts0); ////////////////////////////////////////////////////////////////////// var point = ps.Shape as Point; Debug.Assert(point != null); string strategy2 = "Calculate the line intercept by substituting the point into line pattern."; var term4 = new Term(Expression.Multiply, new List <object>() { goal.Rhs, point.XCoordinate }); var term5 = new Term(Expression.Add, new List <object>() { term4, k }); var eqinternal2 = new Equation(point.YCoordinate, term5); object obj; bool result = eqinternal2.IsEqGoal(out obj); var eqGoal = obj as EqGoal; Debug.Assert(eqGoal != null); var gTuple = eqGoal.Traces[0]; var gLst = gTuple.Item2 as List <TraceStep>; Debug.Assert(gLst != null); ls._innerLoop.AddRange(gLst); var appliedRule2 = SubstitutionRule.ApplySubstitute(eqInternal1, ps); var ts = new TraceStep(eqInternal1, ls, SubstitutionRule.SubstituteKC(), strategy2, appliedRule2); ls._innerLoop.Add(ts); ls.GenerateATrace(strategy); }
/* * Automatic scaffolding * * Distance function (x-x0)^2+(y-y0)^2 = d^2 * * Forward chaining to derive d. * Backward chaining to derive other four parameters. */ private static Tuple <object, object> SlopeSubstitution(Point pt1, Point pt2, double value) { //out strategy string strategy = "Substitute two points and slope value into the slope function."; //1. Substitute two points into the line slope function. //2. Substitute slope property into the line slope function. var lst = new List <TraceStep>(); /////////////////////////////////////////////////////////////////// var variable = new Var('m'); //1. string step1metaRule = "Consider the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step1AppliedRule = String.Format( "Substitute two points value into the slope function {0}=({1}-{2})/({3}-{4})", variable, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); /* var pt1X = new Var("x0"); * var pt1Y = new Var("y0"); * var pt2X = new Var("x1"); * var pt2Y = new Var("y1");*/ /* var term1_1 = new Term(Expression.Subtract, new List<object>() { pt1X, pt2X }); * var term2_1 = new Term(Expression.Subtract, new List<object>() { pt1Y, pt2Y }); * var rhs_1 = new Term(Expression.Divide, new List<object>() { term2_1, term1_1 }); * * var old_eq = new Equation(variable, rhs_1);*/ var term1_11 = new Term(Expression.Subtract, new List <object>() { pt2.XCoordinate, pt1.XCoordinate }); var term2_11 = new Term(Expression.Subtract, new List <object>() { pt2.YCoordinate, pt1.YCoordinate }); var rhs_11 = new Term(Expression.Divide, new List <object>() { term1_11, term2_11 }); var eq = new Equation(variable, rhs_11); string kc = GeometryScaffold.KC_LineSlopeForm; var trace1 = new TraceStep(null, eq, kc, step1metaRule, step1AppliedRule); //lst.Add(trace1); lst.Add(trace1); /////////////////////////////////////////////////////////////////////////// //2. var newEq = new Equation(value, rhs_11); string step2metaRule = "Substitute slope property into the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step2AppliedRule = String.Format( "Substitute slope value into the slope function {0}=({1}-{2})/({3}-{4})", value, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); kc = SubstitutionRule.SubstituteKC(); var trace2 = new TraceStep(null, newEq, kc, step2metaRule, step2AppliedRule); //lst.Add(trace1); lst.Add(trace2); //////////////////////////////////////////////////////////////////////////// var newTuple = new Tuple <object, object>(strategy, lst); return(newTuple); }
public void GenerateYCacheSymbol(object obj, EqGoal goal) { var point = Shape as Point; Debug.Assert(point != null); CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); if (CachedSymbols.Count == 0) { var gPoint = new Point(point.Label, point.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); CachedSymbols.Add(gPointSymbol); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } //Substitution trace var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } else { foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var pt = ss.Shape as Point; if (pt != null) { var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict()); if (!pt.YCoordinate.Equals(yResult)) { var gPt = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate); var gPointSymbol = new PointSymbol(gPt); //substitute pt.YCoordinate = yResult; ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { pt.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //pt.Traces.Insert(0, ts); } else { //generate var gPoint = new Point(pt.Label, pt.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.Traces.AddRange(goal.Traces); gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(PointAcronym.X)) { gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.X, pair.Value)); } } CachedSymbols.Add(gPointSymbol); //substitute //Add traces from pt to gPoint for (int i = pt.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, pt.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } } } } }
public void CacheC(object obj, EqGoal goal) { CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); if (CachedSymbols.Count == 0) { var line = Shape as Line; Debug.Assert(line != null); #region generate new object var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); CachedSymbols.Add(gLineSymbol); //Transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } //Substitution trace string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(this, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); #endregion } else { #region Iterate existing point object foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var line = ss.Shape as Line; if (line != null) { var cResult = LogicSharp.Reify(line.C, goal.ToDict()); if (!line.C.Equals(cResult)) { var gline = new Line(line.Label, line.A, line.B, line.C); var gLineSymbol = new LineSymbol(gline); //substitute line.C = cResult; ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { line.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(gLineSymbol, ss, kc, rule, appliedRule); //line.Traces.Insert(0, ts); line._innerLoop.Add(ts); line.GenerateATrace(SubstitutionRule.SubstitutionStrategy); } else { //generate var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(LineAcronym.A)) { gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.A, pair.Value)); } else if (pair.Key.Equals(LineAcronym.B)) { gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.B, pair.Value)); } } CachedSymbols.Add(gLineSymbol); //substitute //Add traces from line to gLine for (int i = line.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, line.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(ss, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); } } } #endregion } }