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

Simplify before testing convergence #16739

Closed

Conversation

AnimeshSinha1309
Copy link
Contributor

Added simplification of function, before testing for convergence and added tests.

References to other Issues or PRs

Fixes #16699

Brief description of what is fixed or changed

Fixing issues with Sum of the factorial series, just added simplification of the expression before the tests for convergence start.

Release Notes

  • concrete
    • simplify sums before testing for convergence

Added simplification of function, before testing for convergence and added tests. Fixed sympy#16699.
@sympy-bot
Copy link

Hi, I am the SymPy bot (v147). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.5.

Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it.

Click here to see the pull request description that was parsed.

Added simplification of function, before testing for convergence and added tests.

#### References to other Issues or PRs
Fixes #16699

#### Brief description of what is fixed or changed
Fixing issues with Sum of the factorial series, just added simplification of the expression before the tests for convergence start.

#### Release Notes

<!-- BEGIN RELEASE NOTES -->
* concrete
  * simplify sums before testing for convergence
<!-- END RELEASE NOTES -->

@smichr
Copy link
Member

smichr commented Apr 27, 2019

Is there a way to just return None if you know that simplification? Or if this is a high-level routine then maybe using simplify makes sense.

@maurogaravello
Copy link
Contributor

I do not think this resolves the issue. This patch works in the specific example and probably is better than nothing.
If you consider

expr = 1 - cos(1/n)
s = Sum(expr, (n, 1, oo))

the result given by SymPy is False, even with the proposed patch, but the correct result is True.
This is due, as in the original example, to a non sharp result of order.

A possible alternative is to evaluate limit(expr * n, n, oo). In the case the result is a positive number (not 0), then the convergence is False. This is in substitution of the lines (445-446)

if p1_series_test[p] >= -1:
                return S.false 

@AnimeshSinha1309
Copy link
Contributor Author

AnimeshSinha1309 commented Apr 27, 2019

@smichr I did not fully understand what you said. Do you mean that if the call to simplify fails? When exactly should I return None.
I see that this patch is slowing things down and not solving the issue.
Also the following calls to the order function still don't give answer that make sense:

>>> print(O(factorial(n), (n, oo)))
O(factorial(n), (n, oo))
>>> print(O(factorial(n)/factorial(n-2), (n, oo)))
O(1, (n, oo))
>>> print(O(factorial(n)/factorial(n+2), (n, oo)))
O(1, (n, oo))

We would want that the factorial() calls all mapped to a result O(exp(n*log(n)), (n, oo)), because the factorial is bounded by (n/2)**n and (n/3)**n, right?

@AnimeshSinha1309
Copy link
Contributor Author

So again, this function call is the problem here, right?

>>> n = Symbol('n')
>>> O(1-cos(n), (n, 0))
O(1)

The result should have been O(n^2), but that would require the Order function to have some understanding of the Taylor series expansions, won't it?

Also calling limit(expr * n) might just work, but even then all the calls to order don't really make much sense. 😢

@maurogaravello
Copy link
Contributor

So again, this function call is the problem here, right?

>>> n = Symbol('n')
>>> O(1-cos(n), (n, 0))
O(1)

Exactly. This is the main problem.
O(1) here is correct, but not sharp!
O(n**-2) is also correct. This would produce the correct result.

@smichr
Copy link
Member

smichr commented May 9, 2019

I see that I had an incomplete thought in my suggestion. What it was intended to suggest is that maybe obtaining a meaningless O expression of something like that would allow you to know that you should return none of else use a limit or simplification.

Update the master to the current state and restart on the issue.
@oscarbenjamin
Copy link
Contributor

What's the status of this? @AnimeshSinha1309 @smichr

@AnimeshSinha1309
Copy link
Contributor Author

Sorry, I did not follow up on this. My Attempts did not work out, I would appriciate any help I can get. I am available to work again, trying now.

@AnimeshSinha1309
Copy link
Contributor Author

AnimeshSinha1309 commented Jul 3, 2019

Honestly I am out of ideas, simple fixes to the is_convergent() method, or calling Simplify, fail on examples like 1-cos(1/n), and fixing the Order constructor is hard and I always end up breaking a bunch of tests. I am wondering if it would be okay to just fix a few cases, and leave fixing the Order function for another issue.

For now, this is my plan for fixing the limit comparison test in summations.py

### ------ limit-comparison with p-series ------ ###
if limit(sequence_term * sym, sym, oo) > 0:
    return False
if limit(sequence_term * (sym ** 1.0001), sym, oo) < oo:
    return True

Changed the is_convergent() method to test limit comparison against 1/n and 1/n**1.0001, fixing the convergence test problem, though not the sharp order problem.
The previous commit forgot to call the function is_convergent in the tests, missing out the brackets. Fixing that here.
@AnimeshSinha1309
Copy link
Contributor Author

>>> Product(n/(n + 1), (n, 1, oo)).is_convergent()
False

Is this example not incorrect, since Product(n/(n + 1), (n, 1, oo)) = (1/2) * (2/3) * ... * (n-1/n) = 1/n = 1/oo = 0. So the answer should be True, it's convergent, should it not?

Some tests fixed, some code. Changed some tests that seem most certainly wrong, esp. an example.
@jksuom
Copy link
Member

jksuom commented Jul 3, 2019

An infinite product with limit 0 is considered divergent.

Handling the negative constant, fixing what I thought was an erroneous test and changed.
@codecov
Copy link

codecov bot commented Jul 3, 2019

Codecov Report

Merging #16739 into master will increase coverage by 0.559%.
The diff coverage is 70.833%.

@@              Coverage Diff              @@
##            master    #16739       +/-   ##
=============================================
+ Coverage   73.922%   74.482%   +0.559%     
=============================================
  Files          620       623        +3     
  Lines       160243    161391     +1148     
  Branches     37600     37878      +278     
=============================================
+ Hits        118456    120208     +1752     
+ Misses       36305     35858      -447     
+ Partials      5482      5325      -157

@AnimeshSinha1309
Copy link
Contributor Author

Thanks for that, I fixed my mistake, undid the example that I had changed. I am just confused about one more thing, I changed a few lines in Summation, and added 3 tests for it, why is my Codecov score still falling, should I just keep adding mode examples?

Issue sympy#14488 is no longer raising a NotImplementedError due to the limit being evaluated correctly.
### ------------- alternating series test ----------- ###
dict_val = sequence_term.match((-1)**(sym + p)*q)
if not dict_val[p].has(sym) and is_decreasing(dict_val[q], interval):
return S.true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this and everything below is untested. Are these tests (alternating series etc) not already implemented somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand, there are already tests for Alternating Series and for everything else that follows. I believe that the issue might be that my p-series limit comparison is just returning a solution, blocking access to some of the functions later in the code. But now the codecov score looks pretty decent, right? And if not, what is it that I fix, I have no clue about codecov?

@czgdp1807
Copy link
Member

Closing as the associated issue is outdated.

@czgdp1807 czgdp1807 closed this May 20, 2020
@AnimeshSinha1309 AnimeshSinha1309 deleted the issue/convergence-16699 branch May 20, 2020 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong result in the convergence of infinite sums with factorials
7 participants