Obtain a function expression that is product between two other functions expressions

I have a function that calculates x^2 and another that calculates x^3. I wanna obtain the expression of x**5 by multiplying the two other functions.

Here is what I've been trying to do:

def pol1(x):
    f=x**2
    return f
def pol2(x):
    f=x**3
    return f
def new(f,g,x):
    n=f*g
    return n

neo=new(pol1, pol2, 2)
print(neo)

Upvotes: 0

Views: 41

Answers (3)

Moses Koledoye
Moses Koledoye

Reputation: 78556

You're passing the x parameter unused. And function objects are not to be multiplied in python.

Here is what you want:

def new(f, g, x):
    n = f(x) * g(x)
    return n

neo = new(pol1, pol2, 2)
print(neo)
# 32

Upvotes: 0

jhwang
jhwang

Reputation: 43

You're passing in x to the new function, so why don't you just utilize it when assigning n:

def new(f,g,x):
    n=f(x)*g(x)
    return n

Upvotes: 1

Paulo Prestes
Paulo Prestes

Reputation: 494

Your code is just missing to pass the parameter x for the f and g in the new function.

def pol1(x):
    f=x**2
    return f
def pol2(x):
    f=x**3
    return f
def new(f,g,x):
    n=f(x)*g(x)
    return n

neo=new(pol1, pol2, 2)
print(neo)

Upvotes: 0

Related Questions