public void AutoSimplify_MathOp_AlgebraDerive() { IMathSystem s = _p.CurrentSystem; _p.Interpret(@"Signal x;"); _p.Interpret(@"Signal fn;"); _p.Interpret(@"Signal fdiv;"); _p.Interpret(@"Signal fdiff;"); _p.Interpret(@"fn <- 3*exp(-3*x)-x;"); _p.Interpret(@"fdiff <- diff(3*exp(-3*x)-x, x);"); _p.Interpret(@"fdiv <- fn / fdiff;"); Signal x = s.LookupNamedSignal("x"); Std.ConstrainAlwaysReal(x); Signal fdiv = s.LookupNamedSignal("fdiv"); Assert.AreEqual(new MathIdentifier("Divide", "Std"), fdiv.DrivenByPort.Entity.EntityId, "A"); Signal fdiv_n = fdiv.DrivenByPort.InputSignals[0]; Signal fdiv_d = fdiv.DrivenByPort.InputSignals[1]; Assert.AreEqual(new MathIdentifier("Subtract", "Std"), fdiv_n.DrivenByPort.Entity.EntityId, "B"); Assert.AreEqual(new MathIdentifier("Derive", "Std"), fdiv_d.DrivenByPort.Entity.EntityId, "C"); // Execute MathOp Std.Derive Signal simplified = Std.AutoSimplify(fdiv); Assert.AreEqual("(3*exp(-3*x)+-1*x)*(-1+-9*exp(-3*x))^(-1)", _f.Format(simplified, FormattingOptions.Compact), "D"); Assert.AreEqual(new MathIdentifier("Multiply", "Std"), simplified.DrivenByPort.Entity.EntityId, "E"); Signal simplified_n = simplified.DrivenByPort.InputSignals[0]; Signal simplified_d = simplified.DrivenByPort.InputSignals[1]; Assert.AreEqual(new MathIdentifier("Add", "Std"), simplified_n.DrivenByPort.Entity.EntityId, "F"); Assert.AreEqual(new MathIdentifier("Power", "Std"), simplified_d.DrivenByPort.Entity.EntityId, "G"); s.PromoteAsInput(x); s.AddSignalTree(simplified, true, false); //s.PromoteAsOutput(simplified); s.RemoveUnusedObjects(); // The Derive Mapping should be removed from the system Assert.IsFalse(s.GetAllPorts().Exists(delegate(Port port) { return(port.Entity.EqualsById(new MathIdentifier("Derive", "Std"))); }), "H"); Assert.AreEqual(-0.3, s.Evaluate(0.0)[0], 1e-8, "x=0.0"); Assert.AreEqual(.5874238104, s.Evaluate(1.0)[0], 1e-8, "x=1.0"); Assert.AreEqual(3.139070661, s.Evaluate(Constants.Pi)[0], 1e-8, "x=Pi"); Assert.AreEqual(-.3334664750, s.Evaluate(-2.5)[0], 1e-8, "x=-2.5"); }
private void InitializeObserverWithCurrentSystem(ISystemObserver observer) { if (_system == null) { return; } observer.BeginInitialize(); ReadOnlySignalSet allSignals = _system.GetAllSignals(); for (int i = 0; i < allSignals.Count; i++) { observer.OnSignalAdded(allSignals[i], i); } ReadOnlyBusSet allBuses = _system.GetAllBuses(); for (int i = 0; i < allBuses.Count; i++) { observer.OnBusAdded(allBuses[i], i); } ReadOnlyPortSet allPorts = _system.GetAllPorts(); for (int i = 0; i < allPorts.Count; i++) { observer.OnPortAdded(allPorts[i], i); } for (int i = 0; i < allSignals.Count; i++) { Signal s = allSignals[i]; if (s.IsDriven && allPorts.Contains(s.DrivenByPort)) { observer.OnPortDrivesSignal(s, s.DrivenByPort, s.DrivenByPort.OutputSignals.IndexOf(s)); } } for (int i = 0; i < allPorts.Count; i++) { Port p = allPorts[i]; for (int j = 0; j < p.InputSignalCount; j++) { Signal s = p.InputSignals[j]; if (allSignals.Contains(s)) { observer.OnSignalDrivesPort(s, p, j); } } } ReadOnlySignalSet inputs = _system.GetAllInputs(); for (int i = 0; i < inputs.Count; i++) { observer.OnInputAdded(inputs[i], i); } ReadOnlySignalSet outputs = _system.GetAllOutputs(); for (int i = 0; i < outputs.Count; i++) { observer.OnOutputAdded(outputs[i], i); } observer.EndInitialize(); }