# How to get multiplication of square roots to simplify?

All I want is for Sage to take the sqrt(3)*sqrt(2) and show me it is sqrt(6). I have looked through the forums and found a few solutions but they are all from 5+ years ago and don't seem to do anything any more. Here is an example:

from sage.manifolds.utilities import simplify_chain_real
a = var("a")
assume(a>0)
a = sqrt(2)*sqrt(3)+6
show(a)
show(simplify_chain_real(a))
show(a.simplify_real())


My output is the following for all of these: sqrt(3)*sqrt(2)+6

What do I need to do to get the output to display sqrt(6)+6? I am sure there is some really advanced mathematical reason for this not to work, but I just dealing with fairly standard stuff.

I am using the following version of Sage:

SageMath version 9.8, Release Date: 2023-02-11

edit retag close merge delete

Please link to the solutions from years ago, so that updated versions can be posted.

( 2023-03-30 15:39:56 +0200 )edit

This answer still works: a.maxima_methods().rootscontract()

( 2023-03-30 15:51:06 +0200 )edit

Sort by » oldest newest most voted

Well...

sage: (sqrt(3)*sqrt(2)+6)._sympy_().simplify()._sage_()
sqrt(6) + 6


HTH,

more

One possible trick is to replace sqrt(2) by a variable t and sqrt(3) by a variable d, so you get a polynomial in t and d, and introduce a variable z for sqrt(6), and then reduce the polynomial in t and d by t*d - z using some monomial ordering where t*d > z, e.g. the lexicographic ordering with t > d > z, so that all instances of t*d are replaced by z, and then you can substitute z by sqrt(6):

def collect_sqrt2sqrt3_to_sqrt6(expr):
expr_poly = expr.subs({sqrt(2) : var('t'), sqrt(3) : var('d')})
R = PolynomialRing(SR, names=['t', 'd', 'z'], order='lex')
t, d, z = R.gens()
poly = expr_poly.polynomial(ring=R)
poly_collected = poly.reduce([t*d - z])
return SR(poly_collected.subs({z : sqrt(6), t : sqrt(2), d : sqrt(3)}))


Example:

sage: a = sqrt(2)*sqrt(3)+6
sage: collect_sqrt2sqrt3_to_sqrt6(a)
sqrt(6) + 6


With some more effort this could be generalized to detect which square roots appear in the expression, etc.

more