首页 > 解决方案 > 如何在 sympy 中定义自定义函数?

问题描述

我会在 Sympy 中定义如下函数:

自定义函数示例

f = x 对于 x>=0 和 f = 1/x**2 对于 x<0

我尝试实现如下自定义函数:

class CustomFunction(Function):
    @classmethod
    def eval(cls, arg):
        arg = sympify(arg)
        if arg.is_number:
            if arg>=0:
                return ((x).subs(x,arg))
            else:
                return (1/x**2).subs(x, arg)

        if isinstance(arg, exp): 
            # What do I have to write here?
            pass

我会绘制这个函数,并像 sympy 中的任何其他函数一样使用它。拜托,你能帮我解决这个问题吗?

标签: functionsympy

解决方案


推荐阅读