Strange behaviour of `region_plot`

asked 2023-10-15 22:32:30 +0200

Matplotlib gravatar image

Basically, the region_plot primitive behaves strangely if the expression has half-integer exponents. More specifically, here is the setup I'm using:

u = var("u")
v = var("v")

unit_disc = u**2 + v**2 <= 1
uv_range = ((u, -1, 1), (v, -1, 1))

I am first defining a two-variables functions as follows, and trying to plot the associated implicit curve:

f = u + sqrt((1 - u**2 - v**2)**3)
show(f)
region_plot([f == 0, unit_disc], *uv_range)

This works nicely to draw the zero level of the function. (Note: I am using the region_plot primitive instead of the more suited implicit_plot one, because I want to restrict the plot to the unit disc.)

However, if I use a half-integer exponent, the primitive will throw the exception ValueError: negative number to a fractional power not real:

g = u + (1 - u**2 - v**2)**(3/2)
show(g)
region_plot([g == 0, unit_disc], *uv_range)

Did I discover a new bug?

The issue is that the way I define my function makes it so that I end up having those half-integer exponents lying everywhere, despite using only polynomials and substituing with square root terms. Sage automatically 'reduces' everything to half-integer exponents, and there is no way for me to revert this change.

How can I prevent this behaviour?

(P.S.: I wanted to include images and all, but even if I host them outside of this platform, it won't let me because karma... Why? This is not StackOverflow...)

edit retag flag offensive close merge delete

Comments

1

This works on my machine and it also works on sagecell.sagemath.org. What version of Sage are you using, and what platform?

John Palmieri gravatar imageJohn Palmieri ( 2023-10-15 23:23:26 +0200 )edit

You could also use the method "add_condition" to add the restriction to the unit disc on the full plot (this is for 3d plots).

FrédéricC gravatar imageFrédéricC ( 2023-10-16 08:25:03 +0200 )edit

I am currently running sagemath 9.5 on Ubuntu. What version do you use?

Matplotlib gravatar imageMatplotlib ( 2023-10-16 11:47:53 +0200 )edit

On the unit disc g can be replaced by g = u + abs (1 - u**2 - v**2)**(3/2) and this version should work on older versions of sage

achrzesz gravatar imageachrzesz ( 2023-10-16 14:27:04 +0200 )edit

I'm using the latest beta release. sagecell.sagemath.org is using version 10.1. Note that 9.5 was released in January 2022, so it's a bit old by now.

John Palmieri gravatar imageJohn Palmieri ( 2023-10-16 17:01:36 +0200 )edit