Tesla
Tesla

Reputation: 141

Using the lambda function for a function that depends on the input

As an input, I have a list of nonnegative whole numbers which are supposed to be the coefficients of a polynomial. But I also want to evaluate the polynomial for a certain number x.

For example:

If we have L=[2,3,1] as an input and x=42 we get 2x^2+3x+1=3655 What I want is for example:

>>>p=polynomial([2,3,1])
>>>p(O)
1 
>>>p(42)
>>>3655

I guess I have to make use of the lambda function somehow, and I do know how it works for two variables on a given function, but in this case the function depends on my input.

def polynomial(coef):
coef=coef[::-1]
for i in range(len(coef)):
    p=lambda x: coef[i]*x**i
    p+=p
return lambda x: p

This is of course absolute nonsense, as I cannot add up one lambda function to another, but this is what my approaching "intuition" is.

Some hints are much appreciated.

Upvotes: 3

Views: 813

Answers (4)

Right leg
Right leg

Reputation: 16720

The following lambda function evaluates a polynomial function, input as a coeff list of coefficients, in a given x:

from functools import reduce

lambda coeff, x: reduce(lambda a, b: a*x + b, coeff)

It seems that you want to generate these polynomial functions. You can still do it with this method:

def generate_polynomial(coeff):
    return lambda x: (lambda y: reduce(lambda a, b: a*y + b, coeff))(x)

>>> p = generate_polynomial([20,0,17])
>>> p(10)
2017

This is merely based on Horner's algorithm.

Besides, if you want to use exclusively lambda and no built-in functions, you can also emulate reduce with lambda functions. You might want to give a look at Python - Removing duplicates in list only by using filter and lambda and Removing duplicates using only lambda functions, showing how to get rid of the filter function.

Upvotes: 1

VPfB
VPfB

Reputation: 17267

Simple Python:

def make_poly(coefs):
    def poly(x):
        result = 0
        for c in coefs:
            result = result * x + c
        return result
    return poly

p = make_poly([2,3,1])

print(p(0))
print(p(42))

EDIT: code modified as suggested by Mark Dickinson in the comments

Upvotes: 2

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

The most obvious pythonic solution (using a closure - with a lambda or (preferably) a named inner function) has already been posted, but for the sake of completeness I'll add the other pythonic solution - the OO version using a custom callable class:

class Polynomial(object):
    def __init__(self, coef):
        self.coef = coef

    def __call__(self, x):
        n = len(self.coef) - 1
        return sum(c * x ** (n - i) for (i, c) in enumerate(self.coef))

p = Polynomial([2,3,1])
print p(0)
print p(42)

Upvotes: 4

Max Wasylow
Max Wasylow

Reputation: 11

You can do it using lambda:

def polynomial(coef):
  n = len(coef) - 1
  return lambda x : sum([c * x ** (n - i) for (i, c) in enumerate(coef)])

Lambda isn't necessary however, you can define another function inside the polynomial function like so:

def polynomial(coef):
  def f(x):
    n = len(coef) - 1
    return sum([c * x ** (n - i) for (i, c) in enumerate(coef)])
  return f

Edit: Previously input was tied to 3 coefficients

Upvotes: 1

Related Questions