Ask Your Question
4

.canonicalize_radical() produces incorrect result

asked 2018-11-24 16:01:17 +0100

sagenovice gravatar image

updated 2018-11-24 16:08:48 +0100

I'm trying to simplify some trigonometric expressions using sage, and I noticed that .simplify_full() doesn't optimize those, unless a .canonicalize_radical() is used (thanks slelievre for the hint). But that yields incorrect results for some expressions. For example:

sage: y = sqrt(sin(x)^2 + 4*sin(x) + 4) - sqrt(sin(x)^2 - 4*sin(x) + 4)
sage: y.simplify_full()
sqrt(sin(x)^2 + 4*sin(x) + 4) - sqrt(sin(x)^2 - 4*sin(x) + 4)

.canonicalize_radical() simplifies it further:

sage: y.canonicalize_radical()
4

But that is wrong! The answer should be 2*sin(x). Obviously it selected an incorrect sign for the second sqrt(...).

Is there a way to make .canonicalize_radical() smarter? Or any other way to simplify an expression like this correctly?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2018-11-24 18:22:32 +0100

eric_g gravatar image

A solution is to use simplify_sqrt_real, which assumes that x lies in the real domain:

sage: y = sqrt(sin(x)^2 + 4*sin(x) + 4) - sqrt(sin(x)^2 - 4*sin(x) + 4)
sage: from sage.manifolds.utilities import simplify_sqrt_real
sage: simplify_sqrt_real(y)
2*sin(x)

More generally, you can use simplify_chain_real, which applies a chain of simplifications (including simplify_sqrt_real) valid in the real domain:

sage: from sage.manifolds.utilities import simplify_chain_real
sage: simplify_chain_real(y)
2*sin(x)
edit flag offensive delete link more

Comments

Thank you! I've experimented with simplify_chain_real a little, and sometimes it produces a result that's bigger and more complex than the source, but it's definitely a good thing to try! Thanks! Also, according to simplify_chain_real docs, it still uses canonicalize_radical() internally, however I could not reproduce any issues with simplify_chain_real yet. Is it considered "safe to use"? Or is it better to use simplify_sqrt_real and simplify_abs_trig and use simplify_chain_real only as a hint, or verify the result by hand?

sagenovice gravatar imagesagenovice ( 2018-11-25 17:53:20 +0100 )edit

I would say it is pretty safe to use simplify_chain_real since it is used systematically in calculus on real manifolds and performs not too badly. Among all these examples, the only one where it was not sufficient is the hyperbolic plane example. It turned out necessary to add (cf. cell [4])

maxima_calculus.eval("domain: real;")

to have some sqrt simplification be done correctly.

eric_g gravatar imageeric_g ( 2018-11-25 18:43:30 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-11-24 16:01:17 +0100

Seen: 596 times

Last updated: Nov 24 '18