# Substituting derivatives in Taylor expansions

Within Taylor expansions, as well as in results of 'Derivative'- operations, derivatives are marked by symbol "D[ ]" . I want to substitute them by other symbolic expressions/functions in order to get the usual presentation of derivative symbols via Latex. The following testprogram demonstrates that derivatives triggered by the "derivative"-operation can indeed be substituted by user-defined symbols, but not derivatives in Taylor expansions.

x,x_0=var("x,x_0")
a_4=var('a_4',latex_name='a_4')
a_5=var('a_5',latex_name='a_5')
T_e=function('T_e',nargs=1)
dt1=function('dt1',nargs=1)
dt2=function('dt2',nargs=1)
dTedx=var('dTedx',latex_name='\\frac{\operatorname{d}{T_e}}{\operatorname{d}x}')
type(T_e)
dt1=T_e(x).derivative(x)
print'dt1:',dt1
dt2=T_e(x_0).derivative(x_0)
print'dt2:',dt2
a3(x)=a_4+a_5*T_e(x)
print 'a3(x):',a3(x)
da3=a3(x).derivative(x)
print 'da3:', da3
print '### After a "derivative"-operation, substituting dt1 by dTedx works:'
da4=da3.subs(T_e(x).derivative(x)==dTedx)
print 'da4:',da4
ata=a3(x).taylor(x,x_0,1)
print 'Taylor series ata:',ata
print '### After Taylor expansion, substituting dt2 by dTedx does not work:'
atb=ata.subs(T_e(x_0).derivative(x_0)==dTedx)
print 'Taylor series atb:',atb


I run this program within In a SAGE6.8 notebook-session. The output is:

dt1: D[0](T_e)(x)
dt2: D[0](T_e)(x_0)
a3(x): a_5*T_e(x) + a_4


### After a "derivative"-operation, substituting dt1 by dTedx works:

da3: a_5*D[0](T_e)(x)
da4: a_5*dTedx


### After Taylor expansion, substituting dt2 by dTedx does not work:

Taylor series ata: a_5*(x - x_0)*D[0](T_e)(x_0) + a_5*T_e(x_0) + a_4
Taylor series atb: a_5*(x - x_0)*D[0](T_e)(x_0) + a_5*T_e(x_0) + a_4


What is the reason for this difference? Maybe, I merely must access the derivative expression within a Taylor expansion in a different way. Thus: which is the correct way to do this?

edit retag close merge delete

Sort by » oldest newest most voted

Don't use the "nargs" argument in the function declaration. It doesn't do anything beyond confusing the Pynac <-> maxima symbol translator. The fact that it confuses the translator is a bug, but given that the "nargs" argument is a no-op in any other respect, the fix for the bug is probably removing the "nargs" argument anyway.

For posterity, the code below helped me diagnose the problem (in hindsight I did see this problem before and it's pretty fundamental: from maxima, one cannot see if T_e is supposed to have an "nargs" attribute).

sage: XX=T_e(x_0).derivative(x_0)
sage: YY=(ata.operands()[0].operands()[2]) #you may need different indices
sage: XX, YY
(D[0](T_e)(x_0), D[0](T_e)(x_0))
sage: bool(XX==YY) #this works because everything gets round-tripped to maxima
True
sage: hash(XX)==hash(YY) #but this indicates something is amiss
False
sage: XX.operator().function(), YY.operator().function()
(T_e, T_e)
sage: XX.operator().function()==YY.operator().function()
False


As you can see the T_e in the taylor expands isn't really the same T_e that you defined before. Simply testing equality of expressions doesn't seem to detect this, probably because the advanced equality tests involved: they would send things over to Maxima to do some simplifications and the translations used there would probably be oblivious to the differing internal representation of XX and YY.

With YY instead of XX, things seem to work:

sage: ata.subs(YY==dTedx)
a_5*dTedx*(x - x_0) + a_5*T_e(x_0) + a_4


See http://trac.sagemath.org/ticket/14608 for a ticket about the underlying bug.

more