[sage-support] Re: cube roots
I like Jason's idea (specifically real_nth_root) as a method. However, to me the real issue is plotting. If someone tries to get a cube root of -1 and gets a complex number, at least they see there is an output! And then someone can help them understand why they get that answer. But there is a well-defined real plot for the cube root of x, with domain the whole real line, and Sage should somehow be able to better than what is in the plot documentation (from all our previous discussions): sage: plot(lambda x : RR(x).nth_root(3), (x,-1, 1)) I hesitate to say we should be able to globally import and have something like sage: plot(real_nth_root(x,3), (x,-1,1)) and afaict having semantics to check for every case out there like when plotting x^(2/3)+x^(1/3) would be ridiculous. Plus I'm not even sure Python/Sage allow us to "call" functions partly and then plot them, based on previous discussions here. But any idea for how to make this class of plots a little more straightforward (i.e. doable in one command without lambdas or semicolons) would be very helpful. - kcrisman --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Thu, May 14, 2009 at 12:34 PM, Jason Grout wrote: >> Bill Page wrote: >> Ok thanks. I recall the discussion and I can indeed write: >> >> sage: f=lambda x:RR(x).nth_root(3) >> sage: f(-2.0) >> -1.25992104989487 >> >> but I think I'll let my earlier comment stand: >> I think there should be a more obvious way. > > > Of course, you're welcome to suggest a way. Note that in earlier > threads, having a switch that determines which root to pick has > been negatively viewed. > -1 > What about changing the name of the above function to: > > RR(x).real_root(3) ? > > That would certainly be easier to find and would be a bit more > descriptive. Of course, right now, there is an nth_root function for > complex numbers that also would have to be addressed. > -0 > Or what about making real_root a method for any number (or > real_nth_root, or make nth_root take an argument for a target > domain, like RR or RDF)? > Perhaps we should continue this discussion on sage-devel if there is some reall interest in resolving this issue? What I personally would really prefer is that x^(1/3) call '.nth_root(3)' because (1/3) is an element of Rational Field. I suppose this requires some small change to the coercion system. Then x^(1.0/3.0) or equivalently x^RR(1/3) could continue to behave as it does now. Regards, Bill Page. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
Bill Page wrote: > On Thu, May 14, 2009 at 11:06 AM, Jason Grout wrote: >> Bill Page wrote: >>> Consider the problem to define >>> >>> f(x) = x^(1/3) >>> >>> so that it takes the real branch for x < 0. The best I have been able >>> to come up with so far is: >>> >>> sage: f = lambda x: >>> RealField(53)(x).sign()*(RealField(53)(x).sign()*x)^(1/3) >>> sage: plot(f,(-2,2)) >>> >> plot(lambda x: RR(x).nth_root(3), -5, 5, plot_points=20) >> >> This is from a mailing list discussion last year (Feb 2008?) on the same >> issue. In fact, there have been several discussions of this. Search >> sage-devel for "plotting cube roots", for example. >> > > Ok thanks. I recall the discussion and I can indeed write: > > sage: f=lambda x:RR(x).nth_root(3) > sage: f(-2.0) > -1.25992104989487 > > but I think I'll let my earlier comment stand: > >>> I think there should be a more obvious way. Of course, you're welcome to suggest a way. Note that in earlier threads, having a switch that determines which root to pick has been negatively viewed. What about changing the name of the above function to: RR(x).real_root(3) ? That would certainly be easier to find and would be a bit more descriptive. Of course, right now, there is an nth_root function for complex numbers that also would have to be addressed. Or what about making real_root a method for any number (or real_nth_root, or make nth_root take an argument for a target domain, like RR or RDF)? Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Thu, May 14, 2009 at 11:06 AM, Jason Grout wrote: > > Bill Page wrote: >> >> Consider the problem to define >> >> f(x) = x^(1/3) >> >> so that it takes the real branch for x < 0. The best I have been able >> to come up with so far is: >> >> sage: f = lambda x: RealField(53)(x).sign()*(RealField(53)(x).sign()*x)^(1/3) >> sage: plot(f,(-2,2)) >> > > plot(lambda x: RR(x).nth_root(3), -5, 5, plot_points=20) > > This is from a mailing list discussion last year (Feb 2008?) on the same > issue. In fact, there have been several discussions of this. Search > sage-devel for "plotting cube roots", for example. > Ok thanks. I recall the discussion and I can indeed write: sage: f=lambda x:RR(x).nth_root(3) sage: f(-2.0) -1.25992104989487 but I think I'll let my earlier comment stand: >> I think there should be a more obvious way. Regards, Bill Page. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
Bill Page wrote: > On Thu, May 14, 2009 at 4:59 AM, John Cremona wrote: >> This debate has been going on for as long as computers have been in >> existence. Yes, there is a case to be made the odd roots of negative >> reals should return a negative real instead of the "principal" complex >> root. But that leads to more subtle problems in other places. > > Granted. Choose your poison. > >> If all of mathematica, maple and matlab do the "non-obvious thing" >> there must be a good reason for it! > > There is but I think these reasons do not necessarily apply to Sage. > >> And as Mike said, you can always get the >> real root by inserting brackets. >> > > ??? > > Consider the problem to define > > f(x) = x^(1/3) > > so that it takes the real branch for x < 0. The best I have been able > to come up with so far is: > > sage: f = lambda x: RealField(53)(x).sign()*(RealField(53)(x).sign()*x)^(1/3) > sage: plot(f,(-2,2)) > plot(lambda x: RR(x).nth_root(3), -5, 5, plot_points=20) This is from a mailing list discussion last year (Feb 2008?) on the same issue. In fact, there have been several discussions of this. Search sage-devel for "plotting cube roots", for example. I thought the above plot was in the faq, but I can't find it now. Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Thu, May 14, 2009 at 4:59 AM, John Cremona wrote: > > This debate has been going on for as long as computers have been in > existence. Yes, there is a case to be made the odd roots of negative > reals should return a negative real instead of the "principal" complex > root. But that leads to more subtle problems in other places. Granted. Choose your poison. > If all of mathematica, maple and matlab do the "non-obvious thing" > there must be a good reason for it! There is but I think these reasons do not necessarily apply to Sage. > And as Mike said, you can always get the > real root by inserting brackets. > ??? Consider the problem to define f(x) = x^(1/3) so that it takes the real branch for x < 0. The best I have been able to come up with so far is: sage: f = lambda x: RealField(53)(x).sign()*(RealField(53)(x).sign()*x)^(1/3) sage: plot(f,(-2,2)) I think there should be a more obvious way. Regards, Bill Page. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Thu, May 14, 2009 at 1:56 AM, Robert Bradshaw wrote: > > On May 13, 2009, at 9:11 PM, Bill Page wrote: > >> On Wed, May 13, 2009 at 11:54 PM, Robert Bradshaw wrote: >>> >>> This is because the branch in which the positive real root is real is >>> taken. We're opting for continuity and consistency with complex >>> numbers. >>> >> >> If I wrote: >> >> sage: ComplexField(53)(-2.0)^(1/3) >> 0.629960524947437 + 1.09112363597172*I >> >> that looks ok to me, but >> >> sage: RealField(53)(-2.0)^(1/3) >> 0.629960524947437 + 1.09112363597172*I >> >> looks very strange. Could you explain the advantage? > > I can try :) > Thanks. I appreciate your willingness to re-hash this old subject. :-) > sage: a > -2.00 > sage: a^(1/3) > # what should happen here? > > The real field automatically promotes to complex in many instances > (e.g. sqrt, or all other non-integral powers or negative numbers), so > that's why I don't find it too strange. Also, it provides continuity > in the exponent: > > sage: [(-2.0)^a for a in [0..1, step=1/10]] > > [1.00, > 1.01931713553736 + 0.331196214043796*I, > 0.929316490603148 + 0.675187952399881*I, > 0.723648529606410 + 0.996016752925812*I, > 0.407750368641006 + 1.25492659684357*I, > 8.65956056235493e-17 + 1.41421356237309*I, > -0.468382177707358 + 1.44153211743623*I, > -0.954859959434831 + 1.31425198474794*I, > -1.40858040033850 + 1.02339356496073*I, > -1.77473421303888 + 0.576646101394740*I, > -2.00] > > I would find it odd if every other value here were real. > I would not find it odd and I guess in a way it is just a matter of taste. But 1/3 is an element of a Rational Field. It is not "naturally" continuous anyway. sage: b=1/3 sage: parent(b) Rational Field sage: (-2.0)^b ... On the other hand if I wrote: sage: b=1.0/3.0 sage: parent(b) Real Field with 53 bits of precision sage: (-2.0)^b 0.629960524947437 + 1.09112363597172*I I can explain this result as you indicate above. > Note that we're not the only ones doing this: > > sage: mathematica("(-2.0)^(1/3)") > 0.6299605249474367 + 1.0911236359717214*I > sage: maple("(-2.0)^(1/3);") > .6299605250+1.091123636*I > sage: matlab("(-2.0)^(1/3);") > 0.6300 + 1.0911i > sage: pari("(-2.0)^(1/3);") > 0.629960524947437 + 1.09112363597172*I > The difference is that none of these system have the notion of type (or parent). Regards, Bill Page. --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
This debate has been going on for as long as computers have been in existence. Yes, there is a case to be made the odd roots of negative reals should return a negative real instead of the "principal" complex root. But that leads to more subtle problems in other places. If all of mathematica, maple and matlab do the "non-obvious thing" there must be a good reason for it! And as Mike said, you can always get the real root by inserting brackets. John Cremona On May 14, 6:56 am, Robert Bradshaw wrote: > On May 13, 2009, at 9:11 PM, Bill Page wrote: > > > > > On Wed, May 13, 2009 at 11:54 PM, Robert Bradshaw wrote: > > >> This is because the branch in which the positive real root is real is > >> taken. We're opting for continuity and consistency with complex > >> numbers. > > > If I wrote: > > > sage: ComplexField(53)(-2.0)^(1/3) > > 0.629960524947437 + 1.09112363597172*I > > > that looks ok to me, but > > > sage: RealField(53)(-2.0)^(1/3) > > 0.629960524947437 + 1.09112363597172*I > > > looks very strange. Could you explain the advantage? > > I can try :) > > sage: a > -2.00 > sage: a^(1/3) > # what should happen here? > > The real field automatically promotes to complex in many instances > (e.g. sqrt, or all other non-integral powers or negative numbers), so > that's why I don't find it too strange. Also, it provides continuity > in the exponent: > > sage: [(-2.0)^a for a in [0..1, step=1/10]] > > [1.00, > 1.01931713553736 + 0.331196214043796*I, > 0.929316490603148 + 0.675187952399881*I, > 0.723648529606410 + 0.996016752925812*I, > 0.407750368641006 + 1.25492659684357*I, > 8.65956056235493e-17 + 1.41421356237309*I, > -0.468382177707358 + 1.44153211743623*I, > -0.954859959434831 + 1.31425198474794*I, > -1.40858040033850 + 1.02339356496073*I, > -1.77473421303888 + 0.576646101394740*I, > -2.00] > > I would find it odd if every other value here were real. > > Note that we're not the only ones doing this: > > sage: mathematica("(-2.0)^(1/3)") > 0.6299605249474367 + 1.0911236359717214*I > sage: maple("(-2.0)^(1/3);") > .6299605250+1.091123636*I > sage: matlab("(-2.0)^(1/3);") > 0.6300 + 1.0911i > sage: pari("(-2.0)^(1/3);") > 0.629960524947437 + 1.09112363597172*I > > - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On May 13, 2009, at 9:11 PM, Bill Page wrote: > On Wed, May 13, 2009 at 11:54 PM, Robert Bradshaw wrote: >> >> This is because the branch in which the positive real root is real is >> taken. We're opting for continuity and consistency with complex >> numbers. >> > > If I wrote: > > sage: ComplexField(53)(-2.0)^(1/3) > 0.629960524947437 + 1.09112363597172*I > > that looks ok to me, but > > sage: RealField(53)(-2.0)^(1/3) > 0.629960524947437 + 1.09112363597172*I > > looks very strange. Could you explain the advantage? I can try :) sage: a -2.00 sage: a^(1/3) # what should happen here? The real field automatically promotes to complex in many instances (e.g. sqrt, or all other non-integral powers or negative numbers), so that's why I don't find it too strange. Also, it provides continuity in the exponent: sage: [(-2.0)^a for a in [0..1, step=1/10]] [1.00, 1.01931713553736 + 0.331196214043796*I, 0.929316490603148 + 0.675187952399881*I, 0.723648529606410 + 0.996016752925812*I, 0.407750368641006 + 1.25492659684357*I, 8.65956056235493e-17 + 1.41421356237309*I, -0.468382177707358 + 1.44153211743623*I, -0.954859959434831 + 1.31425198474794*I, -1.40858040033850 + 1.02339356496073*I, -1.77473421303888 + 0.576646101394740*I, -2.00] I would find it odd if every other value here were real. Note that we're not the only ones doing this: sage: mathematica("(-2.0)^(1/3)") 0.6299605249474367 + 1.0911236359717214*I sage: maple("(-2.0)^(1/3);") .6299605250+1.091123636*I sage: matlab("(-2.0)^(1/3);") 0.6300 + 1.0911i sage: pari("(-2.0)^(1/3);") 0.629960524947437 + 1.09112363597172*I - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Wed, May 13, 2009 at 11:54 PM, Robert Bradshaw wrote: > > This is because the branch in which the positive real root is real is > taken. We're opting for continuity and consistency with complex numbers. > If I wrote: sage: ComplexField(53)(-2.0)^(1/3) 0.629960524947437 + 1.09112363597172*I that looks ok to me, but sage: RealField(53)(-2.0)^(1/3) 0.629960524947437 + 1.09112363597172*I looks very strange. Could you explain the advantage? --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On May 13, 2009, at 8:49 PM, Bill Page wrote: > On Wed, May 13, 2009 at 11:23 PM, Alex Ghitza wrote: >> >> On Thu, May 14, 2009 at 1:19 PM, Bill Page wrote: >>> >>> On Wed, May 13, 2009 at 10:46 PM, Mike Hansen wrote: On Wed, May 13, 2009 at 6:58 PM, Bill Page wrote: > > Can someone explain this apparently inconsistent result? It's just operator precedence: sage: -(2.0^(1/3)) -1.25992104989487 sage: (-2.0)^(1/3) 0.629960524947437 + 1.09112363597172*I >>> >>> Clear. Thanks. Why this particular root? >>> >> >> I believe that it is the real 3rd root of 2 times the "natural" >> primitive 6th root of 1, i.e. exp(2*pi*i/6). >> > > In most cases (e.g. plot) isn't the real root more desirable? This is because the branch in which the positive real root is real is taken. We're opting for continuity and consistency with complex numbers. - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Wed, May 13, 2009 at 11:23 PM, Alex Ghitza wrote: > > On Thu, May 14, 2009 at 1:19 PM, Bill Page wrote: >> >> On Wed, May 13, 2009 at 10:46 PM, Mike Hansen wrote: >>> >>> On Wed, May 13, 2009 at 6:58 PM, Bill Page wrote: Can someone explain this apparently inconsistent result? >>> >>> It's just operator precedence: >>> >>> sage: -(2.0^(1/3)) >>> -1.25992104989487 >>> sage: (-2.0)^(1/3) >>> 0.629960524947437 + 1.09112363597172*I >>> >> >> Clear. Thanks. Why this particular root? >> > > I believe that it is the real 3rd root of 2 times the "natural" > primitive 6th root of 1, i.e. exp(2*pi*i/6). > In most cases (e.g. plot) isn't the real root more desirable? --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Thu, May 14, 2009 at 1:19 PM, Bill Page wrote: > > On Wed, May 13, 2009 at 10:46 PM, Mike Hansen wrote: >> >> On Wed, May 13, 2009 at 6:58 PM, Bill Page wrote: >>> >>> Can someone explain this apparently inconsistent result? >> >> It's just operator precedence: >> >> sage: -(2.0^(1/3)) >> -1.25992104989487 >> sage: (-2.0)^(1/3) >> 0.629960524947437 + 1.09112363597172*I >> > > Clear. Thanks. Why this particular root? > I believe that it is the real 3rd root of 2 times the "natural" primitive 6th root of 1, i.e. exp(2*pi*i/6). -- Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne -- Australia -- http://www.ms.unimelb.edu.au/~aghitza/ --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Wed, May 13, 2009 at 10:46 PM, Mike Hansen wrote: > > On Wed, May 13, 2009 at 6:58 PM, Bill Page wrote: >> >> Can someone explain this apparently inconsistent result? > > It's just operator precedence: > > sage: -(2.0^(1/3)) > -1.25992104989487 > sage: (-2.0)^(1/3) > 0.629960524947437 + 1.09112363597172*I > Clear. Thanks. Why this particular root? --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: cube roots
On Wed, May 13, 2009 at 6:58 PM, Bill Page wrote: > > Can someone explain this apparently inconsistent result? It's just operator precedence: sage: -(2.0^(1/3)) -1.25992104989487 sage: (-2.0)^(1/3) 0.629960524947437 + 1.09112363597172*I --Mike --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---