Example #1
0
        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 ScanDefineArchitecture()
        {
            tokenizer.Match(TokenTypes.TextIdentifier, "architecture");
            MathIdentifier architectureId = ScanEntityMathIdentifierOrLabel(true);
            MathIdentifier entityId       = ScanEntityMathIdentifierOrLabel(true);
            IEntity        entity         = library.LookupEntity(entityId);

            tokenizer.Match("{");
            IMathSystem originalSystem = system;
            IMathSystem tempSystem     = Binder.CreateSystem();

            system = tempSystem;
            while (tokenizer.LookaheadFistToken.Text != "}")
            {
                NextStatement();
            }
            foreach (string input in entity.InputSignals)
            {
                tempSystem.PromoteAsInput(tempSystem.LookupNamedSignal(input));
            }
            foreach (string output in entity.OutputSignals)
            {
                tempSystem.AddSignalTree(tempSystem.LookupNamedSignal(output), tempSystem.GetAllInputs(), true, false);
            }
            ReadOnlySignalSet leafs = tempSystem.GetAllLeafSignals();

            foreach (Signal s in leafs)
            {
                Signal so;
                if (originalSystem.TryLookupNamedSignal(s.Label, out so) && so.Value != null)
                {
                    s.PostNewValue(so.Value);
                }
            }
            Service <ISimulationMediator> .Instance.SimulateInstant();

            system = originalSystem;
            tokenizer.Match("}");
            tempSystem.RemoveUnusedObjects();
            tempSystem.PublishToLibrary(architectureId, entity.EntityId);
        }
 public void AppendSystemInputSignal(Guid iid)
 {
     _system.PromoteAsInput(_signalMappings[iid]);
 }