Skip to content

Commit

Permalink
gh-38181: sage.plot, sage.repl: Modularization fixes (imports), `…
Browse files Browse the repository at this point in the history
…# needs`

    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes #12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes #12345". -->

Also removing a use of `SR`, when testing for `Expression` is good
enough.

- Cherry-picked from #35095.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - #12345: short description why this is a dependency -->
<!-- - #34567: ... -->
    
URL: #38181
Reported by: Matthias Köppe
Reviewer(s): Kwankyu Lee, Matthias Köppe
  • Loading branch information
Release Manager committed Jun 16, 2024
2 parents c706cf8 + 5dfa06c commit e57b899
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/sage/plot/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,15 +2438,15 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
x_formatter, y_formatter = tick_formatter
from matplotlib.ticker import FuncFormatter, FixedFormatter
from sage.misc.latex import latex
from sage.symbolic.ring import SR
from sage.structure.element import Expression
from .misc import _multiple_of_constant
# ---------------------- Formatting x-ticks ----------------------
if x_formatter is None:
if scale[0] == 'log':
x_formatter = LogFormatterMathtext(base=base[0])
else:
x_formatter = ScalarFormatter()
elif x_formatter in SR:
elif isinstance(x_formatter, Expression):
x_const = x_formatter
x_formatter = FuncFormatter(lambda n, pos:
_multiple_of_constant(n, pos, x_const))
Expand Down Expand Up @@ -2475,7 +2475,7 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
y_formatter = LogFormatterMathtext(base=base[1])
else:
y_formatter = ScalarFormatter()
elif y_formatter in SR:
elif isinstance(y_formatter, Expression):
y_const = y_formatter
y_formatter = FuncFormatter(lambda n, pos:
_multiple_of_constant(n, pos, y_const))
Expand Down Expand Up @@ -2763,10 +2763,10 @@ def matplotlib(self, filename=None,
if stylesheet in plt.style.available:
plt.style.use(stylesheet)

from sage.symbolic.ring import SR
from sage.structure.element import Expression
# make sure both formatters typeset or both don't
if not isinstance(tick_formatter, (list, tuple)):
if tick_formatter == "latex" or tick_formatter in SR:
if tick_formatter == "latex" or isinstance(tick_formatter, Expression):
tick_formatter = (tick_formatter, "latex")
else:
tick_formatter = (tick_formatter, None)
Expand Down
16 changes: 9 additions & 7 deletions src/sage/plot/hyperbolic_regular_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
# https://www.gnu.org/licenses/
#*****************************************************************************

from sage.matrix.constructor import matrix
from sage.misc.decorators import options, rename_keyword
from sage.misc.functional import is_odd
from sage.misc.lazy_import import lazy_import
from sage.plot.hyperbolic_polygon import HyperbolicPolygon
from sage.plot.all import Graphics
from sage.plot.plot import Graphics
from sage.rings.cc import CC
from sage.rings.integer import Integer
from sage.misc.decorators import options, rename_keyword
from sage.symbolic.constants import pi, e
from sage.functions.hyperbolic import arccosh
from sage.functions.trig import sin, cos, cot
from sage.misc.functional import is_odd
from sage.matrix.constructor import matrix

lazy_import("sage.functions.hyperbolic", "arccosh")
lazy_import("sage.functions.trig", ["sin", "cos", "cot"])
lazy_import("sage.symbolic.constants", ["pi", "e"])


class HyperbolicRegularPolygon(HyperbolicPolygon):
Expand Down
15 changes: 8 additions & 7 deletions src/sage/plot/plot3d/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ def f(x,y): return math.exp(x/5)*math.cos(y)
# https://www.gnu.org/licenses/
# ****************************************************************************

from .tri_plot import TrianglePlot
from .index_face_set import IndexFaceSet
from .shapes import arrow3d
from .base import Graphics3dGroup
from sage.misc.lazy_import import lazy_import
from sage.misc.sageinspect import sage_getargspec, is_function_or_cython_function
from sage.plot.colors import rainbow
from .texture import Texture
from sage.plot.plot3d.base import Graphics3dGroup
from sage.plot.plot3d.index_face_set import IndexFaceSet
from sage.plot.plot3d.shapes import arrow3d
from sage.plot.plot3d.texture import Texture
from sage.plot.plot3d.tri_plot import TrianglePlot

from sage.functions.trig import cos, sin
from sage.misc.sageinspect import sage_getargspec, is_function_or_cython_function
lazy_import("sage.functions.trig", ["cos", "sin"])


class _Coordinates:
Expand Down
30 changes: 17 additions & 13 deletions src/sage/repl/display/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
contains a new facility for displaying lists of matrices in an easier
to read format::
sage: [identity_matrix(i) for i in range(2,5)]
sage: [identity_matrix(i) for i in range(2, 5)] # needs sage.modules
[
[1 0 0 0]
[1 0 0] [0 1 0 0]
Expand Down Expand Up @@ -236,19 +236,23 @@ def _ipython_float_precision_changed(change):
sage: shell.run_cell('matrix.options.precision') # indirect doctest # needs sage.modules
None
"""
from sage.matrix.constructor import options
s = change.new
if not s:
# unset the precision
options.precision = None
try:
from sage.matrix.constructor import options
except ImportError:
pass
else:
try:
prec = int(s)
if prec >= 0:
options.precision = prec
# otherwise ignore the change
except ValueError:
pass
s = change.new
if not s:
# unset the precision
options.precision = None
else:
try:
prec = int(s)
if prec >= 0:
options.precision = prec
# otherwise ignore the change
except ValueError:
pass


class SagePlainTextFormatter(PlainTextFormatter):
Expand Down
1 change: 1 addition & 0 deletions src/sage/repl/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# sage_setup: distribution = sagemath-repl
# sage.doctest: needs pillow
"""
Sage Wrapper for Bitmap Images
Expand Down
14 changes: 9 additions & 5 deletions src/sage/repl/ipython_kernel/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

from ipywidgets.widgets import SelectionSlider, ValueWidget, ToggleButtons
from ipywidgets.widgets.interaction import interactive, signature
from collections import OrderedDict
from collections.abc import Iterable, Iterator
from .widgets import EvalText, SageColorPicker
from sage.structure.element import parent

from ipywidgets.widgets import SelectionSlider, ValueWidget, ToggleButtons
from ipywidgets.widgets.interaction import interactive, signature

import sage.rings.abc

from sage.misc.lazy_import import lazy_import
from sage.structure.element import Matrix
from sage.repl.ipython_kernel.widgets import EvalText, SageColorPicker
from sage.structure.element import Matrix, parent

lazy_import("sage.plot.colors", "Color")


Expand Down Expand Up @@ -173,6 +176,7 @@ def widget_from_single_value(cls, abbrev, *args, **kwds):

return input_grid(abbrev.nrows(), abbrev.ncols(),
default=abbrev.list(), to_value=abbrev.parent())

if isinstance(abbrev, Color):
return SageColorPicker(value=abbrev.html_color())
# Get widget from IPython if possible
Expand Down
17 changes: 11 additions & 6 deletions src/sage/repl/ipython_kernel/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class TransformWidget():
sage: from ipywidgets import ToggleButtons
sage: from sage.repl.ipython_kernel.widgets import TransformWidget
sage: class TransformToggleButtons(TransformWidget, ToggleButtons): pass
sage: w = TransformToggleButtons(options=["pi", "e"], transform=lambda x: x+x)
sage: w = TransformToggleButtons(options=["pi", "e"], transform=lambda x: x + x)
sage: w
TransformToggleButtons(options=('pi', 'e'), value='pi')
sage: w.get_interact_value()
Expand Down Expand Up @@ -240,7 +240,8 @@ class TransformIntRangeSlider(TransformWidget, IntRangeSlider):
EXAMPLES::
sage: from sage.repl.ipython_kernel.widgets import TransformIntRangeSlider
sage: w = TransformIntRangeSlider(min=0, max=100, value=(7,9), transform=lambda x: x[1]-x[0])
sage: w = TransformIntRangeSlider(min=0, max=100, value=(7, 9),
....: transform=lambda x: x[1] - x[0])
sage: w
TransformIntRangeSlider(value=(7, 9))
sage: w.get_interact_value()
Expand All @@ -257,7 +258,8 @@ class TransformFloatRangeSlider(TransformWidget, FloatRangeSlider):
EXAMPLES::
sage: from sage.repl.ipython_kernel.widgets import TransformFloatRangeSlider
sage: w = TransformFloatRangeSlider(min=0, max=100, value=(7,9), transform=lambda x: x[1]-x[0])
sage: w = TransformFloatRangeSlider(min=0, max=100, value=(7, 9),
....: transform=lambda x: x[1] - x[0])
sage: w
TransformFloatRangeSlider(value=(7.0, 9.0))
sage: w.get_interact_value()
Expand All @@ -274,7 +276,7 @@ class TransformText(TransformWidget, Text):
EXAMPLES::
sage: from sage.repl.ipython_kernel.widgets import TransformText
sage: w = TransformText(value="hello", transform=lambda x: x+x)
sage: w = TransformText(value="hello", transform=lambda x: x + x)
sage: w
TransformText(value='hello')
sage: w.get_interact_value()
Expand All @@ -291,7 +293,7 @@ class TransformTextarea(TransformWidget, Textarea):
EXAMPLES::
sage: from sage.repl.ipython_kernel.widgets import TransformTextarea
sage: w = TransformTextarea(value="hello", transform=lambda x: x+x)
sage: w = TransformTextarea(value="hello", transform=lambda x: x + x)
sage: w
TransformTextarea(value='hello')
sage: w.get_interact_value()
Expand Down Expand Up @@ -371,7 +373,10 @@ class Grid(TransformWidget, HBox, ValueWidget):
sage: from sage.repl.ipython_kernel.widgets import Grid
sage: w = Grid(2, 2, lambda i,j: Text(value="%s,%s"%(i,j)))
sage: w
Grid(value=[['0,0', '0,1'], ['1,0', '1,1']], children=(Label(value=''), VBox(children=(Text(value='0,0'), Text(value='1,0'))), VBox(children=(Text(value='0,1'), Text(value='1,1')))))
Grid(value=[['0,0', '0,1'], ['1,0', '1,1']],
children=(Label(value=''),
VBox(children=(Text(value='0,0'), Text(value='1,0'))),
VBox(children=(Text(value='0,1'), Text(value='1,1')))))
sage: w.get_interact_value()
[['0,0', '0,1'], ['1,0', '1,1']]
"""
Expand Down
6 changes: 3 additions & 3 deletions src/sage/repl/preparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
4
sage: 87.factor()
3 * 29
sage: 15.10.sqrt()
sage: 15.10.sqrt() # needs sage.rings.real_mpfr
3.88587184554509
sage: preparse('87.sqrt()')
'Integer(87).sqrt()'
Expand All @@ -83,7 +83,7 @@
Symbolic functional notation::
sage: # needs sage.symbolic
sage: a=10; f(theta, beta) = theta + beta; b = x^2 + theta
sage: a = 10; f(theta, beta) = theta + beta; b = x^2 + theta
sage: f
(theta, beta) |--> beta + theta
sage: a
Expand Down Expand Up @@ -1032,7 +1032,7 @@ def parse_ellipsis(code, preparse_step=True):
'(ellipsis_range(1,2,Ellipsis,n))'
sage: parse_ellipsis("for i in (f(x) .. L[10]):")
'for i in (ellipsis_iter(f(x) ,Ellipsis, L[10])):'
sage: [1.0..2.0]
sage: [1.0..2.0] # needs sage.rings.real_mpfr
[1.00000000000000, 2.00000000000000]
TESTS:
Expand Down
5 changes: 3 additions & 2 deletions src/sage/repl/user_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ def initialize_globals(all, g=None):

def get_global(name):
"""
Return the value of global variable ``name``. Raise ``NameError``
if there is no such global variable.
Return the value of global variable ``name``.
Raise :class:`NameError` if there is no such global variable.
INPUT:
Expand Down

0 comments on commit e57b899

Please sign in to comment.