Dear Aaron,
first of all thank you for answering.
before i start subclassing, let me ask if i can do something easier: can i
use "subs"?
right now i am failing, but there might be some obvious error in what i
do...
in any case i also tried (and failed) with your suggestion, surely due to
my lack of understanding of the sympy internals.
here goes my code
regards
Riccardo
from sympy import *
u = symbols('u')
der = symbols('der')
e = Function('e')(u)
s = Function('s')(e)
print(s)
Derivative(e,u)
print(diff(e,u))
print(diff(s,e))
print(diff(s,u)) #here i would like "der" to be replaced within the chain
rule
#list of replacements i would like to happen
aaa = { Derivative(e,u):symbols('DeDu'), Derivative(s,e):symbols('DsDe') }
print(diff(s,u).subs(aaa)) #here Derivative(s,e) is not substituted, i
would like to get "se*eu" as a result
#the reason for the failure to substitute is here ... how can i do this
correctly?
for iter in diff(s,u).atoms(Derivative):
print("iter = ",iter)
print(iter == Derivative(s,e))
if iter in aaa:
print ("found")
else:
print("not found")
#####################################################
### TRYING THE SUGGESTION
#####################################################
class FunctionWithDerivative(Function):
def __init__(self, x, D):
super(FunctionWithDerivative, self).__init__()
self.x = x #this would be the var the function depends on f(x)
self.D = D #this would be the output i wish for
Derivative(FunctionWithDerivative(x,D),x)
def fdiff(self, argindex=1):
"""
Return the first derivative of this function.
"""
print("**************",self.args) -------- NOT BEING CALLED!!!!!
if len(self.args) == 1:
if(self.args[0] == self.x):
return self.D
else:
return 0
else:
raise ArgumentIndexError(self, argindex)
u = symbols('u')
e = Function('e')(u)
s = FunctionWithDerivative(symbols('e'), symbols('DsDe') )
print(s)
Derivative(e,u)
print(diff(e,u))
print(diff(s,e))
print(diff(s,u)) #here i would like "der" to be replaced within the chain
rule
On Friday, October 14, 2016 at 5:27:21 PM UTC+2, Aaron Meurer wrote:
>
> If you want to define advanced things you need to subclass from
> Function rather than using symbols(cls=Function). For derivatives, you
> should define fdiff, which should return the derivative of the
> function without consideration of the chain rule. For example, search
> for "fdiff" in this file to see some examples for exp, log, and
> LambertW
> https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/exponential.py.
>
>
>
> Aaron Meurer
>
> On Fri, Oct 14, 2016 at 4:53 AM, Riccardo Rossi <[email protected]
> <javascript:>> wrote:
> > Dear List,
> >
> > i am writing since i would like to define the output of the derivative
> of a
> > function, and i don't have a clue of how to achieve it
> >
> > to explain what i wish to do, let's consider the following script
> >
> > from sympy import *
> >
> > u = symbols('u')
> > der = symbols('der')
> > e = symbols('e', cls=Function)(u)
> > s = symbols('s', cls=Function)(e)
> > Derivative(e,u) = der #essentially i would like to teach to sympy to use
> a
> > symbol for the Derivative
> > ---> but here i get "SyntaxError: can't assign to function call"
> >
> > print(diff(e,u))
> > print(diff(s,e))
> > print(diff(s,u)) #here i would like "der" to be replaced within the
> chain
> > rule
> >
> > any suggestion would be very welcome...
> >
> > 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 to [email protected] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > Visit this group at https://groups.google.com/group/sympy.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.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 https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/939b99f4-b75e-4f76-992e-28a92015e926%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.