Ejemplo n.º 1
0
        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);

            // ....
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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");
        }
Ejemplo n.º 4
0
        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");
        }
Ejemplo n.º 5
0
        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);
                }
            }));
        }
Ejemplo n.º 6
0
        public void AutoSimplify_RationalNumberExpression()
        {
            // A
            Signal a =
                StdBuilder.Add(
                    StdBuilder.Divide(IntegerValue.Constant(2), IntegerValue.Constant(3)),
                    RationalValue.Constant(3, 4));

            Assert.AreEqual("2/3+3/4", _f.Format(a, FormattingOptions.Compact), "A1");

            Signal aS = Std.AutoSimplify(a);

            Assert.AreEqual("17/12", _f.Format(aS, FormattingOptions.Compact), "A2");

            // B
            Signal b =
                StdBuilder.Power(
                    StdBuilder.Divide(IntegerValue.Constant(4), IntegerValue.Constant(2)),
                    IntegerValue.Constant(3));

            Assert.AreEqual("(4/2)^3", _f.Format(b, FormattingOptions.Compact), "B1");

            Signal bS = Std.AutoSimplify(b);

            Assert.AreEqual("8", _f.Format(bS, FormattingOptions.Compact), "B2");

            // C
            Signal c =
                StdBuilder.Divide(
                    IntegerValue.ConstantOne,
                    StdBuilder.Subtract(
                        RationalValue.Constant(2, 4),
                        RationalValue.ConstantHalf));

            Assert.AreEqual("1/(1/2-1/2)", _f.Format(c, FormattingOptions.Compact), "C1");

            Signal cS = Std.AutoSimplify(c);

            Assert.AreEqual("Std.Undefined", _f.Format(cS, FormattingOptions.Compact), "C2");

            // D
            Signal d =
                StdBuilder.Power(
                    StdBuilder.Power(
                        IntegerValue.Constant(5),
                        IntegerValue.Constant(2)),
                    StdBuilder.Power(
                        IntegerValue.Constant(3),
                        IntegerValue.Constant(1)));

            Assert.AreEqual("(5^2)^(3^1)", _f.Format(d, FormattingOptions.Compact), "D1");

            Signal dS = Std.AutoSimplify(d);

            Assert.AreEqual("15625", _f.Format(dS, FormattingOptions.Compact), "D2");
        }
Ejemplo n.º 7
0
        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);
     }));
 }
Ejemplo n.º 9
0
        public static void RegisterTheorems(ILibrary library)
        {
            //Analysis.DerivativeTransformation.Provider.Add(
            //    new Analysis.DerivativeTransformation(_entityId,
            //    delegate(Port port, Signal[] derivedInputSignals)
            //    {
            //        Signal innerA = derivedInputSignals[1] * StdBuilder.NaturalLogarithm(port.InputSignals[0]);
            //        Signal innerB = (port.InputSignals[1] * derivedInputSignals[0]) / port.InputSignals[0];
            //        return port.OutputSignals[0] * (innerA + innerB);
            //    }));

            Algebra.AutoSimplifyTransformation.Provider.Add(
                new Algebra.AutoSimplifyTransformation(_entityId,
                                                       delegate(Port port)
            {
                // TODO
                return(ManipulationPlan.DoAlter);
            },
                                                       delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs)
            {
                if (SimplifyOperands(manipulatedInputs) || hasManipulatedInputs)
                {
                    if (manipulatedInputs.Count == 0)
                    {
                        return(new SignalSet(IntegerValue.ConstantMultiplicativeIdentity));
                    }
                    if (manipulatedInputs.Count == 1)
                    {
                        return(manipulatedInputs);
                    }
                    if (Std.IsConstantMultiplicativeIdentity(manipulatedInputs[0]))
                    {
                        return(new SignalSet(manipulatedInputs[0]));
                    }
                    if (Std.IsConstantMultiplicativeIdentity(manipulatedInputs[1]))
                    {
                        return(new SignalSet(manipulatedInputs[0]));
                    }
                    if (Std.IsConstantAdditiveIdentity(manipulatedInputs[1]))
                    {
                        return(new SignalSet(IntegerValue.ConstantOne));
                    }
                    return(new SignalSet(StdBuilder.Power(manipulatedInputs)));
                }
                else
                {
                    return(port.OutputSignals);
                }
            }));
        }
