Пример #1
0
    public void CreateDecimalTest()
    {
        var d = Dec.Create(0.5m);

        Assert.AreEqual(d, Literal.Create(1) / Literal.Create(2));
        Assert.AreEqual(d.ToString(), "0.5");
    }
Пример #2
0
    public void CreateDecimalTest2()
    {
        var d = Dec.Create(0.33333333333333m);

        Assert.AreEqual(d, Literal.Create(1) / Literal.Create(3));
        Assert.AreEqual(d.ToString(), "0.33333333333333");
    }
    public void ExprTest()
    {
        const string input  = "12 * 3 + foo(-3, x)() * (2 + 1)";
        var          actual = AddOp.Create(
            MultiplyOp.Create(
                Literal.Create(12),
                Literal.Create(3)
                ),
            MultiplyOp.Create(
                Call.Create(
                    Call.Create(
                        new Identifier("foo"),
                        NegativeOp.Create(Literal.Create(3)),
                        new Identifier("x")
                        )
                    ),
                AddOp.Create(
                    new Two(),
                    new One()
                    )
                )
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
Пример #4
0
    public void Pi()
    {
        var actual   = ExprParser.ParseOrThrow("2×π");
        var expected = Literal.Create(2) * new Identifier("π");

        Assert.AreEqual(expected, actual);
    }
    public void TrailingZerosOnDecimal()
    {
        var f = ExprParser.ParseOrThrow("1.2340000");

        Assert.AreEqual(f, new One() + (Literal.Create(234) / Literal.Create(1000)));
        Assert.AreEqual(f, (Literal.Create(234) / Literal.Create(1000)) + new One());
    }
Пример #6
0
        /// <summary>
        /// This is the command handler that will add the code to multiple the output of the random number primitive by 10
        /// This is called when the user presses the Multiply By 10 button
        /// </summary>
        /// <param name="parameter">The command parameter associated with instance of the command</param>
        /// <param name="selection">The current selection</param>
        /// <param name="host">The Composition host for the session</param>
        /// <param name="site">The document edit site which is managing the edit session</param>
        public static void OnMultipleBy10(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            // The selected item will be the random number node since we only add this command for random numbers
            var node = selection.First().Model as Node;

            if (node == null)
            {
                return;
            }

            // Start a transaction and add content to multiply the output by 10
            using (var transaction = node.TransactionManager.BeginTransaction("Multiple By 10", TransactionPurpose.User))
            {
                // Create a multiple node, position it nicely to the right ot the random number, and add it to the same diagram
                var multiply = Multiply.Create(ElementCreateInfo.ForNew);
                multiply.TopLeft = new SMPoint(node.Bounds.Right + StockDiagramGeometries.StandardNodeWidth, node.Top + (2 * StockDiagramGeometries.GridSize));
                node.Diagram.AddNode(multiply);

                // Wire the random number output to the first input on the multiple node
                node.Diagram.WireWithin((NodeTerminal)node.OutputTerminals.First(), (NodeTerminal)multiply.InputTerminals.First());

                // Create a Double Numeric Constant with an initial value of 10.0
                var literal = Literal.Create(NITypes.Double, 10.0);

                // Position the constant nicely and add it to the diagram
                literal.TopLeft = new SMPoint(node.Left + StockDiagramGeometries.TinyNodeWidth, node.Bounds.Bottom + (2 * StockDiagramGeometries.GridSize));
                node.Diagram.AddNode(literal);

                // Wire the constant to the multiply node
                node.Diagram.WireWithin(literal.OutputTerminal, (NodeTerminal)multiply.InputTerminals.ElementAt(1));

                // Commit the transaction to finish the operation
                transaction.Commit();
            }
        }
    public void ParseSciNotWithParenTest()
    {
        var f = ExprParser.ParseOrThrow("1.2340000E(4)");

        Assert.AreEqual(f, (new One() * Literal.Create(10000)) + Literal.Create(2340));
        Assert.AreEqual(f, Literal.Create(2340) + (Literal.Create(10000) * new One()));
    }
    public void Test6()
    {
        const string input = "(1-a*(3-4)*x/21)^3-(1/2)^5+11-(16.31^3-a)*r*f(x)";
        var          ex2   = ExprParser.ParseOrThrow("16.31^3");

        ex2 = ex2.Simplify();
        Assert.AreEqual(ex2, DivideOp.Create(4338722591, 1000000));
        var a                 = new Identifier("a");
        var one               = new One();
        var three             = Literal.Create(3);
        var four              = Literal.Create(4);
        var x                 = new Identifier("x");
        var twentyone         = Literal.Create(21);
        var two               = new Two();
        var five              = Literal.Create(5);
        var eleven            = Literal.Create(11);
        var sixteenPthirtyone = ExprParser.ParseOrThrow("16.31");
        var r                 = new Identifier("r");
        var fOfx              = Call.Create(new Identifier("f"), x);
        var expected          = ExprParser.ParseOrThrow(input);
        var actual            = (one - (a * (three - four) * x / twentyone)).Raise(three) - (one / two).Raise(five) + eleven - ((sixteenPthirtyone.Raise(three) - a) * r * fOfx).Simplify();

        Assert.AreEqual(expected.Simplify(), actual);
        Assert.AreEqual((a * r * fOfx) - (Literal.Create(4338722591) * r * fOfx / Literal.Create(1000000)) + (a.Raise(three) * x.Raise(three) / Literal.Create(9261)) + (a.Raise(two) * x.Raise(two) / Literal.Create(147)) + (a * x / Literal.Create(7)) + (Literal.Create(383) / Literal.Create(32)), actual);
    }
    public void TrailingZerosOnSciNo()
    {
        var f = ExprParser.ParseOrThrow("1.2340000E4");

        Assert.AreEqual(f, (new One() * Literal.Create(10000)) + Literal.Create(2340));
        Assert.AreEqual(f, Literal.Create(2340) + (Literal.Create(10000) * new One()));
    }
Пример #10
0
 public static Expr ToExpr(this BigInteger i)
 {
     if (i < 0)
     {
         return(NegativeOp.Create(Literal.Create(BigInteger.Abs(i))));
     }
     return(Literal.Create(i));
 }
    public void SqrtOf8()
    {
        var sqrtOf8 = SqrtOp.Create(Literal.Create(8)).Simplify();
        var two     = new Two();
        var three   = Literal.Create(3);
        var actual  = two.Raise(three / two);

        Assert.AreEqual(sqrtOf8, actual);
    }
    public void NegateDivideTest()
    {
        const string input  = "- (1/3)";
        var          actual = NegativeOp.Create(
            DivideOp.Create(new One(), Literal.Create(3))
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected, actual);
    }
    public void SqrtN16()
    {
        var NegSixteen   = NegativeOp.Create(Literal.Create(16));
        var sqrt         = NegSixteen.Sqrt();
        var four         = Literal.Create(4);
        var sqrtOfNegOne = SqrtOp.Create(-1).Simplify();
        var expected     = four * sqrtOfNegOne;

        Assert.AreEqual(sqrt, expected);
    }
    public void FractionRaiseToFraction()
    {
        var twentyFive = Literal.Create(25);
        var thirtyTwo  = Literal.Create(32);
        var oneTenth   = DivideOp.Create(1, 10);
        var result     = (thirtyTwo / twentyFive).Raise(oneTenth);
        var actual     = SqrtOp.Create(2) / PowerOp.Create(5, DivideOp.Create(1, 5));

        Assert.AreEqual(result, actual);
    }
    public void EqualityTest()
    {
        const string input  = "12 * 3";
        var          actual = MultiplyOp.Create(
            Literal.Create(12),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void SubtractBeforeMultTest()
    {
        const string input  = "d-12 * 3";
        var          actual = SubtractOp.Create(
            new Identifier("d"),
            MultiplyOp.Create(Literal.Create(12), Literal.Create(3))
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input).Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void Test3()
    {
        const string input    = "-a/b^1+3-a/b^1";
        var          term1    = DivideOp.Create(NegativeOp.Create(new Identifier("a")), PowerOp.Create(new Identifier("b"), new One()));
        var          term2    = Literal.Create(3);
        var          term3    = DivideOp.Create(new Identifier("a"), PowerOp.Create(new Identifier("b"), new One()));
        var          actual   = SubtractOp.Create(AddOp.Create(term1, term2), term3).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void SubtractionTest()
    {
        const string input  = "12 - 3";
        var          actual = SubtractOp.Create(
            Literal.Create(12),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void NegativeBeforeSubtractTest()
    {
        const string input  = "-12 - 3";
        var          actual = SubtractOp.Create(
            NegativeOp.Create(Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input).Simplify();

        Assert.AreEqual(expected, actual);
    }
Пример #20
0
 public static Expr Create(Expr left, Expr right) => (left, right) switch
 {
     (null, _) => new Zero(),
     (_, null) => new Zero(),
     (_, Zero z) => z,
     (Zero z, _) => z,
     (One _, _) => right,
     (_, One _) => left,
     (Literal l, Literal r) => Literal.Create(l.Value * r.Value),
     (FractOp d1, FractOp d2) => DivideOp.Create(d1.Left * d2.Left, d1.Right * d2.Right),
     _ => new MultiplyOp(left, right)
 };
    public void Test4()
    {
        const string input    = "1-2/3+1-3+3*2";
        var          a        = new One();
        var          b        = DivideOp.Create(new Two(), Literal.Create(3));
        var          c        = new One();
        var          d        = Literal.Create(3);
        var          e        = MultiplyOp.Create(Literal.Create(3), new Two());
        var          actual   = AddOp.Create(SubtractOp.Create(AddOp.Create(SubtractOp.Create(a, b), c), d), e).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void FunctionMethodNegateParamTest()
    {
        const string input  = "foo(-3, x)()";
        var          actual =
            Call.Create(
                Call.Create(
                    new Identifier("foo"),
                    NegativeOp.Create(Literal.Create(3)),
                    new Identifier("x")
                    )).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected, actual);
    }
Пример #23
0
 public IEnumerable <object[]> GetData(MethodInfo methodInfo)
 {
     for (int j = 0; j < 100; j++)
     {
         var        d       = randomizer.Next(10, 50);
         var        tempArr = new Expr[d];
         BigInteger total   = 1;
         for (int i = 0; i < d; i++)
         {
             var val = data[randomizer.Next(4)];
             total      = total * val;
             tempArr[i] = Literal.Create(val);
         }
         var actual   = tempArr.GroupBy((e) => e).Select((g) => g.Count() == 1 ? g.Key : PowerOp.Create(g.Key, g.Count()));
         var expected = Literal.Create(total).Factor();
         yield return(new object[] { total.ToString(), actual, expected });
     }
 }
    public void Beta1Formula()
    {
        var actual   = ExprParser.ParseOrThrow("0.85-(0.05/7)*(fᐠ_c-28)");
        var expected = Dec.Create(0.85m) - (Dec.Create(0.05m) / Literal.Create(7)) * (new Identifier("fᐠ_c") - Literal.Create(28));

        Assert.AreEqual(expected, actual);
        Assert.AreEqual("0.85-((0.05/7)×(fᐠ_c-28))", actual.ToString());
        var substiResult = actual.Substitute(new Identifier("fᐠ_c"), 32).Simplify();

        Assert.AreEqual(substiResult, DivideOp.Create(23, 28));
        if (substiResult.TryToDouble(out var val))
        {
            Assert.AreEqual(0.8214, val, 0.0001);
        }
        else
        {
            Assert.Fail($"{substiResult} is not a double");
        }
    }
    public void SubtractThenMultTest()
    {
        var input  = "(d-12) * 3";
        var actual = MultiplyOp.Create(
            SubtractOp.Create(new Identifier("d"), Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.ToString(), "3×(d-12)");
        Assert.AreEqual(expected.Simplify(), actual);
        input  = "(d+12) * 3";
        actual = MultiplyOp.Create(
            AddOp.Create(new Identifier("d"), Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        expected = ExprParser.ParseOrThrow(input).Simplify();
        Assert.AreEqual(expected, actual);
    }
Пример #26
0
 public static Expr ToDecimal(string h, string d) => Literal.Create(h.ToBigInt()) + d.ToFraction();
    public void SqrtOf4()
    {
        var sqrt4 = Literal.Create(4).Raise(DivideOp.Create(1, 2)).Simplify();

        Assert.AreEqual(sqrt4, new Two());
    }
    public void QuantityTwoMinus_a_raisedToQuantity1Plus3Test()
    {
        const string input    = "(2-a)^(1+3)";
        var          actual   = PowerOp.Create(SubtractOp.Create(new Two(), new Identifier("a")), AddOp.Create(new One(), Literal.Create(3))).Simplify();
        var          expected = ExprParser.ParseOrThrow(input).Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void ParseSciNotWithParenTest2()
    {
        var f = ExprParser.ParseOrThrow("-(1.2340000)E(-14)");

        Assert.AreEqual(f, (new NegativeOne() - (Literal.Create(234) / Literal.Create(1000))) * PowerOp.Create(10, -14));
    }
    public void SciNotSmallE()
    {
        var f = ExprParser.ParseOrThrow("-(1.2340000)e(-14)");

        Assert.AreEqual(f, (new NegativeOne() - (Literal.Create(234) / Literal.Create(1000))) * PowerOp.Create(10, -14));
    }