Esempio n. 1
0
        public void ExecuteTest6()
        {
            var exp = new Sub(new Number(2), new Sqrt(new Number(-9)));
            var expected = new Complex(2, -3);

            Assert.Equal(expected, exp.Execute());
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public void ExecuteTest3()
        {
            var exp = new Sub(new Number(7), new ComplexNumber(2, 4));
            var expected = new Complex(5, -4);

            Assert.Equal(expected, exp.Execute());
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public void ExecuteTest4()
        {
            var exp = new Sub(new ComplexNumber(7, 3), new Number(2));
            var expected = new Complex(5, 3);

            Assert.Equal(expected, exp.Execute());
        }
Esempio n. 6
0
        public void DerivativeTest1()
        {
            // x - sin(x)
            IExpression exp = new Sub(new Variable("x"), new Sin(new Variable("x")));
            IExpression deriv = exp.Differentiate();

            Assert.AreEqual("1 - (cos(x) * 1)", deriv.ToString());
        }
Esempio n. 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);
        }
Esempio n. 8
0
        public void SubTwoVectorsTest()
        {
            var vector1 = new Vector(new[] { new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(7), new Number(1) });
            var sub = new Sub(vector1, vector2);

            var expected = new Vector(new[] { new Number(-5), new Number(2) });
            var result = sub.Calculate();

            Assert.AreEqual(expected, result);
        }
Esempio n. 9
0
        public void DerivativeTest2()
        {
            Number num1 = new Number(2);
            Variable x = new Variable("x");
            Mul mul1 = new Mul(num1, x);

            Number num2 = new Number(3);
            Mul mul2 = new Mul(num2, x.Clone());

            IExpression exp = new Sub(mul1, mul2);
            IExpression deriv = exp.Differentiate();

            Assert.AreEqual("(2 * 1) - (3 * 1)", deriv.ToString());

            num1.Value = 5;
            num2.Value = 4;
            Assert.AreEqual("(5 * x) - (4 * x)", exp.ToString());
            Assert.AreEqual("(2 * 1) - (3 * 1)", deriv.ToString());
        }
Esempio n. 10
0
        public void SubTwoMatricesTest()
        {
            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 sub = new Sub(matrix1, matrix2);

            var expected = new Matrix(new[]
            {
                new Vector(new[] { new Number(-3), new Number(1) }),
                new Vector(new[] { new Number(-2), new Number(-2) })
            });
            var result = sub.Calculate();

            Assert.AreEqual(expected, result);
        }
Esempio n. 11
0
        public void SubSecondZero()
        {
            var sub = new Sub(new Variable("x"), new Number(0));
            var expected = new Variable("x");

            SimpleTest(sub, expected);
        }
Esempio n. 12
0
        public void SubSameVars9()
        {
            // (x * 3) - (x * 2)
            var x = new Variable("x");
            var sub = new Sub(new Mul(x, new Number(3)), new Mul(x, new Number(2)));
            var expected = x;

            SimpleTest(sub, expected);
        }
Esempio n. 13
0
        public void SubSecondUnaryMinus()
        {
            var sub = new Sub(new Number(2), new UnaryMinus(new Variable("x")));
            var expected = new Add(new Number(2), new Variable("x"));

            SimpleTest(sub, expected);
        }
Esempio n. 14
0
        public void SubSameVars5()
        {
            // x - (x * 2)
            var x = new Variable("x");
            var sub = new Sub(x, new Mul(x, new Number(2)));
            var expected = new UnaryMinus(x);

            SimpleTest(sub, expected);
        }
Esempio n. 15
0
        public void SubSameVars8()
        {
            // 3x - 2x
            var x = new Variable("x");
            var sub = new Sub(new Mul(new Number(3), x), new Mul(new Number(2), x));
            var expected = x;

            SimpleTest(sub, expected);
        }
Esempio n. 16
0
        public void SubSameVars10()
        {
            // (x * 4) - (x * 2)
            var x = new Variable("x");
            var sub = new Sub(new Mul(x, new Number(4)), new Mul(x, new Number(2)));
            var expected = new Mul(new Number(2), x);

            SimpleTest(sub, expected);
        }
Esempio n. 17
0
        public void SubSameVars2()
        {
            // (x - x) - x
            var x = new Variable("x");
            var sub = new Sub(new Sub(x, x), x);
            var expected = new UnaryMinus(x);

            SimpleTest(sub, expected);
        }
