Skip to content

Commit

Permalink
Merge pull request #26720 from ForeverHaibara/fix-radsimp
Browse files Browse the repository at this point in the history
Fix radsimp for non-Expr arguments
  • Loading branch information
oscarbenjamin committed Jun 18, 2024
2 parents 6b8e209 + ba3541e commit 7fc4652
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sympy/simplify/radsimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ def radsimp(expr, symbolic=True, max_terms=4):
1/(a + b*sqrt(c))
"""
from sympy.core.expr import Expr
from sympy.simplify.simplify import signsimp

syms = symbols("a:d A:D")
Expand Down Expand Up @@ -893,9 +894,14 @@ def handle(expr):
# We do this by recursively calling handle on each piece.
from sympy.simplify.simplify import nsimplify

if expr.is_Atom:
return expr
elif not isinstance(expr, Expr):
return expr.func(*[handle(a) for a in expr.args])

n, d = fraction(expr)

if expr.is_Atom or (d.is_Atom and n.is_Atom):
if d.is_Atom and n.is_Atom:
return expr
elif not n.is_Atom:
n = n.func(*[handle(a) for a in n.args])
Expand Down Expand Up @@ -992,6 +998,9 @@ def handle(expr):
return expr
return _unevaluated_Mul(n, 1/d)

if not isinstance(expr, Expr):
return expr.func(*[radsimp(a, symbolic=symbolic, max_terms=max_terms) for a in expr.args])

coeff, expr = expr.as_coeff_Add()
expr = expr.normal()
old = fraction(expr)
Expand Down
8 changes: 8 additions & 0 deletions sympy/simplify/tests/test_radsimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def test_radsimp():
eq = sqrt(x)/y**2
assert radsimp(eq) == eq

# handle non-Expr args
from sympy.integrals.integrals import Integral
eq = Integral(x/(sqrt(2) - 1), (x, 0, 1/(sqrt(2) + 1)))
assert radsimp(eq) == Integral((sqrt(2) + 1)*x , (x, 0, sqrt(2) - 1))

from sympy.sets import FiniteSet
eq = FiniteSet(x/(sqrt(2) - 1))
assert radsimp(eq) == FiniteSet((sqrt(2) + 1)*x)

def test_radsimp_issue_3214():
c, p = symbols('c p', positive=True)
Expand Down

0 comments on commit 7fc4652

Please sign in to comment.