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 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 RaisedToNumberThenAddTest()
    {
        var actual = AddOp.Create(PowerOp.Create(A, One), Two);

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

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

        Assert.AreEqual(expected, actual);
    }
    public void AddToLiteralTest()
    {
        var actual1 = AddOp.Create(new Identifier("x"), 1);
        var actual2 = AddOp.Create(1, new Identifier("x"));

        Assert.AreEqual(X + One, actual1);
        Assert.AreEqual(One + X, actual2);
    }
    public void XRaiseToXTimesXRaiseTo2()
    {
        var x        = new Identifier("x");
        var xToX     = x.Raise(x);
        var xTo2     = x.Raise(new Two());
        var expected = x.Raise(AddOp.Create(2, x));

        Assert.AreEqual(xToX * xTo2, expected);
    }
    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 SubtractToNegativeLiteralTest()
    {
        var num     = new One();
        var actual1 = AddOp.Create(new Identifier("x"), 1);
        var actual2 = AddOp.Create(1, new Identifier("x"));

        Assert.AreEqual(X - NegativeOp.Create(num), actual1);
        Assert.AreEqual(num - NegativeOp.Create(X), actual2);
    }
Exemplo n.º 10
0
 public static Expr Create(Expr left, Expr right) => (left, right) switch
 {
     (Zero _, _) => - right,
     (_, Zero _) => left,
     (_, NegativeOp {
         Expr : Literal l
     }) => AddOp.Create(left, l),
     _ => new SubtractOp(left, right)
 };
    public void DecimalTest()
    {
        var onePthree = ExprParser.ParseOrThrow("1.3");

        Assert.AreEqual(onePthree, DivideOp.Create(13, 10));
        var actual = AddOp.Create(PowerOp.Create(A, onePthree), Two);

        Assert.AreEqual(ExprParser.ParseOrThrow("a^1.3+2"), actual);
        Assert.AreEqual(A.Raise(onePthree) + Two, actual.Simplify());
    }
    public void NegativeBeforeAddTest()
    {
        const string input  = "-12 + 3";
        var          actual = AddOp.Create(
            NegativeOp.Create(Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), 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 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 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 AddToIdentifierDifferent()
 {
     Assert.AreEqual(i1 + i3, AddOp.Create(new Identifier("x"), new Identifier("y")));
     Assert.AreEqual(i3 + i1, AddOp.Create(new Identifier("y"), new Identifier("x")));
 }