sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.35k stars 460 forks source link

src/sage/plot/plot.py: random doctest failure #33129

Open tornaria opened 2 years ago

tornaria commented 2 years ago
sage -t --long --random-seed=314121851792717929490963296099377352180 src/sage/plot/plot.py
**********************************************************************
File "src/sage/plot/plot.py", line 1782, in sage.plot.plot.plot
Failed example:
    plot(f, (x, -3.5, 3.5), detect_poles='show', exclude=[-3..3], ymin=-5, ymax=5)
Expected:
    Graphics object consisting of 12 graphics primitives
Got:
    Graphics object consisting of 13 graphics primitives
**********************************************************************
1 item had failures:
   1 of 154 in sage.plot.plot.plot
    [461 tests, 1 failure, 60.91 s]
----------------------------------------------------------------------
sage -t --long --random-seed=314121851792717929490963296099377352180 src/sage/plot/plot.py  # 1 doctest failed
----------------------------------------------------------------------

Looks similar to #29954.

Component: doctest framework

Issue created by migration from https://trac.sagemath.org/ticket/33129

DaveWitteMorris commented 2 years ago
comment:1

The problem seems to be that the plot routine occasionally decides there is a pole at x = 0, so it fills the gap in the graph with a vertical dashed gray line. This is hard to see because the extra line is on top of the y-axis. To see the effect more easily, you can run the following code:

while True:
    f(x) = (floor(x)+0.5) / (1-(x-0.5)^2)
    P = plot(f(x - 1), (x, -2.5, 4.5), detect_poles='show',
                exclude=[-2..4], ymin=-5, ymax=5)
    if "13" in str(P):
        bad_plot = P
        break
show(bad_plot)

If your experience is the same as mine, the graph will include a vertical dashed line from (1, -2/3) to (1, 2/3). (I didn't do any statistics, but the bad plot seems to happen once every few hundred tries.)

So this doesn't seem to be the same problem as #29954. Instead, it seems we need to make detect_poles smarter.

fchapoton commented 2 years ago
comment:2

bump to 9.6

mwageringel commented 2 years ago
comment:3

Replying to @DaveWitteMorris:

Instead, it seems we need to make detect_poles smarter.

I agree. The routine for detecting poles computes the slope between consecutive plot points. In this example, the slope at the discontinuity is just very close to the threshold value, so sometimes a pole is detected there (I am a bit surprised this is not deterministic).

A simple attempt would be to increase the threshold a bit. Also, the detection determines the slope only in absolute terms, not relatively – probably it would be good to consider the y-range as well. For example, scaling your example by 10 always replicates the problem:

sage: f(x) = (floor(x)+0.5) / (1-(x-0.5)^2)
sage: plot(10*f(x - 1), (x, -2.5, 4.5), detect_poles='show', exclude=[-2..4], ymin=-50, ymax=50)
Launched png viewer for Graphics object consisting of 13 graphics primitives