Ejemplo n.º 10
0
        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 static void RegisterTheorems(ILibrary library)
 {
     Analysis.DerivativeTransformation.Provider.Add(
         new Analysis.DerivativeTransformation(_entityId,
                                               delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs)
     {
         Signal[] outputs        = new Signal[manipulatedInputs.Count];
         ReadOnlySignalSet sines = StdBuilder.Sine(port.InputSignals);
         for (int i = 0; i < outputs.Length; i++)
         {
             outputs[i] = sines[i] * manipulatedInputs[i];
         }
         return(StdBuilder.Negate(outputs));
     }));
 }
Ejemplo n.º 12
0
        public void Scheduler_PortTransportTest()
        {
            Signal a = Binder.CreateSignal();
            Signal b = Binder.CreateSignal();
            Signal c = StdBuilder.Divide(a, b);

            Signal d = Binder.CreateSignal();
            Signal e = Binder.CreateSignal();
            Signal f = Service <IBuilder> .Instance.Function("xor", d, e);

            a.PostNewValue(new RationalValue(1, 1));
            b.PostNewValue(new IntegerValue(2));
            d.PostNewValue(LogicValue.True);
            e.PostNewValue(LogicValue.False);

            Assert.IsNull(a.Value);
            Assert.IsNull(b.Value);
            Assert.IsNull(c.Value);
            Assert.IsNull(d.Value);
            Assert.IsNull(e.Value);
            Assert.IsNull(f.Value);

            Assert.AreEqual(TimeSpan.Zero, project.SimulateFor(1));
            Assert.IsNotNull(a.Value);
            Assert.IsNotNull(b.Value);
            Assert.IsNull(c.Value);
            Assert.IsNotNull(d.Value);
            Assert.IsNotNull(e.Value);
            Assert.IsNull(f.Value);

            Assert.AreEqual(TimeSpan.Zero, project.SimulateFor(1));
            Assert.IsNotNull(c.Value);
            Assert.IsInstanceOfType(typeof(RationalValue), c.Value);
            Assert.IsNotNull(f.Value);
            Assert.IsInstanceOfType(typeof(LogicValue), f.Value);

            Assert.AreEqual(1, ((RationalValue)c.Value).NumeratorValue);
            Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue);
            Assert.AreEqual(ELogicX01.True, ((LogicValue)f.Value).Value);

            a.PostNewValue(new IntegerValue(3), new TimeSpan(0, 1, 0));
            Assert.AreEqual(1, ((RationalValue)c.Value).NumeratorValue);
            Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue);
            Assert.AreEqual(new TimeSpan(0, 1, 0), project.SimulateFor(new TimeSpan(0, 2, 0)));
            Assert.IsInstanceOfType(typeof(RationalValue), c.Value);
            Assert.AreEqual(3, ((RationalValue)c.Value).NumeratorValue);
            Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue);
        }
Ejemplo n.º 13
0
        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");
        }
