2

I have a list of lambda functions. Lets say this one

l = [lambda x:x**i for i in range(n)]

For every n I need to be able to sum them so I'd have a function like this:

f = lambda x: x + x**2 + x**3 + ... + x**n

Is there any way?

Edit: I wasn't clear. I don't know anything about that functions.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
UpmostScarab
  • 960
  • 10
  • 29
  • Is that an infinite sum or finite? If finite, does it end at x**n? Does it have to be summed, or would a mathematical formula shortcut be acceptable? – Rory Daulton Jun 21 '16 at 12:32
  • @RoryDaulton It's a finite sum. I need to be able to have this sum for any n. Yes it stops on x**n. Edited, thanks. – UpmostScarab Jun 21 '16 at 12:33
  • Beware how [lambda closes over variables](http://stackoverflow.com/questions/10452770/python-lambdas-binding-to-local-values). Here `l` would be a list of lambdas that all share the same value of `i`. – Ilja Everilä Jun 21 '16 at 12:34
  • Note how `range(n) == [0, 1, 2, 3, .., n-1]`. So you'd get: x^0 + x^1 + x^2 + .. + x^(n-1), not x^1 + x^2 + .. + x^n. You might want to change that in your code if that is not what you intended to do. – advance512 Jun 21 '16 at 12:52

5 Answers5

3

Is this the solution you're looking for?

Python 3.x:

n = 5
g = lambda y: sum(  f(y) for f in (lambda x: x**i for i in range(n))  )
print(g(5)) # 781

Python 2.x:

n = 5
g = lambda y: sum(  f(y) for f in (lambda x: x**i for i in xrange(n))  )
print g(5) # 781
advance512
  • 1,327
  • 8
  • 20
  • Not sure I understand what you mean. Please explain? – advance512 Jun 21 '16 at 12:38
  • @MosesKoledoye this actually averts that "danger" :P. The lambdas are built in a generator expression and used up in another generator expression that feeds `sum()` and so a single lambda is produced, used up and so it goes. – Ilja Everilä Jun 21 '16 at 12:42
  • @IljaEverilä Humour me. Thanks for that. – Moses Koledoye Jun 21 '16 at 12:45
  • To be fair, @IljaEverilä, at first the code used range(n) instead of xrange(n), since I based it on a copy-pasta from the original question. :) That being said, I still don't see how it would've been an issue in the original version. – advance512 Jun 21 '16 at 12:46
  • @advance512 it wouldn't have been an issue. Btw using `range()` would've been correct, as the question is tagged [tag:python-3.x]. – Ilja Everilä Jun 21 '16 at 12:52
  • This solution only works in Python 2, both for use of `xrange` and for `print` as a statement rather than a function. – Tom Karzes Jun 21 '16 at 12:55
  • Split into Python 2.x and 3.x examples. – advance512 Jun 21 '16 at 13:02
3

If you mean a finite sum, up to x**n, use the mathematical shortcut

f = lambda x: (x**(n+1) - 1) / (x - 1) if x != 1 else n
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
2
f = lambda x,n: sum( x**i for i in range(n) )

print f(3,4)

>> 40
YOBA
  • 2,759
  • 1
  • 14
  • 29
  • 3
    Better to use a generator expression instead of a list – Moses Koledoye Jun 21 '16 at 12:38
  • This is a better solution if you don't want to create various anonymous lambdas, and I would use it if I cared about performance. My solution uses many lambdas, and as such might be more relevant for academic 'homework', etc. – advance512 Jun 21 '16 at 12:40
  • This doesn't work. The exponent is what varies in the sum, not x. – Tom Karzes Jun 21 '16 at 13:01
0

The simplest way to do this is to avoid creating the list of lambda functions, and to instead sum over a single function. Assuming you've defined x and n, you can do:

f = lambda x, i: x**i
sum(f(x, i) for i in range(n))

In your original example, you have actually created a closure, so your lambda functions do not do what you think they do. Instead, they are all identical, since they all use the final value of i in the closure. That is certainly not what you intended.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0
n=5  
xpower=[]  
for i in range(n):  
    xpower.insert(i, i+1)  
    print(i,xpower)  
f = lambda x, xpower: sum(x**xpower[i] for i in range(len(xpower)))  
print("Example with n=5, x=2:"," ", f(2,xpower))  
Sheikh
  • 1
  • 1