@@ -126,17 +126,24 @@ def _diff_op_apply(d, x):
126126 return _diff_op_ify (d )._diff_op_apply (x )
127127
128128
129- def Sdop (* args ):
130- if len (args ) == 1 and isinstance (args [0 ],Symbol ): # Simple Pdop of order 1
131- return Pdop (args [0 ])
132- elif len (args ) == 2 and isinstance (args [0 ],list ) and isinstance (args [1 ],list ):
133- if len (args [0 ]) != len (args [1 ]):
134- raise ValueError ('In Sdop.__init__ coefficent list and Pdop list must be same length.' )
135- return sum ((a * b for a , b in zip (args [0 ], args [1 ])), DiffOpAdd .identity )
136- elif len (args ) == 1 and isinstance (args [0 ], (list , tuple )):
137- return sum ((a * b for a , b in args [0 ]), DiffOpAdd .identity )
138- else :
139- raise ValueError ('In Sdop.__init__ length of args must be 1 or 2 args = ' + str (args ))
129+ import abc
130+
131+ class Sdop (abc .ABC ):
132+ @classmethod
133+ def __subclasshook__ (cls , c ):
134+ return issubclass (c , DiffOpExpr )
135+
136+ def __new__ (cls , * args ):
137+ if len (args ) == 1 and isinstance (args [0 ],Symbol ): # Simple Pdop of order 1
138+ return Pdop (args [0 ])
139+ elif len (args ) == 2 and isinstance (args [0 ],list ) and isinstance (args [1 ],list ):
140+ if len (args [0 ]) != len (args [1 ]):
141+ raise ValueError ('In Sdop.__init__ coefficent list and Pdop list must be same length.' )
142+ return sum ((a * b for a , b in zip (args [0 ], args [1 ])), DiffOpAdd .identity )
143+ elif len (args ) == 1 and isinstance (args [0 ], (list , tuple )):
144+ return sum ((a * b for a , b in args [0 ]), DiffOpAdd .identity )
145+ else :
146+ raise ValueError ('In Sdop.__init__ length of args must be 1 or 2 args = ' + str (args ))
140147
141148
142149#################### Partial Derivative Operator Class #################
@@ -393,3 +400,19 @@ def _eval_simplify(self, *args, **kwargs):
393400
394401 def diff (self , * args , ** kwargs ):
395402 return super ().diff (* args , simplify = False , ** kwargs )
403+
404+
405+ def _as_terms (d ):
406+ d = d .expand ()
407+ if isinstance (d , DiffOpAdd ):
408+ for a in d .args :
409+ yield from _as_terms (a )
410+ elif isinstance (d , DiffOpMul ):
411+ coeff , pdiff = d .args
412+ yield (coeff , pdiff )
413+ elif isinstance (d , DiffOpPartial ):
414+ yield (S (1 ), d )
415+ elif isinstance (d , DiffOpZero ):
416+ pass
417+ else :
418+ raise NotImplementedError
0 commit comments