public void UseCase_SimpleSystemAsFunction() { // TODO: Replace with new (easier to use) MathFunction class instead of MathSystem _p.KeepTrack = true; MathSystem s = _p.CurrentSystem; s.RemoveUnusedObjects(); Signal x = Binder.CreateSignal(); Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); Signal sinx2 = StdBuilder.Sine(x2); Assert.AreEqual(0, s.InputCount, "Input Signal Count A"); Assert.AreEqual(0, s.OutputCount, "Output Signal Count A"); Assert.AreEqual(0, s.BusCount, "Bus Count A"); Assert.AreEqual(3, s.SignalCount, "Signal Count A"); Assert.AreEqual(2, s.PortCount, "Port Count A"); s.PromoteAsInput(x); s.PromoteAsOutput(sinx2); s.RemoveUnusedObjects(); Assert.AreEqual(1, s.InputCount, "Input Signal Count B"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count B"); Assert.AreEqual(0, s.BusCount, "Bus Count B"); Assert.AreEqual(3, s.SignalCount, "Signal Count B"); Assert.AreEqual(2, s.PortCount, "Port Count B"); double ret = Math.Round(s.Evaluate(Math.PI)[0], 4); Assert.AreEqual(-0.4303, ret, "Result"); }
public void SystemToExpressionTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2)*2 Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo; s1.AddSignalTree(sinx2t2, true, true); // EVALUATE SYSTEM 1 FOR x=1.5 x.PostNewValue(new RealValue(1.5)); p.SimulateInstant(); Assert.AreEqual(0, s1.BusCount, "A0"); Assert.AreEqual(5, s1.SignalCount, "A1"); Assert.AreEqual(3, s1.PortCount, "A2"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "A3"); // SERIALIZE SYSTEM 1 TO EXPRESSION ExpressionWriter writer = new ExpressionWriter(); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); string expr = writer.WrittenExpressions.Dequeue(); Console.WriteLine(expr); // .... }
public void UseCase_SystemAsCompoundArchitecture() { _p.KeepTrack = false; MathSystem s = _p.CurrentSystem; s.RemoveUnusedObjects(); Signal x = Binder.CreateSignal(); Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); Signal sinx2 = StdBuilder.Sine(x2); s.AddSignalTree(sinx2, true, true); s.RemoveUnusedObjects(); s.PublishToLibrary("SineOfSquaredX", "sinx2"); Signal y = Binder.CreateSignal(); y.PostNewValue(RealValue.E); Signal z = Service <IBuilder> .Instance.Function("sinx2", y); _p.SimulateInstant(); //.SimulateFor(new TimeSpan(1)); IValueStructure res = z.Value; RealValue resReal = RealValue.ConvertFrom(res); Assert.AreEqual(0.8939, Math.Round(resReal.Value, 4)); }
public void Pattern_TreePattern() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Pattern psimp = new Pattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Assert.AreEqual(true, psimp.Match(sinx2, sinx2.DrivenByPort), "B01"); Assert.AreEqual(false, psimp.Match(x2, x2.DrivenByPort), "B02"); TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinsqr.Add(new Pattern(new EntityCondition(new MathIdentifier("Square", "Std")))); Assert.AreEqual(true, psinsqr.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinsqr.Match(x2, x2.DrivenByPort), "B04"); TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinadd.Add(new Pattern(new EntityCondition(new MathIdentifier("Add", "Std")))); Assert.AreEqual(false, psinadd.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinadd.Match(x2, x2.DrivenByPort), "B04"); }
public static void RegisterTheorems(ILibrary library) { Analysis.DerivativeTransformation.Provider.Add( new Analysis.DerivativeTransformation(_entityId, delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs) { int cnt = manipulatedInputs.Count - 1; Signal[] multiplySignals = new Signal[cnt]; Signal[] addSignals = new Signal[cnt]; for (int i = 0; i < cnt; i++) { multiplySignals[i] = port.InputSignals[i + 1]; } Signal left = Std.Divide(manipulatedInputs[0], multiplySignals); for (int i = 0; i < cnt; i++) { for (int j = 0; j < cnt; j++) { multiplySignals[j] = i == j ? StdBuilder.Square(port.InputSignals[j + 1]) : port.InputSignals[j + 1]; } Signal num = port.InputSignals[0] * manipulatedInputs[i + 1]; addSignals[i] = Std.Divide(num, multiplySignals); } return(new SignalSet(Std.Subtract(left, addSignals))); })); Algebra.AutoSimplifyTransformation.Provider.Add( new Algebra.AutoSimplifyTransformation(_entityId, delegate(Port port) { // TODO return(ManipulationPlan.DoAlter); }, delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs) { if (SimplifyFactorsForceMultiplication(manipulatedInputs) || hasManipulatedInputs) { if (manipulatedInputs.Count == 0) { return(new SignalSet(IntegerValue.ConstantMultiplicativeIdentity)); } if (manipulatedInputs.Count == 1) { return(manipulatedInputs); } return(new SignalSet(StdBuilder.Divide(manipulatedInputs))); } else { return(port.OutputSignals); } })); }
private void btnBuildSample_Click(object sender, EventArgs e) { Signal x = Binder.CreateSignal(); x.Label = "x"; x.AddConstraint(RealSetProperty.Instance); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo; _ctrl.CurrentSystem.AddSignalTree(sinx2t2, true, true); }
public static void RegisterTheorems(ILibrary library) { Analysis.DerivativeTransformation.Provider.Add( new Analysis.DerivativeTransformation(_entityId, delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs) { SignalSet outputs = new SignalSet(); for (int i = 0; i < manipulatedInputs.Count; i++) { outputs.Add(-(manipulatedInputs[i] / StdBuilder.Square(port.InputSignals[i]))); } return(outputs); })); }
public void SystemToSystemCloneTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo; s1.AddSignalTree(sinx2t2, true, true); // EVALUATE SYSTEM 1 FOR x=1.5 x.PostNewValue(new RealValue(1.5)); p.SimulateInstant(); Assert.AreEqual(0, s1.BusCount, "A0"); Assert.AreEqual(5, s1.SignalCount, "A1"); Assert.AreEqual(3, s1.PortCount, "A2"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "A3"); // CLONE SYSTEM 1 TO SYSTEM 2 /* * HINT: would be simpler to just call: * MathSystem s2 = s1.Clone(); */ SystemWriter writer = new SystemWriter(); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); IMathSystem s2 = writer.WrittenSystems.Dequeue(); Assert.AreEqual(0, s2.BusCount, "B0"); Assert.AreEqual(5, s2.SignalCount, "B1"); Assert.AreEqual(3, s2.PortCount, "B2"); Assert.AreEqual("Std.Real(1.55614639377584)", s2.GetOutput(0).Value.ToString(), "B3"); // EVALUATE SYSTEM 2 FOR x=2.5 s2.GetInput(0).PostNewValue(new RealValue(2.5)); p.SimulateInstant(); Assert.AreEqual("Std.Real(-0.0663584330951136)", s2.GetOutput(0).Value.ToString(), "C0"); // CHECK SYSTEM 1 STILL ON x=1.5 Assert.AreEqual("Std.Real(1.5)", x.Value.ToString(), "D0"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "D1"); }
public void UseCase_ComplexDerivedSystemAsFunction() { // TODO: Replace with new (easier to use) MathFunction class instead of MathSystem _p.KeepTrack = false; MathSystem s = _p.CurrentSystem; Signal x = Binder.CreateSignal(); x.Label = "x"; Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal secx2 = StdBuilder.Secant(x2); secx2.Label = "secx2"; Signal diff = Std.Derive(secx2, x); diff.Label = "diff1"; s.AddSignalTree(diff, true, true); s.RemoveUnusedObjects(); Assert.AreEqual("2*sec(sqr(x))*tan(sqr(x))*x", _f.Format(diff, FormattingOptions.Compact), "Formatted Diff A"); Assert.AreEqual(1, s.InputCount, "Input Signal Count A"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count A"); Assert.AreEqual(0, s.BusCount, "Bus Count A"); Assert.AreEqual(6, s.SignalCount, "Signal Count A"); Assert.AreEqual(4, s.PortCount, "Port Count A"); Signal diff2 = Std.AutoSimplify(diff); s.UnpromoteAsOutput(diff); s.AddSignalTree(diff2, true, true); s.RemoveUnusedObjects(); Assert.AreEqual("2*sec(sqr(x))*tan(sqr(x))*x", _f.Format(diff2, FormattingOptions.Compact), "Formatted Diff B"); Assert.AreEqual(1, s.InputCount, "Input Signal Count B"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count B"); Assert.AreEqual(0, s.BusCount, "Bus Count B"); Assert.AreEqual(6, s.SignalCount, "Signal Count B"); Assert.AreEqual(4, s.PortCount, "Port Count B"); IValueStructure vs = s.Evaluate(ComplexValue.I)[0]; Assert.IsInstanceOfType(typeof(ComplexValue), vs, "Result is complex."); ComplexValue cv = (ComplexValue)vs; Assert.AreEqual(0, Math.Round(cv.RealValue, 4), "Real Result"); Assert.AreEqual(-5.7649, Math.Round(cv.ImaginaryValue, 4), "Imag Result"); }
public static void RegisterTheorems(ILibrary library) { Analysis.DerivativeTransformation.Provider.Add( new Analysis.DerivativeTransformation(_entityId, delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs) { ReadOnlySignalSet squares = StdBuilder.Square(port.OutputSignals); Signal one = IntegerValue.ConstantOne; SignalSet outputs = new SignalSet(); for (int i = 0; i < manipulatedInputs.Count; i++) { outputs.Add((one + squares[i]) * manipulatedInputs[i]); } return(outputs); })); MathIdentifier typeId = new MathIdentifier("TrigonometricSubstitute", "Std"); ITheoremProvider basicProvider; if (!library.TryLookupTheoremType(typeId, out basicProvider)) { basicProvider = Binder.GetInstance <ITransformationTheoremProvider, MathIdentifier>(typeId); library.AddTheoremType(basicProvider); } ((ITransformationTheoremProvider)basicProvider).Add( new BasicTransformation(_entityId.DerivePostfix("TrigonometricSubstitute"), typeId, delegate() { return(new Pattern(new EntityCondition(_entityId))); }, delegate(Port port) { return(ManipulationPlan.DoAlter); }, delegate(Port port, SignalSet transformedInputs, bool hasTransformedInputs) { Signal[] ret = new Signal[transformedInputs.Count]; for (int i = 0; i < ret.Length; i++) { ret[i] = StdBuilder.Sine(transformedInputs[i]) / StdBuilder.Cosine(transformedInputs[i]); } return(ret); })); }
public void SystemToXmlSerializeTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2)*2 Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo; s1.AddSignalTree(sinx2t2, true, true); // EVALUATE SYSTEM 1 FOR x=1.5 x.PostNewValue(new RealValue(1.5)); p.SimulateInstant(); Assert.AreEqual(0, s1.BusCount, "A0"); Assert.AreEqual(5, s1.SignalCount, "A1"); Assert.AreEqual(3, s1.PortCount, "A2"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "A3"); // SERIALIZE SYSTEM 1 TO XML /* * HINT: would be simpler to just call: * string s2xml = s1.WriteXml(false); */ StringBuilder sb = new StringBuilder(); { XmlWriterSettings settings = new XmlWriterSettings(); settings.ConformanceLevel = ConformanceLevel.Document; settings.OmitXmlDeclaration = false; settings.Indent = true; settings.NewLineHandling = NewLineHandling.Entitize; settings.Encoding = Config.InternalEncoding; XmlWriter xwriter = XmlWriter.Create(sb, settings); xwriter.WriteStartElement("Systems"); XmlSystemWriter writer = new XmlSystemWriter(xwriter); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); xwriter.WriteEndElement(); xwriter.Flush(); xwriter.Close(); } string s2xml = sb.ToString(); Console.WriteLine(s2xml); // READER XML BACK TO SYSTEM 2 /* * HINT: would be simpler to just call: * MathSystem s2 = MathSystem.ReadXml(s2xml, c); */ IMathSystem s2; { StringReader sr = new StringReader(s2xml); XmlReader xreader = XmlReader.Create(sr); xreader.ReadToFollowing("Systems"); xreader.Read(); SystemWriter writer = new SystemWriter(); XmlSystemReader reader = new XmlSystemReader(writer); reader.ReadSystems(xreader, false); xreader.ReadEndElement(); s2 = writer.WrittenSystems.Dequeue(); } Assert.AreEqual(0, s2.BusCount, "B0"); Assert.AreEqual(5, s2.SignalCount, "B1"); Assert.AreEqual(3, s2.PortCount, "B2"); Assert.AreEqual("Std.Real(1.55614639377584)", s2.GetOutput(0).Value.ToString(), "B3"); // EVALUATE SYSTEM 2 FOR x=2.5 s2.GetInput(0).PostNewValue(new RealValue(2.5)); p.SimulateInstant(); Assert.AreEqual("Std.Real(-0.0663584330951136)", s2.GetOutput(0).Value.ToString(), "C0"); //-0.0331792165475568 }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List <CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }
public void Pattern_CoalescedTreeMatching() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; CoalescedTreeNode root = new CoalescedTreeNode(AlwaysTrueCondition.Instance); root.Subscribe(new MathIdentifier("A", "Test")); CoalescedTreeNode sin = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Sine", "Std"))); sin.AddGroup(new MathIdentifier("B", "Test"), "sin"); root.ConditionAxis.Add(sin); CoalescedTreeNode sqr = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Square", "Std"))); sqr.AddGroup(new MathIdentifier("B", "Test"), "sqr"); sqr.Subscribe(new MathIdentifier("B", "Test")); CoalescedChildPattern sqrPattern = new CoalescedChildPattern(); sqrPattern.AddChild(sqr); sin.PatternAxis.Add(sqrPattern); CoalescedTreeNode tan = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Tangent", "Std"))); tan.AddGroup(new MathIdentifier("B", "Test"), "tan"); tan.Subscribe(new MathIdentifier("C", "Test")); root.ConditionAxis.Add(tan); MatchCollection res = root.MatchAll(sinx2, sinx2.DrivenByPort, 1); Assert.AreEqual(true, res.Contains(new MathIdentifier("A", "Test")), "C01"); Assert.AreEqual(true, res.Contains(new MathIdentifier("B", "Test")), "C02"); Assert.AreEqual(false, res.Contains(new MathIdentifier("C", "Test")), "C03"); Match mA = res[new MathIdentifier("A", "Test")]; Assert.AreEqual(new MathIdentifier("A", "Test"), mA.PatternId, "C04"); Assert.AreEqual(0, mA.GroupCount, "C05"); Match mB = res[new MathIdentifier("B", "Test")]; Assert.AreEqual(new MathIdentifier("B", "Test"), mB.PatternId, "C06"); Assert.AreEqual(2, mB.GroupCount, "C07"); Group mBsqr = mB["sqr"]; Assert.AreEqual(1, mBsqr.Count, "C08"); Assert.AreEqual(x2.InstanceId, mBsqr[0].First.InstanceId, "C09"); Assert.AreEqual(x2.DrivenByPort.InstanceId, mBsqr[0].Second.InstanceId, "C10"); Group mBsin = mB["sin"]; Assert.AreEqual(1, mBsin.Count, "C11"); Assert.AreEqual(sinx2.InstanceId, mBsin[0].First.InstanceId, "C12"); Assert.AreEqual(sinx2.DrivenByPort.InstanceId, mBsin[0].Second.InstanceId, "C13"); }