3

...while still leaving it executable within the function.

The idea behind this is I want to create a summation function. Here's what I have so far:

def summation(n, bound, operation):
    if operation is None and upper != 'inf':
        g = 0
        for num in range(n, limit + 1):
            g += num
        return g
    else:
        pass

But summations are most often about infinite convergent series (for which I use 'inf'), with operations applied to each term. Ideally, I'd like to be able to write print summation(0, 'inf', 1 / factorial(n)) and get the mathematical constant e, or def W(x): return summation(1, 'inf', ((-n) ** (n - 1)) / factorial(n)) to get the Lambert W function.

All that comes to my mind is passing the appropriate arithmetic as a string and then using the exec statement to execute it. But I don't think that would accomplish the whole thing, and it's obviously dangerous to use exec with possibly user-entered code.

1

1 Answer 1

8

In Python, functions are first-class, which is to say they can be used and passed around like any other values, so you can take a function:

def example(f):
    return f(1) + f(2)

To run it, you could define a function like this:

def square(n):
    return n * n

And then pass it to your other function:

example(square)  # = square(1) + square(2) = 1 + 4 = 5

You can also use lambda to avoid having to define a new function if it's a simple expression:

example(lambda n: n * n)
Sign up to request clarification or add additional context in comments.

8 Comments

That takes care of functions, but how can I also incorporate arithmetic (as in the e example I gave)?
(That is, without defining another function for every single operation I want to use.)
@Lee: Just use a more complex lambda expression, e.g., summation(0, 'inf', lambda n: 1 / factorial(n)).
For infinite summations, is there a simple way to "calculate to available floating point accuracy" as the Python documentation claims e and π are, or should I ask a new question?
@Lee: You could continue on and on until the previous partial sum == the next partial sum. If that's not satisfactory, you should probably ask a new question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.