public static Expr Create(BigInteger value) { if (value == 0) return new Zero(); if (value == 1) return new One(); if (value < 0) NegativeOp.Create(Create(-value)); return new Literal(value); }
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 CreateNegativeZeroTest() { var zero = new Zero(); var negativezero = NegativeOp.Create(0); Assert.AreEqual(zero, negativezero); }
public void Test4() { var d = ExprParser.ParseOrThrow("2*4/3-4"); var dsimped = d.Simplify(); Assert.AreEqual(dsimped, NegativeOp.Create(DivideOp.Create(4, 3))); }
public void AddToNegativeLiteralTest() { var actual1 = SubtractOp.Create(new Identifier("x"), 1); var actual2 = SubtractOp.Create(1, new Identifier("x")); Assert.AreEqual(X + new NegativeOne(), actual1); Assert.AreEqual(One + NegativeOp.Create(X), actual2); }
public static Expr ToExpr(this BigInteger i) { if (i < 0) { return(NegativeOp.Create(Literal.Create(BigInteger.Abs(i)))); } return(Literal.Create(i)); }
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 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); }
public void SqrtN16() { var NegSixteen = NegativeOp.Create(Literal.Create(16)); var sqrt = NegSixteen.Sqrt(); var four = Literal.Create(4); var sqrtOfNegOne = SqrtOp.Create(-1).Simplify(); var expected = four * sqrtOfNegOne; Assert.AreEqual(sqrt, expected); }
public void NegateDivideTest() { const string input = "- (1/3)"; var actual = NegativeOp.Create( DivideOp.Create(new One(), Literal.Create(3)) ).Simplify(); var expected = ExprParser.ParseOrThrow(input); Assert.AreEqual(expected, actual); }
public void NegativeBeforeMultiplyTest() { const string input = "-12*3"; var actual = MultiplyOp.Create( NegativeOp.Create(Literal.Create(12)), Literal.Create(3) ).Simplify(); var expected = ExprParser.ParseOrThrow(input); Assert.AreEqual(expected.Simplify(), actual); }
public void NegativeBeforeSubtractTest() { const string input = "-12 - 3"; var actual = SubtractOp.Create( NegativeOp.Create(Literal.Create(12)), Literal.Create(3) ).Simplify(); var expected = ExprParser.ParseOrThrow(input).Simplify(); Assert.AreEqual(expected, 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 FunctionMethodNegateParamTest() { const string input = "foo(-3, x)()"; var actual = Call.Create( Call.Create( new Identifier("foo"), NegativeOp.Create(Literal.Create(3)), new Identifier("x") )).Simplify(); var expected = ExprParser.ParseOrThrow(input); Assert.AreEqual(expected, actual); }
public static Expr Create(BigInteger value) { if (value == 0) { return(new Zero()); } if (value == 1) { return(new One()); } if (value < 0) { NegativeOp.Create(Create(-value)); } return(new Literal(value)); }
public static Expr Create(decimal value) { if (value == 0) { return(new Zero()); } if (value == 1) { return(new One()); } if (value < 0) { NegativeOp.Create(Create(-value)); } return(new Dec(value)); }
public void InvertDivisionOpTest() { var a = new Identifier("a"); var b = new Identifier("b"); var actual1 = new One() / a; Assert.AreEqual(actual1.Invert(), a); var actual2 = DivideOp.Create(-new One(), a); Assert.AreEqual(actual2.Invert(), -a); var actual3 = DivideOp.Create(a, b); Assert.AreEqual(actual3.Invert(), DivideOp.Create(b, a)); var actual4 = DivideOp.Create(NegativeOp.Create(a), b); Assert.AreEqual(actual4.Invert(), DivideOp.Create(-b, a)); }
public void NegativeFractionRootOfFive() { var d = ExprParser.ParseOrThrow("-(1/2)^5").Simplify(); Assert.AreEqual(d, NegativeOp.Create(DivideOp.Create(1, 32))); }