sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.21k stars 421 forks source link

Graph plotting has rounding issue(s) #35905

Open EmmanuelCharpentier opened 1 year ago

EmmanuelCharpentier commented 1 year ago

Is there an existing issue for this?

Did you read the documentation and troubleshoot guide?

Environment

- **OS**: Debian testing running on core i7 + 16 GB RAM
- **Sage Version**: 10.1.beta5

Steps To Reproduce

Graph plotting logic seems to have issues with "marginally inexact" coordinates for vertex positions, best illustrated by an example :

sage: reset()
sage: set_random_seed(0)
sage: g=graphs.CubeGraph(2)
sage: gs=[u for u in g.connected_subgraph_iterator()]
sage: gs[11].get_pos()
{'01': (6.123233995736766e-17, 1.0), '00': (0.0, 0.0)}

[ The curious numerical values of the position coordinates are questionable, but not the subject of this issue ].

Expected Behavior

sage:  h=copy(gs[11])
sage:  h.set_pos({t:tuple(map(round, h.get_pos()[t])) for t in h.get_pos().keys()})
sage: h.get_pos()
{'01': (0.0, 1.0), '00': (0.0, 0.0)}

sage: h.plot()
Launched png viewer for Graphics object consisting of 4 graphics primitives

displays the subgraph (two vertices and an edge)

tmp_8l1w5gtq

Actual Behavior

sage: gs[11].plot()
Launched png viewer for Graphics object consisting of 4 graphics primitives

displays two vertices without an edge :

tmp_ti33t7vg

Additional Information

Original finding : see ask.sagemath.org.

dcoudert commented 6 months ago

A solution is to stop using from math import sin, cos, pi and converting positions to floats. We can use instead from sage.functions.trig import sin, cos and from sage.symbolic.all import pi. The generation of the graph will be slightly slower but it should solve several issues (plot, curious numerical values, etc.).

This may however require to use the rational field for fractions, etc. See for instance graphs.GolombGraph?? in file src/sage/graphs/generators/basic.py. This is a significant amount of work but we might gain stability and avoid weird plotting issues.