Esempio n. 18
0
        private IExpression SimplifySub(Sub sub)
        {
            // sub zero
            if (sub.Left.Equals(zero))
                return _Simplify(new UnaryMinus(sub.Right));
            if (sub.Right.Equals(zero))
                return sub.Left;

            if (sub.Left is Number && sub.Right is Number)
                return new Number((double)sub.Calculate());

            if (sub.Right is UnaryMinus)
            {
                var unMinus = sub.Right as UnaryMinus;
                var add = new Add(sub.Left, unMinus.Argument);

                return add;
            }

            // (2 + x) - 2
            // (x + 2) - 2
            if (sub.Left is Add && sub.Right is Number)
            {
                var bracketAdd = sub.Left as Add;
                var firstNumber = sub.Right as Number;

                if (bracketAdd.Left is Number)
                {
                    var secondNumber = bracketAdd.Left as Number;
                    var result = new Add(bracketAdd.Right, new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()));

                    return _Simplify(result);
                }
                if (bracketAdd.Right is Number)
                {
                    var secondNumber = bracketAdd.Right as Number;
                    var result = new Add(bracketAdd.Left, new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()));

                    return _Simplify(result);
                }
            }
            // 2 - (2 + x)
            // 2 - (x + 2)
            else if (sub.Right is Add && sub.Left is Number)
            {
                var bracketAdd = sub.Right as Add;
                var firstNumber = sub.Left as Number;

                if (bracketAdd.Left is Number)
                {
                    var secondNumber = bracketAdd.Left as Number;
                    var result = new Sub(new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()), bracketAdd.Right);

                    return _Simplify(result);
                }
                if (bracketAdd.Right is Number)
                {
                    var secondNumber = bracketAdd.Right as Number;
                    var result = new Sub(new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()), bracketAdd.Left);

                    return _Simplify(result);
                }
            }
            // (2 - x) - 2
            // (x - 2) - 2
            else if (sub.Left is Sub && sub.Right is Number)
            {
                var bracketSub = sub.Left as Sub;
                var firstNumber = sub.Right as Number;

                if (bracketSub.Left is Number)
                {
                    var secondNumber = bracketSub.Left as Number;
                    var result = new Sub(new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()), bracketSub.Right);

                    return _Simplify(result);
                }
                if (bracketSub.Right is Number)
                {
                    var secondNumber = bracketSub.Right as Number;
                    var result = new Sub(bracketSub.Left, new Number((double)firstNumber.Calculate() + (double)secondNumber.Calculate()));

                    return _Simplify(result);
                }
            }
            // 2 - (2 - x)
            // 2 - (x - 2)
            else if (sub.Right is Sub && sub.Left is Number)
            {
                var bracketSub = sub.Right as Sub;
                var firstNumber = sub.Left as Number;

                if (bracketSub.Left is Number)
                {
                    var secondNumber = bracketSub.Left as Number;
                    var result = new Add(new Number((double)firstNumber.Calculate() - (double)secondNumber.Calculate()), bracketSub.Right);

                    return _Simplify(result);
                }
                if (bracketSub.Right is Number)
                {
                    var secondNumber = bracketSub.Right as Number;
                    var result = new Sub(new Number((double)firstNumber.Calculate() + (double)secondNumber.Calculate()), bracketSub.Left);

                    return _Simplify(result);
                }
            }

            return sub;
        }
Esempio n. 19
0
        public void SubSameVars1()
        {
            // x - x
            var sub = new Sub(new Variable("x"), new Variable("x"));
            var expected = new Number(0);

            SimpleTest(sub, expected);
        }
