Possible to add descriptions to symbols in sympy?

混江龙づ霸主 提交于 2019-12-01 05:14:34

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!