0

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?

dmmfll
  • 2,666
  • 2
  • 35
  • 41
Noah19191
  • 15
  • 6

1 Answers1

1

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
Seb
  • 4,422
  • 14
  • 23