Why is my Sympy code incorrectly calculating the first order Taylor series approximation?

I have an expression like this

sympy expression

which is introduced in sympy like this (for the sake of a reproducible example in this question)

from sympy import * expression = Add(Mul(Integer(-1), Float('0.9926375361451395', prec=2), Add(Mul(Float('0.33167082639756074', prec=2), Pow(Symbol('k1'), Float('-0.66666666666666674', prec=2)), Pow(Symbol('n1'), Float('0.66666666666666674', prec=2))), Mul(Float('0.97999999999999998', prec=2), exp(Mul(Integer(-1), Symbol('mu1'))))), Pow(Add(Mul(Float('0.97999999999999998', prec=2), Symbol('k1'), exp(Mul(Integer(-1), Symbol('mu1')))), Mul(Integer(-1), Symbol('k2')), Mul(Pow(Symbol('n1'), Float('0.66666666666666674', prec=2)), Pow(Mul(Symbol('k1'), exp(Mul(Integer(-1), Symbol('mu1')))), Float('0.33333333333333331', prec=2)))), Integer(-1))), Pow(Add(Mul(Float('0.97999999999999998', prec=2), Symbol('k0'), exp(Mul(Integer(-1), Symbol('mu0')))), Mul(Integer(-1), Symbol('k1')), Mul(Pow(Symbol('n0'), Float('0.66666666666666674', prec=2)), Pow(Mul(Symbol('k0'), exp(Mul(Integer(-1), Symbol('mu0')))), Float('0.33333333333333331', prec=2)))), Integer(-1))) 

Observing this expression, a first-order Taylor approximation for any of the variables, for example. k1 , around some non-zero value should be nonzero, but this code

 x = symbol("x") expression.series(k1, x0 = x, n = 1) 

returns 0 . This is a problem because I am trying (ultimately) to compute the multidimensional approximation of the Taylor series, in the same spirit as this answer , and if one of the extensions of the series is erroneously evaluated to zero, it all breaks down.

Did I code something incorrectly, or is it a simple basic calculus because it is bad and it actually evaluates to zero? From the documentation for the series I am sure that I am using it correctly.

+5
source share
1 answer

I think this is a bug related to how the add operation processes Orders.

This error only applies if you are calculating the zero order (n = 1) of the Taylor series. To avoid this, you can do

 next(expression.series(k1, x0=x, n=None)) 

which is equivalent

 expression.subs(k1, x0=x) 

Here is a simple description of this error:

 from sympy import cos from sympy.abc import x cos(x).series(x, x0=1, n=2) 

Results in

 cos(1) - (x - 1)*sin(1) + O((x - 1)**2, (x, 1)) 

But

 cos(x).series(x, x0=1, n=1) 

Results in O(x - 1, (x, 1)) instead of cos(1) + O(x - 1, (x, 1)) .

This error occurs due to an error in Add.

 O(x).subs(x,x-1) + 1 

Results in O(x - 1, (x, 1)) instead of 1 + O(x - 1, (x, 1)) .

+4
source

Source: https://habr.com/ru/post/1260325/


All Articles