Ejemplo n.º 14
0
        public void Basic_Traversing()
        {
            Signal a = Binder.CreateSignal(); a.Label = "A";
            Signal b = Binder.CreateSignal(); b.Label = "B";
            Signal c = StdBuilder.Add(a, b); c.Label = "C";      //a + b;
            Signal d = StdBuilder.Multiply(a, c); d.Label = "D"; //a * c;

            Assert.IsTrue(c.DependsOn(a), "1: a->c");
            Assert.IsTrue(c.DependsOn(b), "1: b->c");
            Assert.IsTrue(d.DependsOn(a), "1: a->d");
            Assert.IsTrue(d.DependsOn(b), "1: b->d");
            Assert.IsTrue(d.DependsOn(c), "1: c->d");
            Assert.IsFalse(a.DependsOn(b), "1: b!->a");
            Assert.IsFalse(b.DependsOn(a), "1: a!->b");
            Assert.IsFalse(a.DependsOn(d), "1: d!->a");
            Assert.IsFalse(b.DependsOn(d), "1: d!->b");
            Assert.IsFalse(c.DependsOn(d), "1: d!->c");
            Assert.IsFalse(a.DependsOn(c), "1: c!->a");
            Assert.IsFalse(b.DependsOn(c), "1: c!->b");

            Service <IBuilder> .Instance.MapSignals(c, b);

            Assert.IsTrue(c.DependsOn(a), "2: a->c");
            Assert.IsTrue(c.DependsOn(b), "2: b->c");
            Assert.IsTrue(d.DependsOn(a), "2: a->d");
            Assert.IsTrue(d.DependsOn(b), "2: b->d");
            Assert.IsTrue(d.DependsOn(c), "2: c->d");
            Assert.IsFalse(a.DependsOn(b), "2: b!->a");
            Assert.IsTrue(b.DependsOn(a), "2: a->b"); // NEW
            Assert.IsFalse(a.DependsOn(d), "2: d!->a");
            Assert.IsFalse(b.DependsOn(d), "2: d!->b");
            Assert.IsFalse(c.DependsOn(d), "2: d!->c");
            Assert.IsFalse(a.DependsOn(c), "2: c!->a");
            Assert.IsTrue(b.DependsOn(c), "2: c->b");  // NEW

            Assert.IsTrue(Service <IScanner> .Instance.ExistsSignal(d, delegate(Signal s) { return(s.Label == "A"); }, false));
            Assert.IsFalse(Service <IScanner> .Instance.ExistsSignal(d, delegate(Signal s) { return(s.Label == "Z"); }, false));
            Assert.AreEqual("B", Service <IScanner> .Instance.FindSignal(d, delegate(Signal s) { return(s.Label == "B"); }, false).Label);
        }
Ejemplo n.º 15
0
        public void Pattern_Conditions()
        {
            Project    p = new Project();
            MathSystem s = p.CurrentSystem;

            // sin(x^2)
            Signal x = Binder.CreateSignal(); x.Label = "x";

            Std.ConstrainAlwaysReal(x);
            Signal x2    = StdBuilder.Power(x, IntegerValue.ConstantTwo); x2.Label = "x2";
            Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2";

            AlwaysTrueCondition ctrue = AlwaysTrueCondition.Instance;

            Assert.AreEqual(true, ctrue.FulfillsCondition(x, x.DrivenByPort), "A01");

            EntityCondition centity = new EntityCondition(new MathIdentifier("Sine", "Std"));

            Assert.AreEqual(true, centity.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A02");
            Assert.AreEqual(false, centity.FulfillsCondition(x2, x2.DrivenByPort), "A03");

            //InputSignalsPropertyCondition cinputconst = new InputSignalsPropertyCondition(new MathIdentifier("Constant", "Std"), CombinationMode.AtLeastOne);
            InputSignalsFlagCondition cinputconst = new InputSignalsFlagCondition(StdAspect.ConstantFlag, FlagState.Enabled, CombinationMode.AtLeastOne);

            Assert.AreEqual(true, cinputconst.FulfillsCondition(x2, x2.DrivenByPort), "A04");
            Assert.AreEqual(false, cinputconst.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A05");

            OrCondition cor = new OrCondition(centity, cinputconst);

            Assert.AreEqual(true, cor.FulfillsCondition(x2, x2.DrivenByPort), "A06");
            Assert.AreEqual(true, cor.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A07");
            Assert.AreEqual(false, cor.FulfillsCondition(x, x.DrivenByPort), "A08");

            AndCondition cand = new AndCondition(ctrue, centity);

            Assert.AreEqual(true, cand.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A09");
            Assert.AreEqual(false, cand.FulfillsCondition(x2, x2.DrivenByPort), "A10");
        }
        public static void RegisterTheorems(ILibrary library)
        {
            Analysis.DerivativeTransformation.Provider.Add(
                new Analysis.DerivativeTransformation(_entityId,
                                                      delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs)
            {
                return(StdBuilder.Negate(manipulatedInputs));
            }));

            Algebra.AutoSimplifyTransformation.Provider.Add(
                new Algebra.AutoSimplifyTransformation(_entityId,
                                                       delegate(Port port)
            {
                // TODO
                return(ManipulationPlan.DoAlter);
            },
                                                       delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs)
            {
                if (manipulatedInputs.Count == 0)
                {
                    return(new SignalSet(IntegerValue.ConstantAdditiveIdentity));
                }
                if (manipulatedInputs.Count == 1)
                {
                    Signal s = manipulatedInputs[0];
                    if (Std.IsConstantAdditiveIdentity(s))
                    {
                        return(new SignalSet(s));
                    }
                    return(new SignalSet(Std.Negate(s)));
                }
                if (hasManipulatedInputs)
                {
                    return(new SignalSet(StdBuilder.Negate(manipulatedInputs)));
                }
                return(port.OutputSignals);
            }));
        }
        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);
            }));
        }
