test_rules_derivatives.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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))])
  42. def test_match_const_deriv_multiplication_multiple_constants(self):
  43. root = tree('der(2x * 3)')
  44. (l2, x), l3 = root[0]
  45. scope = Scope(root[0])
  46. self.assertEqualPos(match_const_deriv_multiplication(root),
  47. [P(root, const_deriv_multiplication, (scope, l2)),
  48. P(root, const_deriv_multiplication, (scope, l3))])
  49. def test_const_deriv_multiplication(self):
  50. root = tree('der(2x)')
  51. l2, x = root[0]
  52. args = Scope(root[0]), l2
  53. self.assertEqual(const_deriv_multiplication(root, args),
  54. l2 * der(x, x))
  55. def test_match_variable_power(self):
  56. root, x, l2 = tree('der(x ^ 2), x, 2')
  57. self.assertEqualPos(match_variable_power(root),
  58. [P(root, variable_root)])
  59. root = tree('der(2 ^ x)')
  60. self.assertEqualPos(match_variable_power(root),
  61. [P(root, variable_exponent)])
  62. def test_match_variable_power_chain_rule(self):
  63. root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
  64. self.assertEqualPos(match_variable_power(root),
  65. [P(root, chain_rule, (x3, variable_root, ()))])
  66. root = tree('der(2 ^ x ^ 3)')
  67. self.assertEqualPos(match_variable_power(root),
  68. [P(root, chain_rule, (x3, variable_exponent, ()))])
  69. # Below is not mathematically underivable, it's just not within the
  70. # scope of our program
  71. root, x = tree('der(x ^ x), x')
  72. self.assertEqualPos(match_variable_power(root), [])
  73. def test_variable_root(self):
  74. root = tree('der(x ^ 2)')
  75. x, n = root[0]
  76. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  77. def test_variable_root_chain_rule(self):
  78. pass
  79. def test_chain_rule(self):
  80. pass