Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify sqrt expressions in sympy

I'm using sympy v1.0 in a Jupyter Notebook. I'm having trouble getting expression to simplify how I'd like. Here's a toy example; it does the same thing my more complicated expressions do...

import sympy
sympy.init_printing(use_latex='mathjax')
x, y = sympy.symbols("x, y", real=True, positive=True)
sympy.simplify(sqrt(2*x/y))

gives me...

   expression from sympy

But I would prefer...

   enter image description here

How can I get sympy to group things in this way? Ive tried some of the other simplify functions, but they all give me the same result. Or am I missing something else?

like image 276
kwinkunks Avatar asked Apr 22 '16 16:04

kwinkunks


1 Answers

Use "symbol trickery" for numbers that you want to behave like symbols and "vanilla symbols" when you don't want simplifications (as @asmeurer pointed out):

>>> _2,x,y = list(map(Symbol,'2xy'))
>>> sqrt(_2*x/y)
sqrt(2*x/y)
like image 121
smichr Avatar answered Oct 25 '22 13:10

smichr