Skip to content

Commit

Permalink
Trac #33264: Define x as symbolic variable in many animate examples
Browse files Browse the repository at this point in the history
The animated plots examples in the documentation at

- https://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.
html

include an example where `x = lambda t: cos(t)`
and `y = ...` are used for a parametric plot.

Many subsequent examples fail with a type error
if run without resetting `x` to its default value,
either with `reset('x')` or with `x = SR.var('x')`.

{{{
sage: a = animate([plot(x^2 + n) for n in range(4)], ymin=0, ymax=4)
------------------------------------------------------------------------
---
TypeError                                 Traceback (most recent call
last)
<ipython-input-...-...> in <module>
----> 1 a = animate([plot(x**Integer(2) + n) for n in ...], ymin=...,
ymax=...)

<ipython-input-...-...> in <listcomp>(.0)
----> 1 a = animate([plot(x**Integer(2) + n) for n in ...], ymin=...,
ymax=...)

.../site-packages/sage/rings/integer.pyx in
sage.rings.integer.Integer.__pow__
(build/cythonized/sage/rings/integer.c:14496)()
   2153             return coercion_model.bin_op(left, right,
operator.pow)
   2154         # left is a non-Element: do the powering with a Python
int
-> 2155         return left ** int(right)
   2156
   2157     cpdef _pow_(self, other):

TypeError: unsupported operand type(s) for ** or pow(): 'function' and
'int'
}}}

{{{
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....:             xmin=0, xmax=2*pi, ymin=-1, ymax=1, figsize=[2,1])
------------------------------------------------------------------------
---
TypeError                                 Traceback (most recent call
last)
<ipython-input-...> in <module>
----> 1 a = animate([sin(x + float(k)) for ...],
      2             xmin=..., xmax=..., ymin=..., ymax=..., figsize=...)

<ipython-input-...> in <listcomp>(.0)
----> 1 a = animate([sin(x + float(k)) for ...],
      2             xmin=..., xmax=..., ymin=..., ymax=..., figsize=...)

TypeError: unsupported operand type(s) for +: 'function' and 'float'
}}}

{{{
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....:                xmin=0, xmax=2*pi, figsize=[2,1])
....:
------------------------------------------------------------------------
---
TypeError                                 Traceback (most recent call
last)
<ipython-input-...> in <module>
----> 1 a = animate([sin(x + float(k)) for ...],
      2                xmin=..., xmax=..., figsize=...)

<ipython-input-...> in <listcomp>(.0)
----> 1 a = animate([sin(x + float(k)) for ...],
      2                xmin=..., xmax=..., figsize=...)

TypeError: unsupported operand type(s) for +: 'function' and 'float'
}}}

URL: https://trac.sagemath.org/33264
Reported by: gh-umedoblock
Ticket author(s): Michael Orlitzky
Reviewer(s): Samuel Lelièvre
  • Loading branch information
