from sympy import *from sympy.abc import x, yimport sympy
class Double(Function): u"""A simple function that doubles the input."""
@classmethod def eval(cls, arg): return None
def fdiff(self, argindex = 1): return Integer(2) * self.args[0]
def __new__(cls, *args, **kwargs): return super(Double, cls).__new__(cls, *args, **kwargs)
def __init__(self, *args, **kwargs): super(Double, self).__init__()
d = Double('d')
d(2)TypeError: 'Double' object is not callable
class Double(Function): u"""A simple function that doubles the input."""
nargs = 1
@classmethod def eval(cls, arg): pass if arg.is_Number: return sympy.Mul(2, arg)
def fdiff(self, argindex = 1): return sympy.Integer(2)In [47]: Double(x)Out[47]: Double(x)
In [48]: Double(2*x)Out[48]: Double(2*x)
In [49]: Double(2*x).fdiff()Out[49]: 2
In [50]: Double(2*x).diff()Out[50]: 4