test_rules_derivatives.py 4.2 KB

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