Gerard
Gerard

Reputation: 117

Function with a choice of parameters used

I have a two functions where I set parameters for mathematical function. I would like to create one if possible where I would type:

x, y = one_demo_fun(a=1,
                    b=2,
                    c=5,
                    power_of_the_first_x=2,
                    power_of_the_second_x=1))

or

x, y = one_demo_fun(y = 1 * (x ** 2) + (2 * x) + 5)

and get the same result.

I have two functions which I would like to make one, where user can type one of above:

def function_demo_parameters(  # type function settings ax^2+bx+c
        a=1,
        b=1,
        c=1,
        power_of_the_first_x=2,
        power_of_the_second_x=1):
    x = np.linspace(-2, 2, 100)
    y = a * (x ** power_of_the_first_x) + b * (x ** power_of_the_second_x) + c
    return x, y


def function_demo_equation(  # type function
        y=1 * (x ** 2) + (2 * x) + 5):
    x = np.linspace(-2, 2, 100)
    y = y
    return x, y

x, y = function_demo_parameters()
x_1, y_1 = function_demo_equation()

Is there some way to merge them or other way to look at the problem?

Upvotes: 0

Views: 58

Answers (1)

chepner
chepner

Reputation: 531265

You don't want an equation; you want the function that the equation implies.

function_demo_equation(lambda x: 1*(x**2) + 2*x + 5)

Upvotes: 2

Related Questions