Beispiel #1
0
        public void EqualsTest2()
        {
            var add = new Add(new Number(2), new Number(3));
            var sub = new Sub(new Number(2), new Number(3));

            Assert.NotEqual<IExpression>(add, sub);
        }
        public void AddDerivativeTest2()
        {
            var exp = new Add(new Mul(new Number(2), new Variable("x")), new Mul(new Number(3), new Variable("x")));
            var deriv = Differentiate(exp);

            Assert.AreEqual("(2 * 1) + (3 * 1)", deriv.ToString());
        }
Beispiel #3
0
        public void EqualsTest1()
        {
            var add1 = new Add(new Number(2), new Number(3));
            var add2 = new Add(new Number(2), new Number(3));

            Assert.AreEqual(add1, add2);
        }
Beispiel #4
0
        public void DerivativeTest2()
        {
            IExpression exp = new Add(new Mul(new Number(2), new Variable("x")), new Mul(new Number(3), new Variable("x")));
            IExpression deriv = exp.Differentiate();

            Assert.AreEqual("(2 * 1) + (3 * 1)", deriv.ToString());
        }
Beispiel #5
0
        public void EqualsTest2()
        {
            var add = new Add(new Number(2), new Number(3));
            var sub = new Sub(new Number(2), new Number(3));

            Assert.AreNotEqual(add, sub);
        }
Beispiel #6
0
        public void AddDiffNumAdd_VarAddNum_()
        {
            // 2 + (x + 2)
            var add = new Add(new Number(2), new Add(new Variable("x"), new Number(2)));
            var expected = new Add(new Variable("x"), new Number(4));

            SimpleTest(add, expected);
        }
Beispiel #7
0
        public void AddDiffNum_NumSubVar_()
        {
            // 2 + (2 - x)
            var add = new Add(new Number(2), new Sub(new Number(2), new Variable("x")));
            var expected = new Sub(new Number(4), new Variable("x"));

            SimpleTest(add, expected);
        }
Beispiel #8
0
        public void AddDiffNum_VarSubNum_()
        {
            // 2 + (x - 2)
            var add = new Add(new Number(2), new Sub(new Variable("x"), new Number(2)));
            var expected = new Variable("x");

            SimpleTest(add, expected);
        }
Beispiel #9
0
        public void AddDiff_NumSubVar_AddNum()
        {
            // (2 - x) + 2
            var add = new Add(new Sub(new Number(2), new Variable("x")), new Number(2));
            var expected = new Sub(new Number(4), new Variable("x"));

            SimpleTest(add, expected);
        }
Beispiel #10
0
        public void AddDiffNumAdd_NumAddVar_()
        {
            // 2 + (2 + x)
            var add = new Add(new Number(2), new Add(new Number(2), new Variable("x")));
            var expected = new Add(new Variable("x"), new Number(4));

            SimpleTest(add, expected);
        }
Beispiel #11
0
        public void AddTwoVectorsTest()
        {
            var vector1 = new Vector(new[] { new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(7), new Number(1) });
            var add = new Add(vector1, vector2);

            var expected = new Vector(new[] { new Number(9), new Number(4) });
            var result = add.Execute();

            Assert.Equal(expected, result);
        }
Beispiel #12
0
        public void MultiOpAddMul()
        {
            // ({2, 3, 4} + {1, 2, 3}) * 4
            var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) });
            var add = new Add(vector2, vector1);
            var mul = new Mul(add, new Number(4));

            var expected = new Vector(new[] { new Number(12), new Number(20), new Number(28) });
            var result = mul.Execute();

            Assert.Equal(expected, result);
        }
Beispiel #13
0
        public void MultiOpMulAdd()
        {
            // ({1, 2, 3} * 4) + {2, 3, 4}
            var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) });
            var mul = new Mul(vector1, new Number(4));
            var add = new Add(mul, vector2);

            var expected = new Vector(new[] { new Number(6), new Number(11), new Number(16) });
            var result = add.Calculate();

            Assert.AreEqual(expected, result);
        }
Beispiel #14
0
        public void Add4MatricesTest()
        {
            var vector1 = new Vector(new IExpression[] { new Number(1), new Number(2) });
            var vector2 = new Vector(new IExpression[] { new Number(1), new Number(2) });
            var vector3 = new Vector(new IExpression[] { new Number(1), new Number(2) });
            var vector4 = new Vector(new IExpression[] { new Number(1), new Number(2) });
            var add1 = new Add(vector1, vector2);
            var add2 = new Add(vector3, vector4);
            var add3 = new Add(add1, add2);

            var expected = new Vector(new IExpression[] { new Number(4), new Number(8) });

            Assert.Equal(expected, add3.Execute());
        }
Beispiel #15
0
        public void DiffExpTest()
        {
            var differentiator = new Mock<IDifferentiator>();

            var exp = new Add(new Variable("x"), new Number(1));
            var diff = new Number(1);

            differentiator.Setup(d => d.Analyze(exp)).Returns(() => diff);

            var processor = new Processor(null, null, null, differentiator.Object);
            var result = processor.Differentiate(exp);

            differentiator.Verify(d => d.Analyze(exp), Times.Once());

            Assert.Equal(diff, result);
        }
