Why would I want to subclass Function for defining my functions?

31 views
Skip to first unread message

Amit Saha

unread,
Jan 2, 2015, 9:04:12 PM1/2/15
to sy...@googlegroups.com
Hi all,

Let's say I have a function defined as follows:

>>> def f(x):
return x**2

I can differentitate this function:

>>> from sympy import Derivative
>>> Derivative(f(x), x)
Derivative(x**2, x)
>>> Derivative(f(x), x).doit()
2*x
>>> Derivative(f(x), x, x).doit()
2


..find the limit:

>>> from sympy import limit
>>> limit(fx, x, 0)
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
limit(fx, x, 0)
NameError: name 'fx' is not defined
>>> limit(f(x), x, 0)
0


Is there any specific reason I would want to subclass Function and
then override the relevant methods unless I am expecting that my code
would be expected to have the .diff() method or any other methods for
example?

Thanks,
Amit.


--
http://echorand.me

Aaron Meurer

unread,
Jan 2, 2015, 10:03:41 PM1/2/15
to sy...@googlegroups.com
If you always want to evaluate to a given expression, then there is no point in using a Function subclass over a Python function. 

In your example, f(x) is nothing more than a shortcut for x**2. fx = f(x) is semantically equivalent to fx = x**2. 

The point of Function is that it can be unevaluated. This is useful because sometimes you don't have an evaluation (like sin(x) or sin(1) should both stay as-is), and also because it lets you simplify expressions so that they are in terms of a single function, rather than a large expression (Python functions always evaluate eagerly, so there is no way to use f(x) in an expression with your example; it will always be replaced with x**2 as soon as it is evaluated). 

Aaron Meurer




--
http://echorand.me

--
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/CANODV3nfsVsDQaq74hWd1RmGgzdq1X%3DY0FM6q6C9qtd0AwhmLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Amit Saha

unread,
Jan 3, 2015, 3:41:41 AM1/3/15
to sy...@googlegroups.com
On Sat, Jan 3, 2015 at 1:03 PM, Aaron Meurer <asme...@gmail.com> wrote:
> If you always want to evaluate to a given expression, then there is no point
> in using a Function subclass over a Python function.
>
> In your example, f(x) is nothing more than a shortcut for x**2. fx = f(x) is
> semantically equivalent to fx = x**2.
>
> The point of Function is that it can be unevaluated. This is useful because
> sometimes you don't have an evaluation (like sin(x) or sin(1) should both
> stay as-is), and also because it lets you simplify expressions so that they
> are in terms of a single function, rather than a large expression (Python
> functions always evaluate eagerly, so there is no way to use f(x) in an
> expression with your example; it will always be replaced with x**2 as soon
> as it is evaluated).

Thanks a lot for the explanation.
Reply all
Reply to author
Forward
0 new messages