Noah19191
Noah19191

Reputation: 15

Why does my lambdify not working when add my own function

I created a function and make it usable in my Sympy expression like this:

  def Unit(x):
        if(x != 0):
            return 0
        else:
            return 1
  Unit = Function('Unit')
  x = Symbol('x')

My expression:

fx = x ** 2 + Unit(x)

But when I run:

lam_f = lambdify(x, fx, modules=["sympy"])
print(lam_f(-1))

It said that my Unit is not defined? Can anyone explain where i went wrong?

Upvotes: 0

Views: 211

Answers (1)

Seb
Seb

Reputation: 4585

Function('Unit') returns an undefined function with name Unit. See this question. If you want to use your previously defined function Unit, remove the call to Function():

def Unit(x):
    if(x != 0):
        return 0
    else:
        return 1

x = Symbol('x')
fx = x**2 + Unit(x)

lam_f = lambdify(x, fx, modules=['sympy'])
print(lam_f(-1)) # prints 1

Upvotes: 1

Related Questions