Esempio n. 1
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. 2
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. 3
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. 4
0
        public void CalculateTest()
        {
            IExpression exp = new Sub(new Number(1), new Number(2));

            Assert.AreEqual(-1.0, exp.Calculate());
        }
Esempio n. 5
0
        public void MultiOpMulSub()
        {
            // ({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 sub = new Sub(mul, vector2);

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

            Assert.AreEqual(expected, result);
        }