-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Update solvers to handle binomial. #18802
Update solvers to handle binomial. #18802
Conversation
✅ Hi, I am the SymPy bot (v158). 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.6. 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.
Update The release notes on the wiki have been updated. |
I'm not sure where this belongs but certainly not as the opening lines in the solve function. Unfortunately solve is a bit of a mess so there isn't necessarily a correct place to put something like this. |
I think putting those conditions in "preprocess equations" is better. |
Codecov Report
@@ Coverage Diff @@
## master #18802 +/- ##
=============================================
+ Coverage 75.651% 75.676% +0.024%
=============================================
Files 647 647
Lines 168420 168512 +92
Branches 39682 39705 +23
=============================================
+ Hits 127413 127524 +111
+ Misses 35454 35440 -14
+ Partials 5553 5548 -5 |
Please review and suggest the changes. |
Consider that you might have an expression involving a binomial like
We probably don't want to expand all functions so it would be better to use expr.replace to pick out the binomials and expand them. This code is still not in the right place: solve hasn't even converted Eqs to Expr at that point. Look through solve to find where similar transformations are happening. |
I have made the changes so that only binomial terms are expanded and added the code after Eqs are checked and converted. Please check if it is correctly done. |
can someone explain to me why my test on travis-ci is failing. |
I pushed the same changes again and it passed. |
I think translating binomials should still come later after the loop that it is in now. This will need to have a test added for equations with binomial coefficients that can be solved. |
I will make the changes. |
I have added the test. |
I have added the binomial expansion after the loop, please suggest changes. |
In what way was it failing? |
I had written it like this in the previous commit though it worked perfectly for so i changed it to solve cases like this |
This is probably best:
|
It looks like
|
Should we expand the binomial if we have a multivariable equation, example |
Yes if any of the symbols of interest is in the binomial. Otherwise we won't be able to solve the equations. |
ok, I will make that change. |
I have made the changes, please review. |
expand_func expands I think changing |
Or maybe just |
yes, this one will also expand cases like |
should I send a PR for this along with the issue? |
You can make those changes in this PR as well |
I have made the changes, please review. |
@@ -1012,7 +1012,7 @@ def _eval_expand_func(self, **hints): | |||
return binomial(*self.args) | |||
|
|||
k = self.args[1] | |||
if k.is_Add and n in k.args: | |||
if (n-k).is_Integer: | |||
k = n - k |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a test for this.
sympy/solvers/solvers.py
Outdated
@@ -1460,6 +1460,9 @@ def _solve(f, *symbols, **flags): | |||
raise NotImplementedError(not_impl_msg % f) | |||
symbol = symbols[0] | |||
|
|||
#expand binomials only if it has the unknown symbol | |||
f = f.replace(lambda e: isinstance(e, binomial) and e.has(symbol), lambda e: expand_func(e)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too long
sympy/solvers/tests/test_solvers.py
Outdated
assert solve(Eq(binomial(x,2),0)) == [0, 1] | ||
assert solve(a+binomial(x, 3),a) == [-binomial(x, 3)] | ||
assert solve(x-binomial(a, 3) + binomial(y,2) + sin(a),x) == [-sin(a) + binomial(a, 3) - binomial(y, 2)] | ||
assert solve((x+1)-binomial(x+1, 3),x) == [-2, -1, 3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include a space after a comma
This looks good. I'll merge it once tests pass. For future PRs can you try to use better commit messages? The messages here started out good but then got less good as time went on. You should imagine that the message will be looked at in future by someone who does not know the full context of the PR. Something like "add test" does not convey much about the change that is being made to the code. |
I will keep it in mind, this will not happen again. |
Merged. Thanks for this! |
Thank you, l learned a lot from this. |
References to other Issues or PRs
Fixes #10993
Brief description of what is fixed or changed
Added conditions to expand binomial in solvers
Update expand function for binomial
Other comments
Please suggest changes.
Release Notes