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

solve wrt additive generator #19524

Merged
merged 1 commit into from Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions sympy/solvers/solvers.py
Expand Up @@ -20,7 +20,7 @@
default_sort_key)
from sympy.core.sympify import sympify
from sympy.core import (S, Add, Symbol, Equality, Dummy, Expr, Mul,
Pow, Unequality)
Pow, Unequality, Wild)
from sympy.core.exprtools import factor_terms
from sympy.core.function import (expand_mul, expand_log,
Derivative, AppliedUndef, UndefinedFunction, nfloat,
Expand Down Expand Up @@ -1444,6 +1444,25 @@ def _solve(f, *symbols, **flags):
sol = simplify(sol)
return [sol]

poly = None
# check for a single non-symbol generator
dums = f_num.atoms(Dummy)
D = f_num.replace(
lambda i: isinstance(i, Add) and symbol in i.free_symbols,
lambda i: Dummy())
if not D.is_Dummy:
dgen = D.atoms(Dummy) - dums
if len(dgen) == 1:
d = dgen.pop()
w = Wild('g')
gen = f_num.match(D.xreplace({d: w}))[w]
spart = gen.as_independent(symbol)[1].as_base_exp()[0]
if spart == symbol:
try:
poly = Poly(f_num, spart)
except PolynomialError:
pass

result = False # no solution was obtained
msg = '' # there is no failure message

Expand All @@ -1457,7 +1476,8 @@ def _solve(f, *symbols, **flags):
# generator is not a symbol

try:
poly = Poly(f_num)
if poly is None:
poly = Poly(f_num)
if poly is None:
raise ValueError('could not convert %s to Poly' % f_num)
except GeneratorsNeeded:
Expand Down
12 changes: 12 additions & 0 deletions sympy/solvers/tests/test_solvers.py
Expand Up @@ -2187,3 +2187,15 @@ def test_issue_19113_19102():
-2*atan(-sqrt(5)/2 + h + sqrt(2)*sqrt(1 - sqrt(5))/2),
-2*atan(-sqrt(2)*sqrt(1 + sqrt(5))/2 + h + sqrt(5)/2)]
assert solve(3*cos(x) - sin(x)) == [atan(3)]


def test_issue_19509():
a = S(3)/4
b = S(5)/8
c = sqrt(5)/8
d = sqrt(5)/4
assert solve(1/(x -1)**5 - 1) == [2,
-d + a - sqrt(-b + c),
-d + a + sqrt(-b + c),
d + a - sqrt(-b - c),
d + a + sqrt(-b - c)]