On Sun, 7 Feb 2021 at 00:07, David Bailey <d...@dbailey.co.uk> wrote:
>
> Dear Group,
>
> While thinking about Jonathon's question, I came across this oddity:
>
> x=symbols('x')
>
> f=symbols('f',cls=Function)
>
> diff(f,x)
>
> 1
>
> Why 1? I think I would have expected it to generate a TypeError, just like 
> f+x does.

The problem is that an undefined function like f is actually a class.
An object like f(x) is an instance of that class. When you do diff(f,
x) that calls through to f.diff(x). The f class has a diff method but
it is supposed to be called with an instance. Instead if you just call
f.diff(x) then the diff method is called with x as the instance so
what you get is equivalent to x.diff() which is treated as x.diff(x)
which gives 1.

It's like this:

In [10]: class MyExpr(Expr):
    ...:     def diff(self, *symbols):
    ...:         print('self =', self)
    ...:         print('symbols =', symbols)
    ...:

In [11]: diff(MyExpr, x)
self = x
symbols = ()

In [12]: diff(MyExpr(y), x)
self = MyExpr(y)
symbols = (x,)

Ideally we should change it so that an undefined function like f is a
Basic instance rather than a Basic subclass.


Oscar

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxTZUVfp6La93BuCsD-04PYcSeCS-so%3DzrBcx5g_jru04Q%40mail.gmail.com.

Reply via email to