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.Calculate(); Assert.AreEqual(expected, result); }
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); }
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.Calculate(); Assert.AreEqual(expected, result); }
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; }
public void CalculateTest1() { IExpression exp = new Add(new Number(-3), new Number(2)); Assert.AreEqual(-1.0, exp.Calculate()); }