def test_delta_derivative(self): exp, x, d = tree('x ^ 2, x, d') self.assertEqual(tree('d/dx x ^ 2'), der(exp, x)) self.assertEqual(tree('d / dx x ^ 2'), der(exp, x)) self.assertEqual(tree('d/dx x ^ 2 + x'), der(exp, x) + x) self.assertEqual(tree('d/dx (x ^ 2 + x)'), der(exp + x, x)) self.assertEqual(tree('d/d'), d / d)
def test_derivative(self): x = tree('x') self.assertEqual(tree('[x]\''), der(x)) self.assertEqual(tree('x\''), der(x)) self.assertEqual(tree('[x]\'\''), der(der(x))) self.assertEqual(tree('(x)\'\''), der(der(x))) self.assertEqual(tree('x\'\''), der(der(x)))
def test_product_rule(self): root = tree('(x ^ 2 * x)\'') x2, x = f = root[0] self.assertEqual(product_rule(root, (Scope(f), x2)), der(x2) * x + x2 * der(x)) self.assertEqual(product_rule(root, (Scope(f), x)), der(x) * x2 + x * der(x2)) root = tree('(x ^ 2 * x * x ^ 3)\'') (x2, x), x3 = f = root[0] self.assertEqual(product_rule(root, (Scope(f), x2)), der(x2) * (x * x3) + x2 * der(x * x3)) self.assertEqual(product_rule(root, (Scope(f), x)), der(x) * (x2 * x3) + x * der(x2 * x3)) self.assertEqual(product_rule(root, (Scope(f), x3)), der(x3) * (x2 * x) + x3 * der(x2 * x))
def test_const_deriv_multiplication(self): root = tree('d/dx 2x') l2, x = root[0] args = Scope(root[0]), l2, x self.assertEqual(const_deriv_multiplication(root, args), l2 * der(x, x)) self.assertRewrite(["[2x]'", "2[x]'", '2 * 1', '2'])
def test_sum_rule(self): root = tree('(x ^ 2 + x)\'') x2, x = f = root[0] self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(x)) self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2)) root = tree('(x ^ 2 + 3 + x)\'') (x2, l3), x = f = root[0] self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(l3 + x)) self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2 + l3))
def test_der(self): x2, x, y = tree('x ^ 2, x, y') self.assertEqual(der(x2), tree('[x ^ 2]\'')) self.assertEqual(der(x2, x), tree('d/dx x ^ 2')) self.assertEqual(der(x2, y), tree('d/dy x ^ 2'))
def test_quotient_rule(self): root = tree('(x ^ 2 / x)\'') f, g = root[0] self.assertEqual(quotient_rule(root, ()), (der(f) * g - f * der(g)) / g ** 2)
def test_tangens(self): root, x = tree('d/dx tan(x), x') self.assertEqual(tangens(root, ()), der(sin(x) / cos(x), x)) root = tree('tan(x)\'') self.assertEqual(tangens(root, ()), der(sin(x) / cos(x)))
def test_chain_rule(self): root = tree('(2 ^ x ^ 3)\'') l2, x3 = root[0] x, l3 = x3 self.assertEqual(chain_rule(root, (x3, variable_exponent, ())), l2 ** x3 * ln(l2) * der(x3))