Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

series of expression with big-O ignore the big-O #24913

Open
ThibaultLejemble opened this issue Mar 15, 2023 · 3 comments
Open

series of expression with big-O ignore the big-O #24913

ThibaultLejemble opened this issue Mar 15, 2023 · 3 comments
Labels

Comments

@ThibaultLejemble
Copy link

It seems that when an expression involves a big-O, then its series expansion does note take into account this big-O.

The example below shows the expansion of 1/(1+x) and 1/(1+x+O(x**n)) at point 0 and order 4, which I believe should be different

from sympy import *

x = Symbol('x')

f = 1 / (1 - x)
g = 1 / (1 - x + O(x**2))

# Ok: 1 + x + x**2 + x**3 + x**4 + O(x**5)
print(series(f, x, 0, 4)) 

# Wrong: 1 + x + x**2 + x**3 + x**4 + O(x**5) 
# Expected: 1 + x + O(x**2)
print(series(g, x, 0, 4))

It seems that the O(x**5) is ignored right?

@oscarbenjamin
Copy link
Contributor

Yes, it looks like it is ignored:

In [15]: series(1/(1 - x + O(x**2)))
Out[15]: 
         2    3    4    561 + x + x  + x  + x  + x  + Ox

@oscarbenjamin
Copy link
Contributor

Possibly related to gh-24477

@anutosh491
Copy link
Member

Well I fixed the Pow._eval_nseries function locally which was responsible for this . So now I get

>>> (1/(1 - x + O(x**2))).nseries(x)
1 + x + O(x**2)

But for fixing the series method , I am unsure about the current framework we have

sympy/sympy/core/expr.py

Lines 3046 to 3064 in 3878065

elif ngot < n:
# increase the requested number of terms to get the desired
# number keep increasing (up to 9) until the received order
# is different than the original order and then predict how
# many additional terms are needed
from sympy.functions.elementary.integers import ceiling
for more in range(1, 9):
s1 = self._eval_nseries(x, n=n + more, logx=logx, cdir=cdir)
newn = s1.getn()
if newn != ngot:
ndo = n + ceiling((n - ngot)*more/(newn - ngot))
s1 = self._eval_nseries(x, n=ndo, logx=logx, cdir=cdir)
while s1.getn() < n:
s1 = self._eval_nseries(x, n=ndo, logx=logx, cdir=cdir)
ndo += 1
break
else:
raise ValueError('Could not calculate %s terms for %s'
% (str(n), self))

Now that series internally uses nseries , we get the correct answer through nseries but then the series method compulsorily forces the presence of n (in this case 6 ) terms . I am not sure why this is being done though

Do we expect series(1/(1 - x + O(x**2))) to compulsorily have 6 terms or could we stop at 3 (after O(x**2)) . @jksuom what are your thoughts here ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants