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


On Fri, Jan 2, 2015 at 7:03 PM, Amit Saha <[email protected]> wrote:

> 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
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CAKgW%3D6JQjB_1QO22wVYnPJf1i6Rrgn8SR-iuquEH03nHdguLZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to