Esempio n. 20
0
        private IExpression SimplifyAdd(Add add)
        {
            // plus zero
            if (add.Left.Equals(zero))
                return add.Right;
            if (add.Right.Equals(zero))
                return add.Left;

            if (add.Left is Number && add.Right is Number)
                return new Number((double)add.Calculate());

            if (add.Left is UnaryMinus)
            {
                IExpression temp = add.Left;
                add.Left = add.Right;
                add.Right = temp;
            }
            if (add.Right is UnaryMinus)
            {
                var unMinus = add.Right as UnaryMinus;
                var sub = new Sub(add.Left, unMinus.Argument);

                return sub;
            }

            // 2 + (2 + x)
            // 2 + (x + 2)
            // (2 + x) + 2
            // (x + 2) + 2
            Add bracketAdd = null;
            Number firstNumber = null;
            if (add.Left is Add && add.Right is Number)
            {
                bracketAdd = add.Left as Add;
                firstNumber = add.Right as Number;
            }
            else if (add.Right is Add && add.Left is Number)
            {
                bracketAdd = add.Right as Add;
                firstNumber = add.Left as Number;
            }
            if (bracketAdd != null)
            {
                if (bracketAdd.Left is Number)
                {
                    var secondNumber = bracketAdd.Left as Number;
                    var result = new Add(bracketAdd.Right, new Number(firstNumber.Value + secondNumber.Value));

                    return _Simplify(result);
                }
                if (bracketAdd.Right is Number)
                {
                    var secondNumber = bracketAdd.Right as Number;
                    var result = new Add(bracketAdd.Left, new Number(firstNumber.Value + secondNumber.Value));

                    return _Simplify(result);
                }
            }

            // 2 + (2 - x)
            // 2 + (x - 2)
            // (2 - x) + 2
            // (x - 2) + 2
            Sub bracketSub = null;
            if (add.Left is Sub && add.Right is Number)
            {
                bracketSub = add.Left as Sub;
                firstNumber = add.Right as Number;
            }
            else if (add.Right is Sub && add.Left is Number)
            {
                bracketSub = add.Right as Sub;
                firstNumber = add.Left as Number;
            }
            if (bracketSub != null)
            {
                if (bracketSub.Left is Number)
                {
                    var secondNumber = bracketSub.Left as Number;
                    var result = new Sub(new Number(firstNumber.Value + secondNumber.Value), bracketSub.Right);

                    return _Simplify(result);
                }
                if (bracketSub.Right is Number)
                {
                    var secondNumber = bracketSub.Right as Number;
                    var result = new Add(new Number(firstNumber.Value - secondNumber.Value), bracketSub.Left);

                    return _Simplify(result);
                }
            }

            return add;
        }
Esempio n. 21
0
        public void SubTwoNumbers()
        {
            var sub = new Sub(new Number(3), new Number(2));
            var expected = new Number(1);

            SimpleTest(sub, expected);
        }
Esempio n. 22
0
        public void SubDiff_NumSubVar_SubNum()
        {
            var sub = new Sub(new Sub(new Number(2), new Variable("x")), new Number(2));
            var expected = new UnaryMinus(new Variable("x"));

            SimpleTest(sub, expected);
        }
Esempio n. 23
0
        public void SubDiff_NumAddVar_SubNum()
        {
            // (2 + x) - 2
            var sub = new Sub(new Add(new Number(2), new Variable("x")), new Number(2));
            var expected = new Variable("x");

            SimpleTest(sub, expected);
        }
Esempio n. 24
0
        public void SubDiffNumSub_VarAddNum_()
        {
            // 2 - (x + 2)
            var sub = new Sub(new Number(2), new Add(new Variable("x"), new Number(2)));
            var expected = new UnaryMinus(new Variable("x"));

            SimpleTest(sub, expected);
        }
Esempio n. 25
0
        public void SubDiffNumSub_NumSubVar_()
        {
            var sub = new Sub(new Number(2), new Sub(new Number(2), new Variable("x")));
            var expected = new Variable("x");

            SimpleTest(sub, expected);
        }
Esempio n. 26
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);
        }
Esempio n. 27
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);
        }
Esempio n. 28
0
        public void SubFirstZero()
        {
            var sub = new Sub(new Number(0), new Variable("x"));
            var expected = new UnaryMinus(new Variable("x"));

            SimpleTest(sub, expected);
        }
Esempio n. 29
0
        public void SubDiff_VarAddNum_SubNum()
        {
            // (x + 2) - 2
            var sub = new Sub(new Add(new Variable("x"), new Number(2)), new Number(2));
            var expected = new Variable("x");

            SimpleTest(sub, expected);
        }
Esempio n. 30
0
        public void SubDiff_VarSubNum_SubNum()
        {
            var sub = new Sub(new Sub(new Variable("x"), new Number(2)), new Number(2));
            var expected = new Sub(new Variable("x"), new Number(4));

            SimpleTest(sub, expected);
        }