Ejemplo n.º 18
0
        public static void RegisterTheorems(ILibrary library)
        {
            Analysis.DerivativeTransformation.Provider.Add(
                new Analysis.DerivativeTransformation(_entityId,
                                                      delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs)
            {
                Signal[] outputs             = new Signal[manipulatedInputs.Count];
                ReadOnlySignalSet cotangents = StdBuilder.Cotangent(port.InputSignals);
                for (int i = 0; i < outputs.Length; i++)
                {
                    outputs[i] = Std.Multiply(port.OutputSignals[i], cotangents[i], manipulatedInputs[i]);
                }
                return(StdBuilder.Negate(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)
            {
                return(StdBuilder.Invert(StdBuilder.Sine(transformedInputs)));
                //Signal[] ret = new Signal[transformedInputs.Count];
                //for(int i = 0; i < ret.Length; i++)
                //    ret[i] = Std.Invert(Std.Sine(transformedInputs[i]));
                //return ret;
            }));
        }
        public void StdPolynomial_SingleVariable()
        {
            Signal x = Binder.CreateSignal();

            Signal a0 = IntegerValue.ConstantZero;
            Signal a1 = IntegerValue.ConstantOne;
            Signal a2 = RationalValue.ConstantHalf;
            Signal a3 = IntegerValue.Constant(2);
            Signal a4 = IntegerValue.Constant(3);

            Signal badX  = StdBuilder.Sine(x);
            Signal badA2 = StdBuilder.Tangent(a2);
            Signal badA3 = RealValue.ConstantPI;

            Signal x2 = StdBuilder.Power(x, a3);
            Signal x3 = StdBuilder.Power(x, a4);

            Signal a3x3 = a3 * x3;
            Signal a2x2 = a2 * x2;

            Assert.IsTrue(Polynomial.IsMonomial(x, x), "x: is SVM(x)");
            Assert.IsTrue(Polynomial.IsMonomial(a0, x), "0: is SVM(x)");
            Assert.IsTrue(Polynomial.IsMonomial(a2, x), "1/2: is SVM(x)");
            Assert.IsFalse(Polynomial.IsMonomial(badX, x), "sin(x): is not SVM(x)");
            Assert.IsFalse(Polynomial.IsMonomial(badA2, x), "tan(1/2): is not SVM(x)");
            Assert.IsFalse(Polynomial.IsMonomial(badA3, x), "pi is not SVM(x)");
            Assert.IsTrue(Polynomial.IsMonomial(x3, x), "x^3: is SVM(x)");
            Assert.IsTrue(Polynomial.IsMonomial(a2x2, x), "1/2*x^2: is SVM(x)");

            Assert.AreEqual("Std.Integer(1)", Polynomial.MonomialDegree(x, x).ToString(), "x: SVM deg(x)=1");
            Assert.AreEqual("Std.Integer(0)", Polynomial.MonomialDegree(a2, x).ToString(), "1/2: SVM deg(x)=0");
            Assert.AreEqual("Std.NegativeInfinity", Polynomial.MonomialDegree(a0, x).ToString(), "0: SVM deg(x)=-inf");
            Assert.AreEqual("Std.Integer(3)", Polynomial.MonomialDegree(x3, x).ToString(), "x^3: SVM deg(x)=3");
            Assert.AreEqual("Std.Integer(2)", Polynomial.MonomialDegree(a2x2, x).ToString(), "1/2*x^2: SVM deg(x)=2");

            IValueStructure vs;
            Signal          test = Polynomial.MonomialCoefficient(x, x, out vs);

            Assert.AreEqual("Std.Integer(1)", vs.ToString(), "x: SVM coeff deg=1 ");
            Assert.IsTrue(test.Value.Equals(a1.Value), "x: SVM coeff = 1");

            test = Polynomial.MonomialCoefficient(a2, x, out vs);
            Assert.AreEqual("Std.Integer(0)", vs.ToString(), "1/2: SVM coeff deg=0 ");
            Assert.IsTrue(test.Value.Equals(a2.Value), "1/2: SVM coeff = 1/2");

            test = Polynomial.MonomialCoefficient(a3x3, x, out vs);
            Assert.AreEqual("Std.Integer(3)", vs.ToString(), "2*x^3: SVM coeff deg=3 ");
            Assert.IsTrue(test.Value.Equals(a3.Value), "2*x^3: SVM coeff = 2");

            Signal a3x3_a2x2_a4 = StdBuilder.Add(a3x3, a2x2, a4);

            Assert.IsFalse(Polynomial.IsMonomial(a3x3_a2x2_a4, x), "2*x^3+1/2*x^2+3: is not SVM(x)");
            Assert.IsTrue(Polynomial.IsPolynomial(a3x3_a2x2_a4, x), "2*x^3+1/2*x^2+3: is SVP(x)");
            Assert.AreEqual("Std.Integer(3)", Polynomial.PolynomialDegree(a3x3_a2x2_a4, x).ToString(), "2*x^3+1/2*x^2+3: SVP deg(x)=3");

            test = Polynomial.PolynomialCoefficient(a3x3_a2x2_a4, x, 1);
            Assert.IsTrue(test.Value.Equals(a0.Value), "2*x^3+1/2*x^2+3: SVP coeff(1) = 0");

            test = Polynomial.PolynomialCoefficient(a3x3_a2x2_a4, x, 2);
            Assert.IsTrue(test.Value.Equals(a2.Value), "2*x^3+1/2*x^2+3: SVP coeff(2) = 1/2");

            Signal[] coefficients = Polynomial.PolynomialCoefficients(a3x3_a2x2_a4, x);
            Assert.AreEqual(4, coefficients.Length, "2*x^3+1/2*x^2+3: SVP coeffs: len(coeffs) = 4 (-> deg=3)");
            Assert.IsTrue(coefficients[0].Value.Equals(a4.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[0] = 3");
            Assert.IsTrue(coefficients[1].Value.Equals(a0.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[1] = 0");
            Assert.IsTrue(coefficients[2].Value.Equals(a2.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[2] = 1/2");
            Assert.IsTrue(coefficients[3].Value.Equals(a3.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[3] = 2");
        }
Ejemplo n.º 20
0
        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
        }
Ejemplo n.º 21
0
        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");
        }
Ejemplo n.º 22
0
 protected override MathNet.Symbolics.Signal NegateSignalCore()
 {
     return(StdBuilder.Negate(this));
 }
Ejemplo n.º 23
0
        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");
        }