defining a new function

29 views
Skip to first unread message

Abhishek K Das

unread,
May 17, 2014, 12:25:10 AM5/17/14
to sy...@googlegroups.com
class my_function(Function): nargs = 1 def fdiff(self, argindex = 1): return cos(self.args[0]) @classmethod def eval(cls, arg): arg = sympify(arg) if arg == 0: return sympify(0)

On the website it is given this is how a function should be defined . Now my questions are why did you set argindex = 1 , where is it
being used . And as for @classmethod it just signifies that the value of this function at 0 is 0 , am I right ? Why do we need to declare
eval as @classmethod and does specifying the derivative define that function ( I know that is true mathematically , but how does it work for sympy ) ?

Aaron Meurer

unread,
May 17, 2014, 9:49:07 PM5/17/14
to sy...@googlegroups.com
@classmethod lets you call the method as my_function.eval() (as
opposed to my_function().eval). This is required for eval() because
that is how the superclass uses it to instantiate the class. Think of
it as a special SymPy version of __new__.

The derivative defines how the function behaves with respect to things
that use the derivative, including series expansion and integration.
It is not required (none of the methods are required), but if you want
those things to work, you need to define it. Otherwise, it will just
come out as an unevaluated Derivative when you take the derivative.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/4332b4c0-c3f6-4d90-807b-42b6ecd9cdd0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages