Here is my class for calculating a Riemann Sum. I did a cursory google search for a built-in function with these specifications but didn't find anything (I'm sure I just missed it or didn't look hard enough, but this is good practice).
Your input is welcome and appreciated. I'd like to not have to assign a lambda function to a variable but it works for me. I just worry that someone using this code may get confused.
from numpy import arange
class RiemannSum(object):
def __init__(self, index, upper, rectangles, func, endpoints='mid'):
"""
Constructor for base class for Riemann Sums.
Will be subclassed for left, right, or mid-points for rectangles.
:param index: Where to begin 'n' value. [n = index]
:param upper: Upper bound of sum. Max 'n' value. [Number above sigma notation]
:param rectangles: The number of rectangles to be used in the calculation.
:param func: pass series function here; suggest assigning lambda function to variable and passing here
:param endpoints: default to 'mid'. Valid values are 'mid', 'left', 'right' as strings
"""
self.total = 0
self.index = index
self.upper = upper
self.delta = (upper - index) / rectangles
self.function = func
if endpoints.lower() == 'left': # left endpoints
self.points = arange(self.index, self.upper, self.delta)
elif endpoints.lower() == 'right': # right endpoints
self.points = arange(self.index + self.delta, self.upper + self.delta, self.delta)
else: # mid endpoints, usually most accurate
diff = self.delta / 2
self.points = [i for i in arange((self.index + diff),
(self.upper + diff), self.delta)]
def sum(self):
outputs = [i for i in map(self.function, self.points)]
self.total = sum(outputs)
return self.delta * self.total
Here is an example of the code in use:
from math import sqrt
series = sqrt(x) - 2
num = RiemannSum(1,6,5,series,'mid').sum()
print(num)
1 Answer 1
If something is called "RiemannSum", then it should return a sum. If you want summation to be a separate method, a better term would be "partition"; you could do RiemannPartition.sum(), for instance.
Several other variable names are bit unintuitive to me (e.g. rectangles, index).
You can eliminate several lines by doing:
offset = {'left':0,'right':self.delta,'mid':self.delta/2}[self.endpoints.lower()]
self.points = arange(self.index+offset, self.upper+offset, self.delta)
General tip: if you're doing an if-then with several elif/else clauses, consider whether a dictionary would be simpler.
Explore related questions
See similar questions with these tags.
def square_it(x): return(x**2)
, then you can passsquare_it
as thefunc
parameter. Also, docs.scipy.org/doc/scipy/reference/tutorial/integrate.html \$\endgroup\$