strange plot

81 views
Skip to first unread message

Fernando Gouvea

unread,
Sep 25, 2024, 10:42:24 AM9/25/24
to sage-support

Recently I was trying to get SageMath to plot something:

plot3d(floor(min(x,y)),(x,1,7),(y,1,7))

The result looks like this:

I checked, and it does seem that both floor(min(7,1)) and floor(min(1,7)) are equal to 1, as they should be. But that's not what the plot shows. Any explanations of what is happening?

Thanks,

Fernando


-- 
=============================================================
Fernando Q. Gouvea         http://www.colby.edu/~fqgouvea
Carter Professor of Mathematics
Dept. of Mathematics
Colby College              
5836 Mayflower Hill        
Waterville, ME 04901       

Time is nature's way of making sure that everything doesn't happen at once.

Dima Pasechnik

unread,
Sep 25, 2024, 11:21:41 AM9/25/24
to sage-s...@googlegroups.com
I can only say that Sage  isn't properly geared to plot non-smooth functions.
If you have such a piecewise-linear graph, it's probably better to split it into polyhedral pieces and plot them.



--
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 view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/91c0ba72-2526-4f17-a744-fcfe8d8befdf%40colby.edu.

julian...@gmail.com

unread,
Sep 25, 2024, 11:34:09 AM9/25/24
to sage-support
Hi Fernando,

I believe that problem is that:

sage: min(x, y)
x

In your call the min is evaluated once for the symbolic variables x and y and then the values of x and y (which does not show up anymore) are plugged in for the plot. I have no idea if that's how min is supposed to work. In any case, a workaround is to use:

sage: plot3d(lambda a, b: floor(min(a, b)),(x,1,7),(y,1,7))

Hope that helps,

julian

Nils Bruin

unread,
Sep 25, 2024, 12:52:00 PM9/25/24
to sage-support
On Wednesday 25 September 2024 at 08:34:09 UTC-7 julian...@gmail.com wrote:
Hi Fernando,

I believe that problem is that:

sage: min(x, y)
x

It may be less than ideal, but given that "min" is a built-in function which by the looks of it just picks the first element from its argument such that other elements are not strictly less than it, it is as documented.

There is min_symbolic for symbolic operation. If you care about performance, there may be a difference between using min_symbolic and using a "lambda" argument to plot. You'd need to try to see which one is faster for your application.

Fernando Gouvea

unread,
Sep 28, 2024, 3:05:52 PM9/28/24
to sage-s...@googlegroups.com

Thanks for the answers! Both

plot3d(floor(min_symbolic(x, y)),(x,1,7),(y,1,7))

and

plot3d(lambda a, b: floor(min(a, b)),(x,1,7),(y,1,7))

produce the right plot.

I wonder whether the result of ?min might mention the existence of min_symbolic, and similarly for max. When the first attempt failed, I looked at what ?min said, and didn't get much out of it. Had it mentioned "see also min_symbolic" or something of the kind, that would have been a clue.

Fernando

--
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.
-- 
=============================================================
Fernando Q. Gouvea         http://www.colby.edu/~fqgouvea
Carter Professor of Mathematics
Dept. of Mathematics
Colby College              
5836 Mayflower Hill        
Waterville, ME 04901       

To consider persons and events and situations only in the light of
their effect upon myself is to live on the doorstep of hell.
  -- Thomas Merton

Nils Bruin

unread,
Sep 29, 2024, 12:03:04 AM9/29/24
to sage-support
On Saturday 28 September 2024 at 12:05:52 UTC-7 fqgo...@colby.edu wrote:
I wonder whether the result of ?min might mention the existence of min_symbolic, and similarly for max. When the first attempt failed, I looked at what ?min said, and didn't get much out of it. Had it mentioned "see also min_symbolic" or something of the kind, that would have been a clue.

It can't: it's a python built-in and its docstring is the standard python-provided one. In that context there is no min_symbolic. A web search for "sage min function" does yield https://doc.sagemath.org/html/en/reference/functions/sage/functions/min_max.html as one of the top results, so such searches may be a work-around for the less-than-complete documentation we get from building on a general-purpose computer language.
Reply all
Reply to author
Forward
0 new messages