예제 #1
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");
        }
예제 #2
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);
        }
        public static void RegisterTheorems(ILibrary library)
        {
            Analysis.DerivativeTransformation.Provider.Add(
                new Analysis.DerivativeTransformation(_entityId,
                                                      delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs)
            {
                return(new SignalSet(Std.Add(manipulatedInputs)));
            }));

            Algebra.AutoSimplifyTransformation.Provider.Add(
                new Algebra.AutoSimplifyTransformation(_entityId,
                                                       delegate(Port port)
            {
                // TODO
                return(ManipulationPlan.DoAlter);
            },
                                                       delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs)
            {
                if (SimplifySummands(manipulatedInputs) || hasManipulatedInputs)
                {
                    if (manipulatedInputs.Count == 0)
                    {
                        return(new SignalSet(IntegerValue.ConstantAdditiveIdentity));
                    }
                    if (manipulatedInputs.Count == 1)
                    {
                        return(manipulatedInputs);
                    }
                    return(new SignalSet(StdBuilder.Add(manipulatedInputs)));
                }
                else
                {
                    return(port.OutputSignals);
                }
            }));
        }
        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");
        }