Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define x as symbolic variable in many animate examples #33264

Closed
umedoblock opened this issue Jan 31, 2022 · 20 comments
Closed

Define x as symbolic variable in many animate examples #33264

umedoblock opened this issue Jan 31, 2022 · 20 comments

Comments

@umedoblock
Copy link

The animated plots examples in the documentation at

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'

CC: @slel @mkoeppe

Component: graphics

Keywords: SR.var('x')

Author: Michael Orlitzky

Branch/Commit: e73cd12

Reviewer: Samuel Lelièvre

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

@umedoblock umedoblock added this to the sage-9.6 milestone Jan 31, 2022
@slel
Copy link
Member

slel commented Feb 1, 2022

comment:1

Did you redefine x before that?

Try running reset('x') and running the examples again.

@slel
Copy link
Member

slel commented Feb 1, 2022

comment:2

All the examples work fine for me in a fresh Sage session

  • using the Sage-macOS 9.5 prerelease
  • or using Sage 9.5 final, built from source

If I first redefine x as a function, e.g.

sage: x = lambda t: t + 1

then the failures in the ticket description arise.

Resetting x to its starting value with

sage: reset('x')

solves the issues, and the examples work fine again.

@slel
Copy link
Member

slel commented Feb 1, 2022

comment:3

This seems like user error rather than a defect in Sage.

Propose to close as invalid.

@slel slel removed this from the sage-9.6 milestone Feb 1, 2022
@umedoblock
Copy link
Author

comment:4

Hello slelievre.

You said "redefine x" means below sentence ?

x = lambda t: cos(t)

If it's correct, I redefined.

I tried running reset('x') and running the examples again.

Then TypeError disappeared.

Thanks slelievre.

I'm a beginner of sagemath, so I have no idea how to recovery from TypeError.

And I couldn't run below sentence.

reset('x')

I believe that all of the sagemath beginner cannot recovery from TypeError.

Please change document of "make_image(frame, filename, **kwds)" in Animated plots.

EXAMPLES:

from sage.plot.animate import Animation
class MyAnimation(Animation):
   def make_image(self, frame, filename, **kwds):
       P = parametric_plot(frame[0], frame[1], **frame[2])
       P.save_image(filename,**kwds)

t = var('t')

vvvvvvvvvvvvvvvvvvvv
x = lambda t: cos(t)
^^^^^^^^^^^^^^^^^^^^

y = lambda n,t: sin(t)/n
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)])

d = B.png(); v = os.listdir(d); v.sort(); v  # long time
['00000000.png', '00000001.png', '00000002.png', '00000003.png']
B.show()  # not tested

class MyAnimation(Animation):
   def make_image(self, frame, filename, **kwds):
       G = frame.plot()

G.set_axes_range(floor(G.xmin()),ceil(G.xmax()),floor(G.ymin()),ceil(G.ymax()))
       G.save_image(filename, **kwds)

B = MyAnimation([graphs.CompleteGraph(n) for n in range(7,11)], figsize=5)
d = B.png()
v = os.listdir(d); v.sort(); v
['00000000.png', '00000001.png', '00000002.png', '00000003.png']
B.show()  # not tested

@orlitzky
Copy link
Contributor

orlitzky commented Feb 2, 2022

comment:5

In gh-umedoblock's defense, our examples do rely on some implicit magic, the symbolic variable x being automatically defined whenever sage starts or the doctest environment resets. I would much rather see us begin them all with x = SR.var("x") so that they're truly copy/paste-able, but I expect I'd be pelted with tomatoes if I proposed it.

@slel
Copy link
Member

slel commented Feb 2, 2022

comment:6

Replying to @orlitzky:

In gh-umedoblock's defense, our examples do rely on some implicit magic, the symbolic variable x being automatically defined whenever sage starts or the doctest environment resets. I would much rather see us begin them all with x = SR.var("x") so that they're truly copy/paste-able [...]

Huge +1 from me.

@orlitzky
Copy link
Contributor

orlitzky commented Feb 2, 2022

comment:7

It looks like maybe there's a reason to propose this that won't make me look like a busybody: #32721

@umedoblock
Copy link
Author

comment:8

Do you irritate me ?

Sorry.

I insert reset('x') under MyAnimation()

And animate() doesn't raise TypeError.

So please insert reset('x') under MyAnimation().

from sage.plot.animate import Animation
class MyAnimation(Animation):
    def make_image(self, frame, filename, **kwds):
        P = parametric_plot(frame[0], frame[1], **frame[2])
        P.save_image(filename,**kwds)

t = var('t')
x = lambda t: cos(t)
y = lambda n,t: sin(t)/n
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)])

vvvvvvvvvv
reset('x')
^^^^^^^^^^

d = B.png(); v = os.listdir(d); v.sort(); v  # long time
B.show()  # not tested

a = animate([plot(x^2 + n) for n in range(4)], ymin=0, ymax=4)
d = a.png(); v = os.listdir(d); v.sort(); v  # long time

@slel
Copy link
Member

slel commented Feb 2, 2022

comment:9

Instead of reset('x'), you can also use x = SR.var('x').

I agree many documentation examples would be improved
if they started with that.

@slel slel added this to the sage-9.6 milestone Feb 2, 2022
@umedoblock
Copy link
Author

comment:10

Improved.

And works it.

Thanks slelieve and mjo.

from sage.plot.animate import Animation
class MyAnimation(Animation):
    def make_image(self, frame, filename, **kwds):
        P = parametric_plot(frame[0], frame[1], **frame[2])
        P.save_image(filename,**kwds)

t = var('t')
x = lambda t: cos(t)
y = lambda n,t: sin(t)/n
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)])

vvvvvvvvvvvvvvv
x = SR.var('x')
^^^^^^^^^^^^^^^

d = B.png(); v = os.listdir(d); v.sort(); v  # long time
B.show()  # not tested

a = animate([plot(x^2 + n) for n in range(4)], ymin=0, ymax=4)
d = a.png(); v = os.listdir(d); v.sort(); v  # long time

@orlitzky
Copy link
Contributor

orlitzky commented Feb 3, 2022

Commit: e73cd12

@orlitzky
Copy link
Contributor

orlitzky commented Feb 3, 2022

comment:11

One down, a million to go.


New commits:

e73cd12Trac #33264: add "x = SR.var('x')" to several animation examples.

@orlitzky
Copy link
Contributor

orlitzky commented Feb 3, 2022

Branch: u/mjo/ticket/33264

@orlitzky
Copy link
Contributor

orlitzky commented Feb 3, 2022

Author: Michael Orlitzky

@slel

This comment has been minimized.

@slel
Copy link
Member

slel commented Feb 4, 2022

comment:13

That will help!

@slel
Copy link
Member

slel commented Feb 4, 2022

Reviewer: Samuel Lelièvre

@slel
Copy link
Member

slel commented Feb 4, 2022

Changed keywords from none to SR.var('x')

@slel

This comment has been minimized.

@slel slel changed the title some TypeError occurred in animate() Define x as symbolic variable in many animate examples Feb 4, 2022
@vbraun
Copy link
Member

vbraun commented Feb 16, 2022

Changed branch from u/mjo/ticket/33264 to e73cd12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants