Why is sympy.diff not distinguishing between simplex polynomials as expected?

I'm trying to figure out why sympy.diff

doesn't differentiate polynomials sympy

as expected. Usually sympy.diff

works just fine if a symbolic variable is defined and the polynomial is NOT defined with sympy.Poly

. However, if a function is defined using sympy.Poly

, sympy.diff

apparently, does not compute the derivative. Below is some sample code that shows what I mean:

import sympy as sy

# define symbolic variables
x = sy.Symbol('x')
y = sy.Symbol('y')

# define function WITHOUT using sy.Poly
f1 = x + 1
# define function WITH using sy.Poly
f2 = sy.Poly(x + 1, x, domain='QQ')

# compute derivatives and return results
df1 = sy.diff(f1,x)
df2 = sy.diff(f2,x)
print('f1:  ',f1)
print('f2:  ',f2)
print('df1:  ',df1)
print('df2:  ',df2)

      

This outputs the following results:

f1:   x + 1
f2:   Poly(x + 1, x, domain='QQ')
df1:   1
df2:   Derivative(Poly(x + 1, x, domain='QQ'), x)

      

Why sympy.diff

doesn't he know how to tell the difference between the version of the polynomial sympy.Poly

? Is there a way to differentiate the polynomial, sympy

or a way to transform the polynomial sympy

into a form that allows it to be differentiated?

Note . I've tried with different domains (i.e. domain='RR'

instead of domain='QQ'

) and the output doesn't change.

+3


source to share


1 answer


It seems like a mistake. You can work around it by calling diff

the instance directly Poly

. Ideally, calling a function diff

from a top-level sympy module should produce the same result as calling a method diff

.



In [1]: from sympy import *

In [2]: from sympy.abc import x

In [3]: p = Poly(x+1, x, domain='QQ')

In [4]: p.diff(x)
Out[4]: Poly(1, x, domain='QQ')

In [5]: diff(p, x)
Out[5]: Derivative(Poly(x + 1, x, domain='QQ'), x)

In [6]: diff(p, x).doit()
Out[6]: Derivative(Poly(x + 1, x, domain='ZZ'), x)

      

+4


source







All Articles