Beispiel #16
0
        public void AddTest()
        {
            var tokens = new List<IToken>
            {
                new FunctionToken(Functions.Add, 2),
                new SymbolToken(Symbols.OpenBracket),
                new NumberToken(1),
                new SymbolToken(Symbols.Comma),
                new NumberToken(2),
                new SymbolToken(Symbols.CloseBracket)
            };

            var exp = parser.Parse(tokens);
            var expected = new Add(new Number(1), new Number(2));

            Assert.Equal(expected, exp);
        }
Beispiel #17
0
        public void DiffParamsTest()
        {
            var differentiator = new Mock<IDifferentiator>();

            var exp = new Add(new Variable("x"), new Number(1));
            var diff = new Number(1);

            differentiator.Setup(d => d.Analyze(exp)).Returns(() => diff);
            differentiator.SetupProperty(d => d.Variable);
            differentiator.SetupProperty(d => d.Parameters);

            var diffObj = differentiator.Object;
            var processor = new Processor(null, null, null, diffObj);
            var result = processor.Differentiate(exp, new Variable("x"), new ExpressionParameters());

            differentiator.Verify(d => d.Analyze(exp), Times.Once());

            Assert.Equal("x", diffObj.Variable.Name);
            Assert.NotNull(diffObj.Parameters);
            Assert.Equal(diff, result);
        }
Beispiel #18
0
        public void AddTwoMatricesTest()
        {
            var matrix1 = new Matrix(new[]
            {
                new Vector(new[] { new Number(6), new Number(3) }),
                new Vector(new[] { new Number(2), new Number(1) })
            });
            var matrix2 = new Matrix(new[]
            {
                new Vector(new[] { new Number(9), new Number(2) }),
                new Vector(new[] { new Number(4), new Number(3) })
            });
            var add = new Add(matrix1, matrix2);

            var expected = new Matrix(new[]
            {
                new Vector(new[] { new Number(15), new Number(5) }),
                new Vector(new[] { new Number(6), new Number(4) })
            });
            var result = add.Execute();

            Assert.Equal(expected, result);
        }
Beispiel #19
0
        public void BinaryToStringTest()
        {
            var exp = new Add(new ComplexNumber(3, 2), new ComplexNumber(3, 2));

            Assert.Equal("3+2i + 3+2i", exp.ToString());
        }
Beispiel #20
0
        public void AddDiff_VarSubNum_AddNum()
        {
            // (x - 2) + 2
            var add = new Add(new Sub(new Variable("x"), new Number(2)), new Number(2));
            var expected = new Variable("x");

            SimpleTest(add, expected);
        }
Beispiel #21
0
        public void AddTwoNumbers()
        {
            var add = new Add(new Number(3), new Number(2));
            var expected = new Number(5);

            SimpleTest(add, expected);
        }
Beispiel #22
0
        public void AddSaveVars11()
        {
            // 3x + -4x
            var var = new Variable("x");
            var exp = new Add(new Mul(new Number(3), var), new Mul(new Number(-4), var));
            var expected = new UnaryMinus(var);

            SimpleTest(exp, expected);
        }
Beispiel #23
0
        public void AddSecondUnaryMinus()
        {
            var add = new Add(new Number(2), new UnaryMinus(new Variable("x")));
            var expected = new Sub(new Number(2), new Variable("x"));

            SimpleTest(add, expected);
        }
Beispiel #24
0
        public void AddSaveVars9()
        {
            // x * 2 + x * 3
            var var = new Variable("x");
            var exp = new Add(new Mul(var, new Number(2)), new Mul(var, new Number(3)));
            var expected = new Mul(new Number(5), var);

            SimpleTest(exp, expected);
        }
Beispiel #25
0
        public void AddSaveVars7()
        {
            // -x + 2x
            var var = new Variable("x");
            var exp = new Add(new UnaryMinus(var), new Mul(new Number(2), var));
            var expected = var;

            SimpleTest(exp, expected);
        }
Beispiel #26
0
        public void AddSaveVars6()
        {
            // -x + x
            var var = new Variable("x");
            var exp = new Add(new UnaryMinus(var), var);
            var expected = new Number(0);

            SimpleTest(exp, expected);
        }
Beispiel #27
0
        public void AddSaveVars5()
        {
            // 2x + 3x
            var var = new Variable("x");
            var exp = new Add(new Mul(new Number(2), var), new Mul(new Number(3), var));
            var expected = new Mul(new Number(5), var);

            SimpleTest(exp, expected);
        }
Beispiel #28
0
        public void AddSecondZero()
        {
            var add = new Add(new Variable("x"), new Number(0));
            var expected = new Variable("x");

            SimpleTest(add, expected);
        }
Beispiel #29
0
        public void AddSaveVars3()
        {
            // x + 2x
            var var = new Variable("x");
            var exp = new Add(var, new Mul(new Number(2), var));
            var expected = new Mul(new Number(3), var);

            SimpleTest(exp, expected);
        }
Beispiel #30
0
        public void AddSaveVars10()
        {
            // 3x + -2x
            var var = new Variable("x");
            var exp = new Add(new Mul(new Number(3), var), new Mul(new Number(-2), var));
            var expected = var;

            SimpleTest(exp, expected);
        }