undy
undy

Reputation: 93

SymPy: unable to simplify rather simple expression

I have an expression (expr, see below) that I am unable to simplify in SymPy. For real and positive x, expr is equivalent to x**3 + 2*x, but simplify and refine do not simplify the expression at all. (Mathematica does the simplication without any effort). How to simplify this expression with SymPy?

from sympy import *

x = var('x')

expr = 16*x**3/(-x**2 + sqrt(8*x**2 + (x**2 - 2)**2) + 2)**2 - 2*2**(S(4)/5)*x*(-x**2 + sqrt(8*x**2 + (x**2 - 2)**2) + 2)**(S(3)/5) + 10*x

expr1 = simplify(expr) # does nothing

expr2 = refine(expr, Q.positive(x)) # does nothing

Upvotes: 1

Views: 656

Answers (1)

tel
tel

Reputation: 13999

It can be done!

I rescind my earlier answer. Your expression can be simplified using Sympy. Here's how:

import sympy as sym

x = sym.symbols('x', positive=True)
expr = 16*x**3/(-x**2 + sym.sqrt(8*x**2 + (x**2 - 2)**2) + 2)**2 - 2*2**(sym.S(4)/5)*x*(-x**2 + sym.sqrt(8*x**2 + (x**2 - 2)**2) + 2)**(sym.S(3)/5) + 10*x

sym.simplify(sym.factor(sym.factor(sym.expand(sym.radsimp(expr))), deep=True))

Output:

x*(x**2 + 2)

Basically, I dug through all of the docs on sympy.simplify until I found that magic combination. Also, you have to define x as positive when you create the symbol, just as I did in the code above.

Comment on Mathematica

"Mathematica does the simplication without any effort"

I don't think you should ever underestimate the quantity of time and money that has gone into making the heuristic nightmare that is Mathematica's Simplify seem like it "just works". Sadly, in a lot of ways Sympy is still in it's infancy in comparison. sympy.simplify is one of those ways.

Upvotes: 2

Related Questions