Commit c5624470 authored by Taddeus Kroes's avatar Taddeus Kroes

Added rule to solve an indefinite integral.

parent 2ed4de97
from .utils import find_variables, first_sorted_variable, infinity, \ from .utils import find_variables, infinity, replace_variable, find_variable
replace_variable
from .logarithmic import ln from .logarithmic import ln
#from .goniometry import sin, cos #from .goniometry import sin, cos
from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_INT, \ from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_INT, \
...@@ -74,8 +73,28 @@ def solve_integral(integral, F): ...@@ -74,8 +73,28 @@ def solve_integral(integral, F):
lower = integral[2] lower = integral[2]
upper = infinity() if len(integral) < 4 else integral[3] upper = infinity() if len(integral) < 4 else integral[3]
# TODO: add notation [F(x)]_a^b # TODO: skip indefinite notation if anti-derivative has no impliciely
return replace_variable(F, x, lower) - replace_variable(F, x, upper) # identifiable parameter
return indef(F, lower, upper)
def match_solve_indef(node):
"""
[F(x)]_a^b -> F(b) - F(a)
"""
assert node.is_op(OP_INT_INDEF)
return [P(node, solve_indef)]
def solve_indef(root, args):
"""
[F(x)]_a^b -> F(b) - F(a)
"""
Fx, a, b = root
x = find_variable(Fx)
return replace_variable(Fx, x, b) - replace_variable(Fx, x, a)
def match_integrate_variable_power(node): def match_integrate_variable_power(node):
......
from ..node import ExpressionLeaf as L, OP_MUL, OP_DIV, INFINITY from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_MUL, OP_DIV, \
INFINITY
def greatest_common_divisor(a, b): def greatest_common_divisor(a, b):
...@@ -124,4 +125,4 @@ def replace_variable(f, x, replacement): ...@@ -124,4 +125,4 @@ def replace_variable(f, x, replacement):
children = map(lambda c: replace_variable(c, x, replacement), f) children = map(lambda c: replace_variable(c, x, replacement), f)
return N(f, *children) return N(f.op, *children)
from src.rules.integrals import choose_constant, \ from src.rules.integrals import choose_constant, match_solve_indef, \
match_integrate_variable_power, integrate_variable_root, \ solve_indef, match_integrate_variable_power, integrate_variable_root, \
integrate_variable_exponent integrate_variable_exponent
from src.rules.logarithmic import ln from src.rules.logarithmic import ln
#from .goniometry import sin, cos #from .goniometry import sin, cos
...@@ -25,6 +25,14 @@ class TestRulesIntegrals(RulesTestCase): ...@@ -25,6 +25,14 @@ class TestRulesIntegrals(RulesTestCase):
self.assertEqual(choose_constant(tree('int x ^ c')), a) self.assertEqual(choose_constant(tree('int x ^ c')), a)
self.assertEqual(choose_constant(tree('int a ^ c da')), b) self.assertEqual(choose_constant(tree('int a ^ c da')), b)
def test_match_solve_indef(self):
root = tree('[x ^ 2]_a^b')
self.assertEqualPos(match_solve_indef(root), [P(root, solve_indef)])
def test_solve_indef(self):
root, expect = tree('[x ^ 2]_a^b, b2 - a2')
self.assertEqual(solve_indef(root, ()), expect)
def test_match_integrate_variable_power(self): def test_match_integrate_variable_power(self):
for root in tree('int x ^ n, int x ^ n'): for root in tree('int x ^ n, int x ^ n'):
self.assertEqualPos(match_integrate_variable_power(root), self.assertEqualPos(match_integrate_variable_power(root),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment