Ask Your Question

simplification errors in simple expressions

asked 2011-09-22 10:53:33 -0500

Dirk Danckaert gravatar image

updated 2013-11-06 10:37:30 -0500

tmonteil gravatar image

I'm currently testing the possibilities of SAGE as a teaching aid in a high school math course (in Belgium). I stumbled upon this:

  • When evaluating x/sqrt(x^2), SAGE answers $\frac{x}{|x|}$, as it should. Appending a .simpify()-instruction to the input does not change anything.
  • However, ((1-x^2)/sqrt(1-2*x^2+x^4)).simplify_full() evaluates to $-1$, in stead of $\frac{1-x^2}{|1-x^2|}$.

As an aside, it's definitely baffling that a behemoth program like SAGE is outdone in this respect by a one-floppy-disk, antique program called DERIVE.

As another aside, I still have to find a meaningful use for the instruction simplify(). Can someone provide me an expression that is actually simplified by simplify()?

Note added: I'm not sure I'm satisfied with mr. Fateman's answer (and I definitely disagree that defining sqrt(x^2)=|x| - for real x - entails +1=-1). But what I really want to know is this: is there a SAGE object that represents (reliably) the positive square root of a positive real number x? (Surely a quantity that is not without interest.) And how do I define it in SAGE?

edit retag flag offensive close merge delete



The issue in the second item seems to be that `(sqrt(1-2*x^2+x^4)).simplify_radical()` returns `x^2 - 1`. I'm not sure why, but it's calling maxima to do this computation.

John Palmieri gravatar imageJohn Palmieri ( 2011-09-22 18:04:49 -0500 )edit

Maxima's radcan apparently automatically chooses a branch. See below and the link I'm about to give.

kcrisman gravatar imagekcrisman ( 2011-09-23 04:31:37 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2011-09-23 04:15:25 -0500

Richard Fateman gravatar image

updated 2011-09-23 04:28:35 -0500

kcrisman gravatar image

x/sqrt(x^2) is NOT x/abs(x). There are two square roots of x^2, -x and +x. Neither one of them is abs(x).

If you proceed with the belief that the correct answer is abs(x), you can easily prove (for example) that 1=-1.

Because some people over the years have preferred the incorrect answer, Macsyma and then Maxima has incorporated heuristics that depend on the setting of a flag, radcan, and also depend on domain (e.g. real or complex) and on assumptions (e.g. assume(x>0)) to control the result in this case. Since Maxima (and apparently Sage) is not programmed to deal with multi-valued expressions, you can expect that the answer to such a simplification will be unsatisfactory to some people under some settings of some flags and some options.

Sage apparently has a command called "simplify_full" which calls something in Maxima, but since that command does not exist in Maxima, it is unclear what it is going to do. There are a host of commands in Maxima that do some sort of simplification, e.g. ratsimp, fullratsimp, trigsimp, poissimp, factor, expand, ratexpand, and others. They are controlled by various flags, too.

[Edit by kcrisman - the issue here is Sage's use of radcan().]

As for the general complaint that Sage should do something that DERIVE does -- (a) DERIVE is carefully crafted to match the expectations of a certain audience, and does very well at it. As long as the problems are not "too big".
(b) Sage is a behemoth program, but the authors have only a modest understanding of what is in it, at least with respect to Maxima. If your intention is "do high school math" maybe you should directly use Maxima, esp. wxmaxima. Accessing Maxima via Sage is clumsy. Like tying your shoes while wearing mittens.

There are other computer algebra systems (much simpler than Maxima) and maybe closer to DERIVE in spirit, that might do exactly what you want (high school algebra?) and not much more.

Good luck!

edit flag offensive delete link more


I'll upvote this, as it explains the problem well, even though most Sage (and I'd guess, most Maxima) users don't really understand the difference between a symbolic expression and a function. My surprise here was that radcan() was so aggressive in choosing a branch for radicals.

kcrisman gravatar imagekcrisman ( 2011-09-23 04:31:12 -0500 )edit

Note also that before calling radcan, Sage does `maxima.eval('domain: real$')`. This presumably has some effect?

John Palmieri gravatar imageJohn Palmieri ( 2011-09-23 05:43:26 -0500 )edit

Not in this case, though perhaps in general. I didn't attach the Maxima session here, but it's in the thread referenced in my response.

kcrisman gravatar imagekcrisman ( 2011-09-23 05:45:41 -0500 )edit

I'll refrain from bickering about the 'correct' definition of sqrt(x^2). But whichever definition is used in SAGE, it should be used consistently. If sqrt(x^2) is considered to have two values, then it can't be simplified. And neither can sqrt(1-2x^2+x^4). Simplifying the expression in my post to -1 surely is nonsense. In regard to your suggestion of using Maxima directly: that is no option. There's much much more to choosing a tool for high school then the definition that is used for sqrt(x). If I fall for SAGE, it's because of the notebook interface. I'll have to weigh that pro to the con of using something 'way too big'. I still hope I can hide the intricacies of SAGE to my students, while giving them the benefit of a tremendously useful interface.

Dirk Danckaert gravatar imageDirk Danckaert ( 2011-09-23 06:18:03 -0500 )edit

answered 2011-09-22 15:26:59 -0500

kcrisman gravatar image

updated 2011-09-25 16:22:48 -0500

The method simplify() sends things to Maxima and back. This is useful with assumptions.

sage: var('n')
sage: assume(n,'integer')
sage: sin(n*pi)
sage: sin(n*pi).simplify()

As for your comment, you have to "forget" assumptions about a variable to use other ones. One also has to tell Maxima that the domain is "real", not complex, for the x<0 assumption to take effect. This is somewhat troublesome, but doable.

sage: assume(x<0)
sage: maxima_calculus.eval('domain:real')
sage: sqrt(x^2).simplify()

As for the error, I've sent an email to the Maxima list about this. The thread starts here. Essentially, Maxima's radcan() picks a branch and sticks with it, rather than treating sqrt() as a function per se. But Fateman's answer above gives you what you need to know (even if the news is not so good).

edit flag offensive delete link more


I see. But it seems the use of simplify() is also lacking in consistency. E.g. "assume(x>0);simplify(sqrt(x^2))" returns x, (I dare not add 'as it should') but "assume(x<0);simplify(sqrt(x^2))" doesn't return -x. Meanwhile, I'm watching the Maxima thread.

Dirk Danckaert gravatar imageDirk Danckaert ( 2011-09-23 09:16:01 -0500 )edit

Yes, the "assumptions framework" in Maxima is described as relatively weak by the Maxima doc or other comments by the devs. However, it's better than anything else we have available in Sage. See also my edit to this answer.

kcrisman gravatar imagekcrisman ( 2011-09-25 16:18:51 -0500 )edit

It's beginning to make sense. But I knew about forget(), so that was not the issue. I didn't know about the 'domain:real' thing, though. Strangely, an experiment I just did (with the code you provided, but preceded with forget()) required .simplify_full()(which I knew already to do the job) to get the (-x) result. .simplify() on its own kept returning sqrt(x^2). Then after a few re-evaluations of the same cell (with minor edits of the code), all of a sudden .simplify() returned (-x) too. And kept doing so afterwards. A little bizarre.

Dirk Danckaert gravatar imageDirk Danckaert ( 2011-09-26 02:21:33 -0500 )edit

answered 2013-11-06 03:47:17 -0500

kcrisman gravatar image

Update: has been merged in Sage - which still doesn't give you exactly what you want, but perhaps is more to your liking:

sage: ((1-x^2)/sqrt(1-2*x^2+x^4)).simplify_full()
-(x^2 - 1)/sqrt(x^4 - 2*x^2 + 1)
edit flag offensive delete link more

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


Asked: 2011-09-22 10:53:33 -0500

Seen: 1,139 times

Last updated: Nov 06 '13