# Re: [sympy] defining a derivative

```iterating a little more,

unfortunately the "doit" fails at the next bend...```
```
this code

from sympy import *

u,B = symbols('u B')
e = B*u #here i use an expression instead of a function
s = Function('s')(e)
print(diff(s,u).doit())

is expected to give somethign like

B*Derivative(s(e),e)

instead it fails with the output

*rrossi@PCCB076:~/data/examples\$ python3 test_derivatives2.py
B*Subs(Derivative(s(_xi_1), _xi_1), (_xi_1,), (B*u,))Traceback (most recent
call last):  File "test_derivatives2.py", line 8, in <module>    aaa = {
Derivative(e,u):symbols('DeDu'), Derivative(s,e):symbols('DsDe') }  File
"/home/rrossi/.local/lib/python3.5/site-packages/sympy/core/function.py",
line 1070, in __new__    Can\'t calculate %s%s derivative wrt %s.''' %
(count, ordinal, v)))ValueError: Can't calculate 1st derivative wrt B*u.*i
guess what i should really do is to subfunction the class as your propose.
However i do not get how to make it to work. in my case "fdiff" is simply
not being called

class FunctionWithDerivative(Function):
nargs = 3

#first argument  --> the expression
#second argument --> the variable it depends on
#second argument --> the derivative
@classmethod
def eval(cls,expression,x,D):
print("--------- in eval -----------------. Looks like here it is
not creating the object ...")
cls.expression = expression
cls.x = x
cls.D = D
return cls.expression

def fdiff(self, argindex=1):
print("********** inside fdiff ****",self.args)

u = symbols('u')
e = Function('e')(u)
s = FunctionWithDerivative(symbols('e'), symbols('e'), symbols('DsDe') )

print(diff(s,e).doit())

returns

rrossi@PCCB076:~/data/examples\$ python3 test_derivatives2.py
--------- in eval -----------------. Looks like here it is not creating the
object ...
s =  s
diff(s,e).doit() =  0

*and what is the worst... fdiff is NOT being called...*regards
Riccardo

On Monday, October 17, 2016 at 8:26:04 AM UTC+2, Riccardo Rossi wrote:
>
> Dear Aaron,
>
> nothing on 0.7.6 (the default one on ubuntu 16.04)
>
> only writing here to "document" this behaviour
>
> regards
> Riccardo
>

--
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