Reputation: 133
How can I include a mathematical function as an argument of a function in Python?
In my particular case, I'm writing a Riemann sum calculator that would ideally look like:
def riemann_sum(func_x, minvalue, maxvalue, partitions)
...
return riemannSum
where func_x is some function of x so that I could find the riemann sum of any arbitrary function this:
func_x = x**2
minvalue = 1
maxvalue = 2
partitions = 100
a = riemann_sum(func_x,minvalue,maxvalue,partitions)
print(a)
However, I can't do the above procedure because x is undefined.
I can get the Riemann sum for particular functions of x by manually typing it in to a line of my function that looks like:
someList = [x**2 for x in someOtherList]
Here, the function is x**2, but I can't change it without physically going in and changing the function.
My only solution right now is to define a new Riemann sum function every time I want to find the definite integral of a new function, which works but I feel like there's a better way.
(Edit: My question is different from the Riemann sum question marked as a possible duplicate. Their question is about an implementation specifically for a Riemann sum. My question is about how to incorporate a math function as the argument of a function, and I happen to use Riemann sum as a particular example)
Upvotes: 2
Views: 205
Reputation:
I had a similar problem a few years back when I was coding something for my differential equations class. Here is an example of what worked for me:
func_x = "(x**2)+x+1"
paramList = [(1+(1/k),(1/k)-(1/(k+1))) for k in range(1,101)]
# paramList holds the tuple (x,changeInX) for the riemann sum
def riemann_sum(str_func_x,paramList):
theSum=0
for tup in paramList:
x=tup[0]
diff=tup[1]
theSum+=eval(str_func_x)*diff
return theSum
riemannSumValue = riemann_sum(func_x,paramList)
Make sure that param list is really from 1,1+1/100, . . ., 2 for the first index of the tuples in paramList. . . I think it is from 1.01 to 2 as it is now (I took some tylenol pm 20 minutes ago and now I am too tired to check myself.)
Upvotes: 0
Reputation: 69182
In Python functions are first class objects so you can, for example, pass functions as arguments to other functions. That is, the way you wrote your riemann_sum
function declaration is fine.
What doesn't work is your definition of func_x
, since you need to define func_x
as a function. For that you can either do:
func_x = lambda x: x**2
or, for a more general multiline (or single line) function
def func_x(x):
temp = x**2 # just to stretch this out to another line for demonstration
return temp
Then you can say something like:
def riemann_sum(func_x, minvalue, maxvalue, partitions):
# below just demos calling func_x, and is a bad way to do the sum
riemannSum = 0
step = 1.0*(maxvalue-minvalue)/partitions
value = minvalue
while value<maxvalue:
riemannSum == step*func_x(value) # here's where func_x is called
value += step
return riemannSum
That is, the main point here is that is demonstrates how to call func_x
within the riemann_sum
function. This allows you to evaluate func_x
at different x-values, as required to evaluate the sum.
Upvotes: 1
Reputation: 2542
# Replace with whatever your function is
def func_x(x):
return x**2
def riemann_sum(func_x(3), minvalue, maxvalue, partitions)
...
return riemannSum
This should do it. You can create the function elsewhere, then initialize it in your riemann_sum as an input.
Upvotes: 0