【问题标题】:How define non-continious function in sympy?如何在 sympy 中定义非连续函数?
【发布时间】:2020-04-20 20:22:28
【问题描述】:

我正在寻找如何在 sympy 中定义表达式,例如 f(x)=x 对于 x0 我尝试了类似的理解列表但没有成功 谢谢

【问题讨论】:

    标签: python math sympy symbolic-math


    【解决方案1】:

    您所描述的称为分段函数:

    In [1]: p = Piecewise((x, x<0), (x**2 + 2, x>0))                                                                                               
    
    In [2]: p                                                                                                                                      
    Out[2]: 
    ⎧  x     for x < 0
    ⎪                 
    ⎨ 2               
    ⎪x  + 2  for x > 0
    ⎩  
    

    https://docs.sympy.org/latest/modules/functions/elementary.html#sympy-functions-elementary-piecewise

    【讨论】:

      【解决方案2】:

      欢迎来到 StackOverflow!

      您正在寻找分段函数。这是一个快速的 sn-p,可帮助您快速掌握。

      from sympy import Piecewise # import the Piecewise function
      p = Piecewise((x, x < 0), (x**2+2, x > 0)) # the general form is: Piecewise((branch, condition), (branch, condition), ...)
      p # display the equation
      

      这是输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        • 2012-01-15
        • 2014-09-10
        • 1970-01-01
        • 2017-03-04
        • 1970-01-01
        • 2020-02-15
        相关资源
        最近更新 更多