public void BracketsTest()
    {
        var actual   = ExprParser.ParseOrThrow("1+[2+y]/{x*(y+4)}");
        var expected = new One() + (AddOp.Create(new Two(), new Identifier("y")) / MultiplyOp.Create(new Identifier("x"), AddOp.Create(4, new Identifier("y"))));

        Assert.AreEqual(expected, actual);
    }
    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);
    }
    public void Test5()
    {
        const string input    = "a*b/c";
        var          actual   = DivideOp.Create(MultiplyOp.Create(new Identifier("a"), new Identifier("b")), new Identifier("c")).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected, actual);
    }
    public void TwoXRaiseToTwoX()
    {
        var x    = new Identifier("x");
        var TwoX = MultiplyOp.Create(2, x);
        var test = TwoX.Raise(TwoX);

        Assert.IsTrue(test is MultiplyOp m && m.Left == PowerOp.Create(4, x));
        Assert.AreEqual(test, MultiplyOp.Create(PowerOp.Create(4, x), PowerOp.Create(x, TwoX)));
    }
    public void AddTest1()
    {
        var d = ExprParser.ParseOrThrow("2*x");
        var e = ExprParser.ParseOrThrow("1*x");
        var f = ExprParser.ParseOrThrow("2");
        var h = ExprParser.ParseOrThrow("2");

        Assert.AreEqual(d + e + f + h, AddOp.Create(MultiplyOp.Create(3, new Identifier("x")), 4));
    }
    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 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);
    }
Esempio n. 9
0
        public Expr ToExpr()
        {
            var(VN, VSN, VRN) = _variables.OfType <VarPowN, VarN, VarRootN>();
            var numerators = VN.Concat <Variable>(VSN).Concat(VRN).Select(f => f.ToExpr());

            var(VD, VSD, VRD) = _variables.OfType <VarPowD, VarD, VarRootD>();
            var denominators = VD.Concat <Variable>(VSD).Concat(VRD).Select(f => f.ToExpr());
            var numerator    = numerators.IsEmpty() ? new One() : numerators.Aggregate((n1, n2) => MultiplyOp.Create(n1, n2));

            if (denominators.IsEmpty())
            {
                return(numerator);
            }
            return(DivideOp.Create(numerator, denominators.Aggregate((n1, n2) => MultiplyOp.Create(n1, n2))));
        }
    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);
    }
    public void TwoXRaiseTox()
    {
        var x = new Identifier("x");

        Assert.AreEqual(MultiplyOp.Create(2, x).Raise(x), MultiplyOp.Create(PowerOp.Create(2, x), PowerOp.Create(x, x)));
    }
    public void TwoxRaiseTo3()
    {
        var d = ExprParser.ParseOrThrow("(2*x)^3").Simplify();

        Assert.AreEqual(d, MultiplyOp.Create(8, PowerOp.Create(new Identifier("x"), 3)));
    }
 public void AddToIdentifierSame()
 {
     Assert.AreEqual(i1 + i2, MultiplyOp.Create(2, new Identifier("x")));
     Assert.AreEqual(i2 + i1, MultiplyOp.Create(2, new Identifier("x")));
 }
 public void MultiplyToOther()
 {
     Assert.AreEqual(i1 * i3, MultiplyOp.Create(i1, i3));
     Assert.AreEqual(i3 * i1, MultiplyOp.Create(i3, i1));
 }
 public void AddSubtractToItself()
 {
     Assert.AreEqual(X + X, MultiplyOp.Create(2, X));
     Assert.AreEqual(X - X, Zero);
 }
 public void MultipliedByAnyNumber()
 {
     Assert.AreEqual(X * Two, MultiplyOp.Create(2, X));
     Assert.AreEqual(Two * X, MultiplyOp.Create(2, X));
 }