Nice mess in multiple derivatives...

38 views
Skip to first unread message

Emmanuel Charpentier

unread,
Aug 1, 2020, 4:06:47 AM8/1/20
to sage-support
Consider :

sage: f=function("f")
sage: m=var("m", domain="integer")
sage: assume(m>0)

Sage follows the Maxima convention for multiple derivation d^n/dx^n f(x) is diff(f(x),x,n) :

sage: diff(sin(x),x,3)
-cos(x) # Okay....
sage: diff(sin(x),x,m)
0 ## Huh ???
sage: diff(f(x),x,3)
diff(f(x), x, x, x) # Okay, sort of
sage: diff(f(x),x,m)
0 ## Huh ???

Note that Sage will silently return a mathematically false result when the degree of derivation is symbolic.

The problem doesn't seem to reside i Maxima, which returns results consistent to its convention :

sage: %maxima diff(sin(x),x,3)
-cos(x)
sage: %maxima diff(sin(x),x,m)
'diff(sin(x),x,m)
sage: %maxima diff(f(x),x,3)
'diff(f(x),x,3)
sage: %maxima diff(f(x),x,m)
'diff(f(x),x,m)

The problem is similar to the one seen in sympy when incorrectly called :

sage: import sympy
sage: sympy.diff(*[sympy.sympify(u) for u in [sin(x),x,3]])
-cos(x)
sage: sympy.diff(*[sympy.sympify(u) for u in [sin(x),x,m]])
0 # Huh again...
sage: sympy.diff(*[sympy.sympify(u) for u in [sin(x),[x,m]]])
Derivative(sin(x), (x, m)) # Unfulfilling but correct...
sage: sympy.diff(*[sympy.sympify(u) for u in [f(x),[x,m]]])
Derivative(f(x), (x, m)) # Unfulfilling but correct...

Note that sympy's alternative convention for multiple derivation gives consistent results :

sage: sympy.diff(*[sympy.sympify(u) for u in [sin(x),[x,m]]]).subs([(m, 3)])
Derivative(sin(x), (x, 3))
sage: sympy.diff(*[sympy.sympify(u) for u in [sin(x),[x,m]]]).subs([(m, 3)]).doit
....: ()
-cos(x)
sage: sympy.diff(*[sympy.sympify(u) for u in [f(x),[x,m]]]).subs([(m, 3)])
Derivative(f(x), (x, 3))
sage: sympy.diff(*[sympy.sympify(u) for u in [f(x),[x,m]]]).subs([(m, 3)]).doit()
....: 
Derivative(f(x), (x, 3))

Sage doesn't accept Sympy's convention for multiple derivation :

sage: diff(f(x), (x,3))
---------------------------------------------------------------------------
[ Snip ... ]

TypeError: no canonical coercion from <class 'tuple'> to Symbolic Ring

Therefore, Sage seems to interpret diff(<expression>, <variable>, <numerical value>) as d/d<variable> <expression> and diff(<expression>, <variable1>, <variable2>) as d/d<variable1> d/d<variable2> <expression>, with no way to express d^<variable2>/d<variable1)^<variable2> <expression>.

This is, IMNSHO, a serious mess. Which, as demonstrated above, can silently return mathematically false results.

The problem is that I cannot see a way to fix this that doesn't break backwards compatibility somehow.

Help about the way to efficiently file a (probably critical) ticket against this horror most welcome...

PS : One can note that Mathematica expresses its result in the symbolic case in a peculiar way :

sage: mathematica.D(f(x),[x,m])
Derivative[m][F][x]

One can also note that it may handle some special cases in a smarter way than Sage/Maxima/Sympy :

sage: mathematica.D(sin(x),[x,m])
Sin[(m*Pi)/2 + x]

but this is another story...

kcrisman

unread,
Aug 1, 2020, 8:57:20 PM8/1/20
to sage-support
Just for information, I believe the derivatives are all handled in Pynac.  It's possible that does not have the capability of a symbolic number of derivatives.  It would be worth tracing through to see why 0 is the answer.  Perhaps it (Sage) interprets the syntax as being with respect to the variable m - I think that is what we would have in that case, d/dm (d/dx f) = 0.

Emmanuel Charpentier

unread,
Aug 2, 2020, 3:47:16 AM8/2/20
to sage-support

Dear Karl-Dieter

FYI, this thread, reposted by Éric Gourgoulhon on ~sage-devel~, has moved here.

Le dimanche 2 août 2020 02:57:20 UTC+2, kcrisman a écrit :

Just for information, I believe the derivatives are all handled in Pynac.

That’s what a cursory look shows. But it seems quite intricate…

It's possible that does not have the capability of a symbolic number of derivatives.

The rest of the thread seems to establish that it does not.

It would be worth tracing through to see why 0 is the answer.  Perhaps it (Sage) interprets the syntax as being with respect to the variable m - I think that is what we would have in that case, d/dm (d/dx f) = 0.

That’s my interpretation.

Reply all
Reply to author
Forward
0 new messages