Simplify square root of square

92 views
Skip to first unread message

Paul Royik

unread,
Feb 27, 2015, 5:42:50 AM2/27/15
to sage-s...@googlegroups.com
What is the way to consistently simplify square roots of squares?

Examples:

sqrt((x+1)^2) - > x+1
sqrt(cos(4*x)+1) -> sqrt(2)cos(2x)

Simon King

unread,
Feb 27, 2015, 8:36:59 AM2/27/15
to sage-s...@googlegroups.com
Hi Paul,
Simplification must not change the value of the expression. sqrt(x^2) is
certainly not equal to x. Even under the additional assumption that x is
real, you could only simplify it to abs(x)

Best regards,
Simon


Vincent Delecroix

unread,
Feb 27, 2015, 8:51:15 AM2/27/15
to sage-s...@googlegroups.com
But...

sage: eq = sqrt((pi-5)^2)
sage: eq.canonicalize_radical()
pi - 5

And as you can read from the documentation
"""
Choose a canonical branch of the given expression. The square root,
cube root, natural log, etc. functions are multi-valued. The
"canonicalize_radical()" method will choose *one* of these values
based on a heuristic.
"""

As Simon said, be careful if you start using canonicalize_radical
since it does not preserve equality

sage: eq = sqrt((pi-5)^2)
sage: eq.numerical_approx()
1.85840734641021
sage: eq.canonicalize_radical().numerical_approx()
-1.85840734641021

Vincent

2015-02-27 14:36 UTC+01:00, Simon King <simon...@uni-jena.de>:
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.
>

Paul Royik

unread,
Feb 27, 2015, 10:25:01 AM2/27/15
to sage-s...@googlegroups.com
OK. Let x is real.
How to rewrite sqrt(cos(4x)+1) into sqrt(2)abs(cos(2x))?

Vincent Delecroix

unread,
Feb 27, 2015, 10:41:07 AM2/27/15
to sage-s...@googlegroups.com
Here is one way... not sure it is the best

sage: eq1 = sqrt(cos(4*x)+1)
sage: eq2 = eq1.simplify_trig()
sage: eq2
sqrt(8*cos(x)^4 - 8*cos(x)^2 + 2)

The next step consists in factoring what is inside the sqrt:

sage: o = eq2.operands()[0]
sage: of = o.factor()
sage: o
8*cos(x)^4 - 8*cos(x)^2 + 2
sage: of
2*(2*cos(x)^2 - 1)^2
sage: eq3 = eq2.subs({o:of})
sage: eq3
sqrt(2)*sqrt((2*cos(x)^2 - 1)^2)

Then for the conversion sqrt(x^2) -> abs(x) you can use

sage: eq3.simplify_real()
sqrt(2)*abs(2*cos(x)^2 - 1)

Vincent

Paul Royik

unread,
Feb 27, 2015, 12:42:21 PM2/27/15
to sage-s...@googlegroups.com
Thank you!
Reply all
Reply to author
Forward
0 new messages