# issue with variable assumptions This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am using sage to compute a variety of algebra, and often times, when calculating the squareroots of variables, sage gets the assumptions wrong. Here is a short example:

sage: r,M=var('r','M')

sage: assume(r>0,M>0,r>2*M)

sage: sqrt((2*M-r)^2)

2M-r

Clearly the last line is incorrect (should be r-2M), but I have no idea how to fix it. While this is a trivial example, I frequently work with equations that are many variables in length, where ensuring that the correct variables are positive is a requirement.

Edit: I found a partial solution, however it does not seem to work well with simplifying radicals. If there is an alternative to canonicalize_radical which takes assumptions into account, it would be helpful.

edit retag close merge delete

Sort by » oldest newest most voted

Hi,

Apparently you are using an old version of Sage, because with Sage 6.7, one has

sage: r,M=var('r','M')
sage: assume(r>0,M>0,r>2*M)
sage: sqrt((2*M-r)^2)
sqrt((2*M - r)^2)


To simplify it, simply run

sage: s = sqrt((2*M-r)^2)
sage: s.simplify_real()
-2*M + r


which is correct, given the assumption.

more

Thank you! I was using canonicalize.radical(), which is what I assume was the issue.

However, this does not really simplify. Is there something like canonicalize_radicals() that takes assumptions into account? For example, I have the following expression, that won't simplify further sqrt(((M - r)mu + M)^2 + (mu^2 - 1)(2Mr - r^2))), but in Mathematica correctly simplifies to sqrt((mu*M+M-r)^2). canonical_radical finds the solution, but does not get the correct assumptions