test_rules_derivatives.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from src.rules.derivatives import der, get_derivation_variable, \
  2. match_zero_derivative, match_one_derivative, one_derivative, \
  3. zero_derivative, match_variable_power, variable_root, \
  4. match_const_deriv_multiplication, const_deriv_multiplication, \
  5. chain_rule
  6. from src.node import Scope
  7. from src.possibilities import Possibility as P
  8. from tests.rulestestcase import RulesTestCase, tree
  9. class TestRulesDerivatives(RulesTestCase):
  10. def test_get_derivation_variable(self):
  11. xy, x, l1 = tree('der(xy, x), der(x), der(1)')
  12. self.assertEqual(get_derivation_variable(xy), 'x')
  13. self.assertEqual(get_derivation_variable(x), 'x')
  14. self.assertIsNone(get_derivation_variable(l1))
  15. self.assertRaises(ValueError, tree, 'der(xy)')
  16. def test_match_zero_derivative(self):
  17. root = tree('der(x, y)')
  18. self.assertEqualPos(match_zero_derivative(root),
  19. [P(root, zero_derivative)])
  20. root = tree('der(2)')
  21. self.assertEqualPos(match_zero_derivative(root),
  22. [P(root, zero_derivative)])
  23. def test_zero_derivative(self):
  24. root = tree('der(1)')
  25. self.assertEqual(zero_derivative(root, ()), 0)
  26. def test_match_one_derivative(self):
  27. root = tree('der(x)')
  28. self.assertEqualPos(match_one_derivative(root),
  29. [P(root, one_derivative)])
  30. root = tree('der(x, x)')
  31. self.assertEqualPos(match_one_derivative(root),
  32. [P(root, one_derivative)])
  33. def test_one_derivative(self):
  34. root = tree('der(x)')
  35. self.assertEqual(one_derivative(root, ()), 1)
  36. def test_match_const_deriv_multiplication(self):
  37. root = tree('der(2x)')
  38. l2, x = root[0]
  39. self.assertEqualPos(match_const_deriv_multiplication(root),
  40. [P(root, const_deriv_multiplication, (Scope(root[0]), l2))])
  41. def test_match_const_deriv_multiplication_multiple_constants(self):
  42. root = tree('der(2x * 3)')
  43. (l2, x), l3 = root[0]
  44. scope = Scope(root[0])
  45. self.assertEqualPos(match_const_deriv_multiplication(root),
  46. [P(root, const_deriv_multiplication, (scope, l2)),
  47. P(root, const_deriv_multiplication, (scope, l3))])
  48. def test_const_deriv_multiplication(self):
  49. root = tree('der(2x)')
  50. l2, x = root[0]
  51. args = Scope(root[0]), l2
  52. self.assertEqual(const_deriv_multiplication(root, args),
  53. l2 * der(x, x))
  54. def test_match_variable_power(self):
  55. root, x, l2 = tree('der(x ^ 2), x, 2')
  56. self.assertEqualPos(match_variable_power(root),
  57. [P(root, variable_root)])
  58. def test_match_variable_power_chain_rule(self):
  59. root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
  60. self.assertEqualPos(match_variable_power(root),
  61. [P(root, chain_rule, (x3, variable_root, ()))])
  62. # Below is not mathematically underivable, it's just not within the
  63. # scope of our program
  64. root, x = tree('der(x ^ x), x')
  65. self.assertEqualPos(match_variable_power(root), [])
  66. def test_variable_root(self):
  67. root = tree('der(x ^ 2)')
  68. x, n = root[0]
  69. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  70. def test_variable_root_chain_rule(self):
  71. pass
  72. def test_chain_rule(self):
  73. pass