Esempio n. 1
0
    def test_Format(self):
        self.assertEqual("1+2", s(c(1), c(2)).fmt())

        self.assertEqual("8*x**5", p(c(-8), x(5)).abs_fmt())
        self.assertEqual("8*x**5", p(c(8), x(5)).abs_fmt())

        term1 = p(c(8), x(5))
        term2 = p(c(-2), x(2))
        self.assertEqual("8*x**5-2*x**2", s(term1, term2).fmt())
        self.assertEqual(term1, term1)
Esempio n. 2
0
    def test_Format(self):
        self.assertEqual("1+2", s(c(1), c(2)).fmt())

        self.assertEqual("8*x**5", p(c(-8), x(5)).abs_fmt())
        self.assertEqual("8*x**5", p(c(8), x(5)).abs_fmt())

        term1 = p(c(8), x(5))
        term2 = p(c(-2), x(2))
        self.assertEqual("8*x**5-2*x**2", s(term1, term2).fmt())
        self.assertEqual(term1, term1)
Esempio n. 3
0
    def test_Distributivity_of_Multiplication(self):
        def eq(expect, original):
            self.assertEqual(expect, original.apply_distributivity())

        # x * 2 + x * 3 <-- x * (2 + 3)
        eq(s(p(x(1), c(2)), p(x(1), c(3))), p(x(1), s(c(2), c(3))))

        # 5 * (8x^5 - 2x^2) = 5*(8*x^5) + 5*(-2*x^2)
        term1 = p(c(8), x(5))
        term2 = p(c(-2), x(2))

        eq(s(p(c(5), p(c(8), x(5))), p(c(5), p(c(-2), x(2)))),
           p(c(5), s(term1, term2)))
Esempio n. 4
0
    def test_Distributivity_of_Multiplication(self):
        def eq(expect, original):
            self.assertEqual(expect, original.apply_distributivity())

        # x * 2 + x * 3 <-- x * (2 + 3)
        eq(s(
            p(x(1), c(2)),
            p(x(1), c(3))),
           p(x(1), s(c(2), c(3))))

        # 5 * (8x^5 - 2x^2) = 5*(8*x^5) + 5*(-2*x^2)
        term1 = p(c(8), x(5))
        term2 = p(c(-2), x(2))

        eq(s(p(c(5), p(c(8), x(5))),
             p(c(5), p(c(-2), x(2)))),
           p(c(5), s(term1, term2)))
Esempio n. 5
0
    def test_Product_Associativity(self):
        def eq(factors, expr):
            self.assertEqual(set(factors), set(expr.apply_associativity().factors))
        # 1 * (2 * 3) --> 1 * 2 * 3
        eq({1, 2, 3}, p(1, p(2, 3)))
        # (1 * 2 ) * 3 --> 1 * 2 * 3

        eq({1, 2, 3}, p(p(1, 2), 3))
        # (1 * (2 * (3 * 4))) --> 1 * 2 * 3 * 4
        self.assertEqual(p(1, 2, 3, 4), p(1, p(2, p(3, 4))).apply_associativity())
Esempio n. 6
0
 def test_GivenExpressions(self):
     #"(x-1)*(x+1)"
     self.assertEqual("x**2-1", p(s(x(1), c(-1)), s(x(1), c(1))).simplify().fmt())
     self.assertEqual("x**2+2*x+1", simplify("(x+1)*(x+1)"))
     self.assertEqual("x**2+6*x", simplify("(x+3)*x*2-x*x"))
     self.assertEqual("x**3+x**2+x", simplify("x+x*x+x*x*x"))
     self.assertEqual("x**4+3*x+6", simplify("(2*x+3)*2-x+x*x*x*x"))
     self.assertEqual("0", simplify("x*x-(x-1)*(x+1)-1"))
     self.assertEqual( "-x", simplify("5-5-x"))
     self.assertEqual("-1", simplify("x*x*x-x*x*x-1"))
Esempio n. 7
0
 def test_GivenExpressions(self):
     #"(x-1)*(x+1)"
     self.assertEqual("x**2-1",
                      p(s(x(1), c(-1)), s(x(1), c(1))).simplify().fmt())
     self.assertEqual("x**2+2*x+1", simplify("(x+1)*(x+1)"))
     self.assertEqual("x**2+6*x", simplify("(x+3)*x*2-x*x"))
     self.assertEqual("x**3+x**2+x", simplify("x+x*x+x*x*x"))
     self.assertEqual("x**4+3*x+6", simplify("(2*x+3)*2-x+x*x*x*x"))
     self.assertEqual("0", simplify("x*x-(x-1)*(x+1)-1"))
     self.assertEqual("-x", simplify("5-5-x"))
     self.assertEqual("-1", simplify("x*x*x-x*x*x-1"))