Release Manager committed Feb 14, 2022
2 parents 5bdf5dd + e73cd12 commit 2e1f8d2
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/sage/plot/animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
The sine function::
sage: x = SR.var("x")
sage: sines = [plot(c*sin(x), (-2*pi,2*pi), color=Color(c,0,0), ymin=-1, ymax=1) for c in sxrange(0,1,.2)]
sage: a = animate(sines)
sage: a # optional -- ImageMagick
Expand Down Expand Up @@ -61,8 +62,7 @@
Animations of 3d objects::
sage: var('s,t')
(s, t)
sage: s,t = SR.var("s,t")
sage: def sphere_and_plane(x):
....: return sphere((0,0,0),1,color='red',opacity=.5)+parametric_plot3d([t,x,s],(s,-1,1),(t,-1,1),color='green',opacity=.7)
sage: sp = animate([sphere_and_plane(x) for x in sxrange(-1,1,.3)])
Expand All @@ -72,7 +72,7 @@
Graphics3d Object
sage: sp.show() # optional -- ImageMagick
sage: (x,y,z) = var('x,y,z')
sage: (x,y,z) = SR.var("x,y,z")
sage: def frame(t):
....: return implicit_plot3d((x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5], region=lambda x,y,z: x<=t or y>=t or z<=t)
sage: a = animate([frame(t) for t in srange(.01,1.5,.2)])
Expand All @@ -85,7 +85,7 @@
method :meth:`sage.plot.animate.Animation.make_image`. This is
illustrated by the following example::
sage: t = var('t')
sage: t = SR.var("t")
sage: a = animate((sin(c*pi*t) for c in sxrange(1,2,.2)))
sage: a.show() # optional -- ImageMagick
Expand Down Expand Up @@ -133,7 +133,7 @@ def animate(frames, **kwds):
EXAMPLES::
sage: t = var('t')
sage: t = SR.var("t")
sage: a = animate((cos(c*pi*t) for c in sxrange(1,2,.2)))
sage: a.show() # optional -- ImageMagick
Expand Down Expand Up @@ -161,6 +161,7 @@ class Animation(WithEqualityById, SageObject):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.3)],
....: xmin=0, xmax=2*pi, figsize=[2,1])
sage: a # optional -- ImageMagick
Expand Down Expand Up @@ -200,13 +201,15 @@ class Animation(WithEqualityById, SageObject):
We check that :trac:`7981` is fixed::
sage: x = SR.var("x")
sage: a = animate([plot(sin(x + float(k)), (0, 2*pi), ymin=-5, ymax=5)
....: for k in srange(0,2*pi,0.3)])
sage: a.show() # optional -- ImageMagick
Do not convert input iterator to a list, but ensure that
the frame count is known after rendering the frames::
sage: x = SR.var("x")
sage: a = animate((plot(x^p, (x,0,2)) for p in sxrange(1,2,.1)))
sage: str(a)
'Animation with unknown number of frames'
Expand All @@ -229,6 +232,7 @@ def __init__(self, v=None, **kwds):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.3)],
....: xmin=0, xmax=2*pi, figsize=[2,1]) # indirect doctest
sage: a # optional -- ImageMagick
Expand Down Expand Up @@ -404,7 +408,7 @@ def make_image(self, frame, filename, **kwds):
....: P = parametric_plot(frame[0], frame[1], **frame[2])
....: P.save_image(filename,**kwds)
sage: t = var('t')
sage: t = SR.var("t")
sage: x = lambda t: cos(t)
sage: y = lambda n,t: sin(t)/n
sage: B = MyAnimation([([x(t), y(i+1,t)],(t,0,1), {'color':Color((1,0,i/4)), 'aspect_ratio':1, 'ymax':1}) for i in range(4)])
Expand Down Expand Up @@ -451,6 +455,7 @@ def png(self, dir=None):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([plot(x^2 + n) for n in range(4)], ymin=0, ymax=4)
sage: d = a.png(); v = os.listdir(d); v.sort(); v # long time
['00000000.png', '00000001.png', '00000002.png', '00000003.png']
Expand Down Expand Up @@ -507,7 +512,7 @@ def graphics_array(self, ncols=3):
Frames can be specified as a generator too; it is internally converted to a list::
sage: t = var('t')
sage: t = SR.var("t")
sage: b = animate((plot(sin(c*pi*t)) for c in sxrange(1,2,.2)))
sage: g = b.graphics_array()
sage: g
Expand Down Expand Up @@ -557,6 +562,7 @@ def gif(self, delay=20, savefile=None, iterations=0, show_path=False,
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, ymin=-1, ymax=1, figsize=[2,1])
sage: td = tmp_dir()
Expand Down Expand Up @@ -633,6 +639,7 @@ def _gif_from_imagemagick(self, savefile=None, show_path=False,
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, ymin=-1, ymax=1, figsize=[2,1])
sage: td = tmp_dir()
Expand Down Expand Up @@ -692,6 +699,7 @@ def _rich_repr_(self, display_manager, **kwds):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([plot(x^2 + n) for n in range(4)], ymin=0, ymax=4)
sage: from sage.repl.rich_output import get_display_manager
sage: dm = get_display_manager()
Expand Down Expand Up @@ -784,6 +792,7 @@ def show(self, delay=None, iterations=None, **kwds):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, figsize=[2,1])
sage: a.show() # optional -- ImageMagick
Expand Down Expand Up @@ -896,6 +905,7 @@ def ffmpeg(self, savefile=None, show_path=False, output_format=None,
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, ymin=-1, ymax=1, figsize=[2,1])
sage: td = tmp_dir()
Expand Down Expand Up @@ -1014,6 +1024,7 @@ def apng(self, savefile=None, show_path=False, delay=20, iterations=0):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, figsize=[2,1])
sage: dir = tmp_dir()
Expand Down Expand Up @@ -1075,6 +1086,7 @@ def save(self, filename=None, show_path=False, use_ffmpeg=False, **kwds):
EXAMPLES::
sage: x = SR.var("x")
sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, ymin=-1, ymax=1, figsize=[2,1])
sage: td = tmp_dir()
Expand Down Expand Up @@ -1153,6 +1165,7 @@ def interactive(self, **kwds):
EXAMPLES::
sage: x = SR.var("x")
sage: frames = [point3d((sin(x), cos(x), x)) for x in (0, pi/16, .., 2*pi)]
sage: animate(frames).interactive(online=True)
Graphics3d Object
Expand Down Expand Up @@ -1218,6 +1231,7 @@ class APngAssembler(object):
EXAMPLES::
sage: from sage.plot.animate import APngAssembler
sage: x = SR.var("x")
sage: def assembleAPNG():
....: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
....: xmin=0, xmax=2*pi, figsize=[2,1])
Expand Down

0 comments on commit 2e1f8d2

Please sign in to comment.