'Approximates the definite integral by a sum.
method ... one of: left, right, midpoint, trapezoid
These are all basically the rectangle method [1], the only difference
is where the function value is taken in each interval to define the
rectangle.
[1] http://en.wikipedia.org/wiki/Rectangle_method
Examples
>>> from sympy import sin, sqrt
>>> from sympy.abc import x
>>> from sympy.integrals import Integral
>>> e = Integral(sin(x), (x, 3, 7))
>>> e
Integral(sin(x), (x, 3, 7))
For demonstration purposes, this interval will only be split into 2
regions, bounded by [3, 5] and [5, 7].
The left-hand rule uses function evaluations at the left of each
interval:
>>> e.as_sum(2, \'left\')
2*sin(5) + 2*sin(3)
The midpoint rule uses evaluations at the center of each interval:
>>> e.as_sum(2, \'midpoint\')
2*sin(4) + 2*sin(6)
The right-hand rule uses function evaluations at the right of each
interval:
>>> e.as_sum(2, \'right\')
2*sin(5) + 2*sin(7)
The trapezoid rule uses function evaluations on both sides of the
intervals. This is equivalent to taking the average of the left and
right hand rule results:
>>> e.as_sum(2, \'trapezoid\')
2*sin(5) + sin(3) + sin(7)
>>> (e.as_sum(2, \'left\') + e.as_sum(2, \'right\'))/2 == _
True
All but the trapexoid method may be used when dealing with a function
with a discontinuity. Here, the discontinuity at x = 0 can be avoided
by using the midpoint or right-hand method:
>>> e = Integral(1/sqrt(x), (x, 0, 1))
>>> e.as_sum(5).n(4)
1.730
>>> e.as_sum(10).n(4)
1.809
>>> e.doit().n(4)  # the actual value is 2
2.000
The left- or trapezoid method will encounter the discontinuity and
return oo:
>>> e.as_sum(5, \'left\')
oo
>>> e.as_sum(5, \'trapezoid\')
oo
See Also
Integral.doit : Perform the integration using any hints'

def as_sum(self, n, method='midpoint'):

    limits = self.limits
    if (len(limits) > 1):
        raise NotImplementedError('Multidimensional    midpoint    rule    not    implemented    yet')
    else:
        limit = limits[0]
        if (len(limit) != 3):
            raise ValueError('Expecting    a    definite    integral.')
    if (n <= 0):
        raise ValueError('n    must    be    >    0')
    if (n == oo):
        raise NotImplementedError('Infinite    summation    not    yet    implemented')
    (sym, lower_limit, upper_limit) = limit
    dx = ((upper_limit - lower_limit) / n)
    if (method == 'trapezoid'):
        l = self.function.limit(sym, lower_limit)
        r = self.function.limit(sym, upper_limit, '-')
        result = ((l + r) / 2)
        for i in range(1, n):
            x = (lower_limit + (i * dx))
            result += self.function.subs(sym, x)
        return (result * dx)
    elif (method not in ('left', 'right', 'midpoint')):
        raise NotImplementedError(('Unknown    method    %s' % method))
    result = 0
    for i in range(n):
        if (method == 'midpoint'):
            xi = ((lower_limit + (i * dx)) + (dx / 2))
        elif (method == 'left'):
            xi = (lower_limit + (i * dx))
            if (i == 0):
                result = self.function.limit(sym, lower_limit)
                continue
        elif (method == 'right'):
            xi = ((lower_limit + (i * dx)) + dx)
            if (i == n):
                result += self.function.limit(sym, upper_limit, '-')
                continue
        result += self.function.subs(sym, xi)
    return (result * dx)
