Possible to add descriptions to symbols in sympy?

后端 未结 1 444
死守一世寂寞
死守一世寂寞 2021-01-12 00:32

I seek a functionality in sympy that can provide descriptions to symbols when needed. This would be something along the lines of

>>> x = symbols(\'         


        
相关标签:
1条回答
  • 2021-01-12 00:55

    You may inherit Symbol class and add your own custom property like here:

    from sympy import Symbol, simplify
    
    # my custom class with description attribute
    class MySymbol(Symbol):
        def __new__(self, name, description=''):
            obj = Symbol.__new__(self, name)
            obj.description = description
            return obj
    
    # make new objects with description
    x = MySymbol('x')
    x.description = 'Distance (m)'
    t = MySymbol('t', 'Time (s)')
    print( x.description, t.description)
    
    # test
    expr = (x*t + 2*t)/t
    print (simplify(expr))
    

    Output:

    Distance (m) Time (s)
    x + 2
    
    0 讨论(0)
提交回复
热议问题