Esempio n. 8
0
    def test_Product_Associativity(self):
        def eq(factors, expr):
            self.assertEqual(set(factors),
                             set(expr.apply_associativity().factors))

        # 1 * (2 * 3) --> 1 * 2 * 3
        eq({1, 2, 3}, p(1, p(2, 3)))
        # (1 * 2 ) * 3 --> 1 * 2 * 3

        eq({1, 2, 3}, p(p(1, 2), 3))
        # (1 * (2 * (3 * 4))) --> 1 * 2 * 3 * 4
        self.assertEqual(p(1, 2, 3, 4),
                         p(1, p(2, p(3, 4))).apply_associativity())
Esempio n. 9
0
 def test_sum_factors(self):
     c1, c2, c3 = c(1), c(2), c(3)
     u, v = s(c1, c2), s(c2, c3)
     self.assertSequenceEqual((u, v), tuple(p(u, v).sum_factors()))
     self.assertSequenceEqual((u, v), tuple(p(u, v, c(1)).sum_factors()))
Esempio n. 10
0
 def test_Format(self):
     self.assertEqual("8*x**5", p(c(-8), x(5)).abs_fmt())
     self.assertEqual("8*x**5", p(c(8), x(5)).abs_fmt())
Esempio n. 11
0
 def test_simplify_adds_equal_powers(self):
     self.assertEqual(s(p(c(2), x(1))), s(x(1), x(1)).simplify())
Esempio n. 12
0
 def test_PowerTerm(self):
     self.assertEqual(x(2), p(x(2)).power_factors())
     self.assertEqual(x(5), p(x(2), x(3)).power_factors())
     self.assertEqual(None, p(x(2), x(-2)).power_factors())
Esempio n. 13
0
 def test_ConstantTerm(self):
     self.assertEqual(c(5), p(c(5)).constant_factor())
     self.assertEqual(c(15), p(c(5), c(3)).constant_factor())
Esempio n. 14
0
 def test_Format(self):
     self.assertEqual("8*x**5", p(c(-8), x(5)).abs_fmt())
     self.assertEqual("8*x**5", p(c(8), x(5)).abs_fmt())
Esempio n. 15
0
 def test___factors(self):
     self.assertEqual(1, len(p([x(1)]).factors))
     self.assertSequenceEqual((x(1),), p(x(1)).factors)
Esempio n. 16
0
 def test_Negative(self):
     self.assertEqual(True, p(c(-1), c(1)).negative())
Esempio n. 17
0
 def test_ProductOfProducts(self):
     self.assertEqual(c(10), p(c(2), p(c(5))).simplify())
     self.assertEqual(c(10), p(p(c(5)), c(2)).simplify())
Esempio n. 18
0
 def test___factors(self):
     self.assertEqual(1, len(p([x(1)]).factors))
     self.assertSequenceEqual((x(1), ), p(x(1)).factors)
Esempio n. 19
0
 def test_ProductOfProducts(self):
     self.assertEqual(c(10), p(c(2), p(c(5))).simplify())
     self.assertEqual(c(10), p(p(c(5)), c(2)).simplify())
Esempio n. 20
0
 def test_PowerTerm(self):
     self.assertEqual(x(2), p(x(2)).power_factors())
     self.assertEqual(x(5), p(x(2), x(3)).power_factors())
     self.assertEqual(None, p(x(2), x(-2)).power_factors())
Esempio n. 21
0
 def test_sum_factors(self):
     c1, c2, c3 = c(1), c(2), c(3)
     u, v = s(c1, c2), s(c2, c3)
     self.assertSequenceEqual((u, v), tuple(p(u, v).sum_factors()))
     self.assertSequenceEqual((u, v), tuple(p(u, v, c(1)).sum_factors()))
Esempio n. 22
0
 def test_simplify_adds_equal_powers(self):
     self.assertEqual(s(p(c(2), x(1))), s(x(1), x(1)).simplify())
Esempio n. 23
0
 def test_Negative(self):
     self.assertEqual(True, p(c(-1), c(1)).negative())
Esempio n. 24
0
 def test_ConstantTerm(self):
     self.assertEqual(c(5), p(c(5)).constant_factor())
     self.assertEqual(c(15), p(c(5), c(3)).constant_factor())