plus/minus operator for python ±

前端 未结 7 645
遇见更好的自我
遇见更好的自我 2020-12-31 02:20

I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this.

Am I

7条回答
  •  臣服心动
    2020-12-31 03:07

    There is no such object in SymPy yet (as you saw, there is an issue suggesting one https://github.com/sympy/sympy/issues/5305). It's not hard to emulate, though. Just create a Symbol, and swap it out with +1 and -1 separately at the end. Like

    pm = Symbol(u'±') # The u is not needed in Python 3. I used ± just for pretty printing purposes. It has no special meaning.
    expr = 1 + pm*x # Or whatever
    # Do some stuff
    exprpos = expr.subs(pm, 1)
    exprneg = expr.subs(pm, -1)
    

    You could also just keep track of two equations from the start.

提交回复
热议问题