10

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

>>> x = symbols('x')
>>> x.description.set('Distance (m)')
>>> t = symbols('t')
>>> t.description.set('Time (s)')
>>> x.description()
'Distance (m)'
>>> t.description()
'Time (s)'

This would be useful because it would enable me to keep track of all my variables and know what physical quantities I am dealing with. Is something like this even remotely possible in sympy?

EDIT

I don't think this is a duplicate because the __doc__ attribute for symbols appears to be immutable. Consider the following:

>>> print(rhow.__doc__)

    Assumptions:
       commutative = True

    You can override the default assumptions in the constructor:

from sympy import symbols
A,B = symbols('A,B', commutative = False)
bool(A*B != B*A)
    True
bool(A*B*2 == 2*A*B) == True # multiplication by scalars is commutative
    True

>>> rhow.__doc__ = 'density of water'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-87-bfae705941d2> in <module>()
----> 1 rhow.__doc__ = 'density of water'

AttributeError: 'Symbol' object attribute '__doc__' is read-only

Indeed a .__doc__ attribute exists, but I cannot change it for my purposes. It is read only.

user32882
  • 5,094
  • 5
  • 43
  • 82
  • Possible duplicate of [Is there an equivalent to Mathematica's "usage" function?](https://stackoverflow.com/questions/43715293/is-there-an-equivalent-to-mathematicas-usage-function) – Wrzlprmft Sep 03 '17 at 10:09
  • You could use a seperate dictionary to keep track of that. Example `what_is = {}, what_is['x'] = 'Distance (m)'`. You need to remember to set your dictionary whenever you assign a symbol, but that's similar to the need to remember to assign the symbol a description. – laolux Sep 03 '17 at 11:56

1 Answers1

10

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__(cls, name, description=''):
        obj = Symbol.__new__(cls, 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
Serenity
  • 35,289
  • 20
  • 120
  • 115
  • This is a very cool suggestion but I actually want to declare multiple symbols with this kind of attribute. In my original question I am showing the `symbols()` and not the `Symbol` command. Could you please edit your question if it is possible to do something similar with the `symbols()` command? – user32882 Sep 07 '17 at 12:50
  • Is this at all possible? declaring multiple `MySymbol` types in one go? – user32882 Sep 15 '17 at 08:36
  • 1
    Make your own customfunction for multideclaring based on source code of 'symbols' function. – Serenity Sep 15 '17 at 10:40