def p_typed_constants_lst(p):
    '''typed_constants_lst : constants_lst HYPHEN type typed_constants_lst
                           | constants_lst HYPHEN type'''
    if len(p) == 4:
        p[0] = [Term.constant(value, p[3]) for value in p[1]]
    elif len(p) == 5:
        p[0] = [Term.constant(value, p[3]) for value in p[1]] + p[4]
def p_constants_lst(p):
    '''constants_lst : constant constants_lst
                     | constant'''
    if len(p) == 2:
        p[0] = [Term.constant(p[1])]
    elif len(p) == 3:
        p[0] = [Term.constant(p[1])] + p[2]
Beispiel #3
0
    def test__str__(self):
        with self.subTest('Prints all terms sorted'):
            self.p.add_term(Term.constant(0))
            expected = '5*x^3 + 2*x^2 + 5'

            self.assertEqual(expected, str(self.p))

        with self.subTest('With a term that is: 0'):
            self.p.add_term(Term.constant(0))
            expected = '5*x^3 + 2*x^2 + 5'

            self.assertEqual(expected, str(self.p))
    def test_parse_term_with_valid_strings(self):
        with self.subTest('2'):
            term = Term.parse_term('2')
            expected_term = Term.constant(2)

            self.assertEqual(term, expected_term)

        with self.subTest('x'):
            term = Term.parse_term('x')
            expected_term = Term(coeff=1, variable='x', power=1)

            self.assertEqual(term, expected_term)

        with self.subTest('x^2'):
            term = Term.parse_term('x^2')
            expected_term = Term(coeff=1, variable='x', power=2)

            self.assertEqual(term, expected_term)

        with self.subTest('2*x'):
            term = Term.parse_term('2*x')
            expected_term = Term(coeff=2, variable='x', power=1)

            self.assertEqual(term, expected_term)

        with self.subTest('2*x^2'):
            term = Term.parse_term('2*x^2')
            expected_term = Term(coeff=2, variable='x', power=2)

            self.assertEqual(term, expected_term)
Beispiel #5
0
 def ground(self, subst):
     args = []
     for arg in self._args:
         if arg in subst:
             value = subst[arg]
             arg = Term.constant(value)
         args.append(arg)
     return Predicate(self._name, args)
    def __str__(self):
        sorted_terms = []

        for term_power in sorted(self._terms.keys(), reverse=True):
            if self._terms[term_power] != Term.constant(0):
                sorted_terms.append(str(self._terms[term_power]))

        return ' + '.join(sorted_terms)
    def test_is_property(self):
        with self.subTest('True'):
            term = Term.constant(7)
            self.assertTrue(term.is_constant)

        with self.subTest('False'):
            term = Term(coeff=5, power=1)
            self.assertFalse(term.is_constant)
    def test_derivative_works(self):
        with self.subTest('Constant'):
            terms = [Term(coeff=1, variable='x', power=0)]
            p = Polynomial(terms).derivative()
            self.assertEqual(str(p), str(Term.constant(0)))

        with self.subTest('Power 1'):
            terms = [Term(coeff=1, variable='x', power=1)]
            p = Polynomial(terms).derivative()
            self.assertEqual(str(p), str(Term.constant(1)))

        with self.subTest('Power 2'):
            terms = [Term(coeff=1, variable='x', power=2)]
            p = Polynomial(terms).derivative()
            expected = Term(coeff=2, variable='x', power=1)
            self.assertEqual(str(p), str(expected))

        with self.subTest('Two terms'):
            terms = [
                Term(coeff=4, variable='x', power=2),
                Term(coeff=3, variable='x', power=3)
            ]
            p = Polynomial(terms).derivative()
            derived_terms = [
                Term(coeff=8, variable='x', power=1),
                Term(coeff=9, variable='x', power=2)
            ]
            derivative_p = Polynomial(derived_terms)
            self.assertEqual(str(p), str(derivative_p))

        with self.subTest('Three terms'):
            terms = [
                Term(coeff=4, variable='x', power=2),
                Term(coeff=3, variable='x', power=3),
                Term(coeff=3, variable='x', power=0)
            ]
            derived_terms = [
                Term(coeff=8, variable='x', power=1),
                Term(coeff=9, variable='x', power=2),
                Term(coeff=0, variable='x', power=0)
            ]
            p = Polynomial(terms).derivative()
            derived_p = Polynomial(derived_terms)
            self.assertEqual(str(p), str(derived_p))
