Permalink
Browse files

tweak unrad: do not use posify

    Posify allows some radicals to simplify to zero when they
    shouldn't. A test for which this happened was added. This
    also raised the issue that expr.as_poly() may fail when
    expr simplifies to a constant during the expansion process
    that poly uses, so this is circumnvented by pre-expanding
    the expression and checking for this condition.
  • Loading branch information...
1 parent 9f47f42 commit 95c86b2b595afec4ba8447406a35f25660c49e21 @smichr smichr committed Apr 6, 2012
Showing with 15 additions and 10 deletions.
  1. +6 −9 sympy/solvers/solvers.py
  2. +9 −1 sympy/solvers/tests/test_solvers.py
View
@@ -29,7 +29,7 @@
from sympy.functions.elementary.miscellaneous import real_root
from sympy.simplify import (simplify, collect, powsimp, posify, powdenest,
nsimplify)
-from sympy.simplify.sqrtdenest import sqrt_depth
+from sympy.simplify.sqrtdenest import sqrt_depth, _mexpand
from sympy.matrices import Matrix, zeros
from sympy.polys import roots, cancel, Poly, together, factor
from sympy.functions.elementary.piecewise import piecewise_fold, Piecewise
@@ -2179,14 +2179,15 @@ def _take(d):
if cov is None:
cov = []
- eq, reps = posify(eq)
- if syms:
- syms = [s.subs([(v, k) for k, v in reps.items()]) for s in syms]
eq = powdenest(eq)
eq, d = eq.as_numer_denom()
+ eq = _mexpand(eq)
if _take(d):
dens.add(d)
+ if not eq.free_symbols:
+ return eq, cov, list(dens)
+
poly = eq.as_poly()
rads = set([g for g in poly.gens if _take(g) and
@@ -2276,10 +2277,6 @@ def _take(d):
neq = unrad(eq, *syms, **dict(cov=cov, dens=dens, n=len(rterms)))
if neq:
eq = neq[0]
- eq = eq.subs(reps)
if eq.could_extract_minus_sign():
eq = -eq
- return (expand_mul(expand_multinomial(eq)),
- [(c[0], c[1].subs(reps)) for c in cov],
- [d.subs(reps) for d in dens]
- )
+ return (_mexpand(eq), cov, list(dens))
@@ -7,7 +7,7 @@
from sympy.core.function import nfloat
from sympy.solvers import solve_linear_system, solve_linear_system_LU,\
solve_undetermined_coeffs
-from sympy.solvers.solvers import _invert, unrad, checksol
+from sympy.solvers.solvers import _invert, unrad, checksol, posify
from sympy.utilities.pytest import XFAIL, raises, skip
@@ -706,6 +706,14 @@ def s_check(rv, ans):
assert solve(sqrt(17*x - sqrt(x**2 - 5)) - 7) == [3]
assert solve(sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x))) == []
+ # don't posify the expession in unrad and use _mexpand
+ z = sqrt(2*x + 1)/sqrt(x) - sqrt(2 + 1/x)
+ p = posify(z)[0]
+ assert solve(p) == []
+ assert solve(z) == []
+ assert solve(z + 6*I) == [-S(1)/11]
+ assert solve(p + 6*I) == []
+
@XFAIL
def test_multivariate():
assert solve((x**2 - 2*x + 1).subs(x, log(x) + 3*x)) == [LambertW(3*S.Exp1)/3]

0 comments on commit 95c86b2

Please sign in to comment.