Redefine derivative of a IndexedBase

16 views
Skip to first unread message

Lorenzo Monacelli

unread,
Mar 3, 2020, 2:42:14 AM3/3/20
to sympy
Dear all,
I am manipulating an IndexedBase object, and I would like to modify its behavior when differentiated.

I thought to do something like this:
class custom(sy.IndexedBase):
    def __init__(self, *args, **kwargs):
        sy.IndexedBase.__init__(self, *args, **kwargs)
        
    def diff(self, *args):
        # Do some stuff
        pass

However, when I initialize the object:
b = custom("b")

It gives the error:
 object.__init__() takes no arguments

However, it works if I do:
b = sy.IndexedBase("b")


I have two questions:
1) How can I create an object derivated from IndexedBase?
2) Is my way of doing correct to redefine the behaviour of the derivative? (It must work if I derivate a whole expression that contains the object)

Thanks for your attention,
Bests,
Lorenzo

Kalevi Suominen

unread,
Mar 3, 2020, 4:31:19 AM3/3/20
to sympy
IndexedBase has no `__init__` method of its own. It inherits `object.__init__` that accepts no arguments and does nothing. Instead, IndexedBase has a `__new__` method that accepts arguments. The custom class will automatically inherit it. It is not necessary to define `__init__` at all. As to 2), I believe that your way is correct.

Kalevi Suominen

Lorenzo Monacelli

unread,
Mar 3, 2020, 8:12:51 AM3/3/20
to sympy
Thanks a lot for the help!
Reply all
Reply to author
Forward
0 new messages