Beispiel #9
0
    def tests_add_term_with_power_already_in_the_terms(self):
        with self.subTest('Term power already in the terms'):
            term = Term.constant(4)

            expected = {
                0: self.term1 + term,
                2: self.term2,
                3: self.term3,
            }
            self.p.add_term(term)

            self.assertEqual(expected, self.p._terms)
Beispiel #10
0
 def ground(self, subst): #list of terms from params
     args = []
     #print("Lista de subst", subst)
     val = [i.value for i in subst]
     #typ = [i.type for i in subst]
     var = [i.name for i in subst]
     for arg in self._args:
         if arg in var:
             value = val[var.index(arg)]
             arg = Term.constant(value=value)
         args.append(arg)   
     #print (Predicate(self._name, args))      
     return Predicate(self._name, args)
    def test_derivative(self):
        with self.subTest('Derivative of constant is 0'):
            t = Term.constant(2)
            expected = Term.constant(0)

            self.assertEqual(expected, t.derivative())

        with self.subTest('Derivative of 2*x^1'):
            t = Term(coeff=2, variable='x', power=1)
            expected = Term.constant(2)

            self.assertEqual(expected, t.derivative())

        with self.subTest('Derivative of x^1'):
            t = Term(coeff=1, variable='x', power=1)
            expected = Term.constant(1)

            self.assertEqual(expected, t.derivative())

        with self.subTest('Derivative of 2*x^2'):
            t = Term(coeff=2, variable='x', power=2)
            expected = Term(coeff=4, variable='x', power=1)

            self.assertEqual(expected, t.derivative())
    def test_is_constant(self):
        with self.subTest('Term with coeff, variable and power is not'):
            t = Term(coeff=2, variable='x', power=3)

            self.assertFalse(t.is_constant)

        with self.subTest('Constant term is constant'):
            value = 2
            t = Term.constant(value)

            self.assertTrue(t.is_constant)

            self.assertEqual(value, t.coeff)
            self.assertIsNone(t.variable)
            self.assertEqual(0, t.power)
    def test__add__raises_exception(self):
        with self.subTest('Terms with different variables'):
            term1 = Term(coeff=1, variable='x', power=2)
            term2 = Term(coeff=1, variable='y', power=2)

            with self.assertRaises(AssertionError):
                term1 + term2

        with self.subTest('Terms with different powers'):
            term1 = Term.constant(1)
            term2 = Term(coeff=1, power=2)

            with self.assertRaises(AssertionError):
                term1 + term2

        with self.subTest('Terms with different variables and powers'):
            term1 = Term(coeff=1, variable='x', power=2)
            term2 = Term(coeff=1, variable='y', power=3)

            with self.assertRaises(AssertionError):
                term1 + term2
    def tests__str__working_correctly(self):
        with self.subTest('Coeff = 0: 0'):
            term = Term(coeff=0, power=2)
            self.assertEqual(str(term), '0')

        with self.subTest('Power = 0: 2'):
            term = Term.constant(2)
            self.assertEqual(str(term), '2')

        with self.subTest('Coeff = 1: x^2'):
            term = Term(coeff=1, power=2)
            self.assertEqual(str(term), 'x^2')

        with self.subTest('Power = 1: 2*x'):
            term = Term(coeff=2, power=1)
            self.assertEqual(str(term), '2*x')

        with self.subTest('Coeff = 1 and Power = 1: x'):
            term = Term(coeff=1, power=1)
            self.assertEqual(str(term), 'x')

        with self.subTest('All positive arguments: 2*x^2'):
            term = Term(coeff=2, power=2)
            self.assertEqual(str(term), '2*x^2')
    def test_derivative(self):
        with self.subTest('constant'):
            term = Term.constant(5)
            expected = Term.constant(0)

            self.assertEqual(expected, term.derived)
Beispiel #16
0
 def setUp(self):
     self.term1 = Term.constant(5)
     self.term2 = Term(coeff=2, power=2)
     self.term3 = Term(coeff=5, power=3)
     self.p = Polinom([self.term1, self.term2, self.term3])
 def test_constant(self):
     expected = Term.constant(5)
     self.assertEqual(expected, Term(coeff=5, variable='x', power=0))