Package sympycore :: Package calculus :: Module integration
[hide private]
[frames] | no frames]

Source Code for Module sympycore.calculus.integration

 1  """ Provides the implementation of integration methods.
 
 2  """ 
 3  
 
 4  __docformat__ = "restructuredtext" 
 5  __all__ = ['integrate'] 
 6  
 
 7  from .algebra import Calculus, one 
 8  from ..utils import NUMBER, SYMBOL, TERMS, FACTORS 
 9  
 
10  Symbol = Calculus.Symbol 
11  Convert = Calculus.convert 
12  
 
13 -def unknown(expr):
14 raise NotImplementedError("don't know how to integrate %s" % (expr,))
15
16 -def integrate_indefinite(expr, x):
17 head, data = expr.pair 18 cls = type(expr) 19 if head is NUMBER or x not in expr._get_symbols_data(): 20 return expr*cls.Symbol(x) 21 elif head is SYMBOL and expr.data == x: 22 return expr**2 / 2 23 elif head is FACTORS: 24 product = one 25 have_x = False 26 for base, e in data.iteritems(): 27 # We don't know how to do exponentials yet 28 if type(e) is cls and x in expr._get_symbols_data(): 29 unknown(expr) 30 if base.head is SYMBOL and base.data == x: 31 if have_x: 32 unknown(expr) 33 e1 = e+1 34 product *= base**e1 / e1 35 have_x = True 36 # Cases like (x+y)*x could still be handled by expanding, 37 # but this may cause infinite recursion if implemented 38 # directly here 39 elif x in base._get_symbols_data(): 40 unknown(expr) 41 else: 42 product *= base**e 43 return product 44 elif head is TERMS: 45 return expr.Add(*(coef*integrate_indefinite(term, x) \ 46 for term, coef in data.iteritems())) 47 unknown(expr)
48
49 -def integrate_definite(expr, x, a, b):
50 head, data = expr.pair 51 if head is NUMBER or x not in expr._get_symbols_data(): 52 return expr*(b-a) 53 elif head is SYMBOL and data == x: 54 return (b**2 - a**2) / 2 55 elif head is FACTORS: 56 product = one 57 have_x = False 58 cls = type(expr) 59 for base, e in data.iteritems(): 60 # We don't know how to do exponentials yet 61 if type(e) is cls and x in expr._get_symbols_data(): 62 unknown(expr) 63 if base.pair == (SYMBOL, x): 64 if have_x: 65 unknown(expr) 66 e1 = e+1 67 product *= (b**e1 - a**e1) / e1 68 have_x = True 69 # Cases like (x+y)*x could still be handled by expanding, 70 # but this may cause infinite recursion if implemented 71 # directly here 72 elif x in base._get_symbols_data(): 73 unknown(expr) 74 else: 75 product *= cls(FACTORS, {base:e}) 76 return product 77 elif head is TERMS: 78 return expr.Add(*(coef*integrate_definite(term, x, a, b) \ 79 for term, coef in data.iteritems())) 80 unknown(expr)
81
82 -def integrate(expr, x):
83 type_ = type 84 Calculus_ = Calculus 85 type_x = type_(x) 86 if type_(expr) is not Calculus_: 87 expr = Convert(expr) 88 if type_x is tuple: 89 v, a, b = x 90 if type_(v) is not Calculus_: v = Symbol(v) 91 if type_(a) is not Calculus_: a = Convert(a) 92 if type_(b) is not Calculus_: b = Convert(b) 93 return integrate_definite(expr, v.data, a, b) 94 else: 95 if type_x is not Calculus_: 96 x = Symbol(x) 97 return integrate_indefinite(expr, x.data)
98