Error ! Need help

129 views
Skip to first unread message

Henri Girard

unread,
Feb 20, 2016, 7:07:33 AM2/20/16
to VPython-users
Hi,
I found this code on the net, but I can't make it working ?
Any help...
Henri

from __future__ import print_function, division
from vpython import *
from numpy import *
dx = 0.05
g = curve(x=arange(-10,10,dx), color=color.green)
scene.autoscale =0
wlth = 2.0
omega = pi
t = 0.0
dt = 0.005
while 1:
    rate(50)
    for pt in g.pos:
        pt[1]=2.5*cos(omega*t-2*pi*pt[0]/wlth)+2.5*cos(omega*t+2*pi*pt[0]/wlth)
t = t+dt
___________________________
TypeError: rate() takes at least 4 arguments (1 given)

Steve Spicklemire

unread,
Feb 20, 2016, 7:39:51 AM2/20/16
to vpytho...@googlegroups.com
You shouldn't need the numpy import. Try taking that out.

thanks,
-steve
--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henri Girard

unread,
Feb 20, 2016, 8:05:32 AM2/20/16
to VPython-users
Thanks :)
I often use it for arange() but I didn't know it could be stopping.

Steve Spicklemire

unread,
Feb 20, 2016, 10:19:24 AM2/20/16
to vpytho...@googlegroups.com, Steve Spicklemire
One difference between the older ‘visual’ package and the newer ‘vpython’ package is that the explicit use of numpy has been removed. You can still use numpy in vpython programs, but you need to import it and manage it yourself. If you want to use arange, I would recommend something like:

import numpy as np

myArray = np.arange(20) # or whatever

that way you won’t stomp on vpython functions (like “rate”) and others.

good luck!
-steve

Bruce Sherwood

unread,
Feb 20, 2016, 10:46:47 AM2/20/16
to vpytho...@googlegroups.com, Steve Spicklemire
Another way to get arange is to say "from numpy import arange". It's unfortunate that Python itself doesn't have a floating-point range-like option. (In GlowScript VPython, which doesn't have access to numpy, "range" is made equivalent to "arange".)

But there's a more important issue. You're trying to run a Classic VPython program in Jupyter VPython, whose syntax is somewhat different from Classic; it is that of GlowScript VPython. One of the significant differences is the syntax of the curve object, which does not support specifying positions in terms of numpy arrays. At glowscript.org click Help for detailed information about the syntax for VPython in both the Jupyter and GlowScript environments.

The fundamental reason why there had to be a change in curve (and some other areas of VPython) is that in both the Jupyter and GlowScript environments your own program is distant from the rendering of the scene. In the GlowScript case, information must be sent from the CPU to the GPU, and these two computers do not share memory. In the Jupyter case, information must be sent from the CPU running your program to the CPU running in the browser that manages the GlowScript graphics library, and then the information has to be sent to the GPU.

In a Classic curve object, your program could change the pos numpy array without VPython knowing you had made the change, and that worked because the array and the rendering functions shared the same memory. In the new situations it is out of the question to send the entire array to the renderer on every render. Rather, there is a set of powerful functions for modifying pos, and only the changes are sent to distant processes.

Bruce Sherwood

unread,
Feb 20, 2016, 11:29:29 AM2/20/16
to VPython-users, stevespi...@gmail.com
I should be a bit more precise about range and arange.

In Jupyter VPython, which of course uses true Python, you have to import arange from numpy in order to run "for" loops over floating-point quantities.

GlowScript VPython uses the RapydScript Python-to-JavaScript compiler, which is very similar to the true Python compiler but not exactly the same. For example, because RapydScript (and JavaScript) do not have access to numpy, the range function accepts floating-point values. In fact, JavaScript doesn't even distinguish between integer values and floating values. In GlowScript you can use "arange" or "range" and have the same results -- they are different names for the same thing. This means that a Jupyter VPython program that uses arange will run correctly at glowscript.org (after commenting out the numpy import statement). Similarly, a GlowScript VPython program that uses arange will run correctly in a Jupyter notebook (if one adds "from numpy import arange").

Bruce Sherwood

unread,
Feb 20, 2016, 11:31:55 AM2/20/16
to VPython-users, stevespi...@gmail.com
Perhaps a VPython notebook should not only execute "from math import *" but also "from numpy import arange"? That would simplify somewhat using the same program in either the Jupyter or GlowScript environments.

Steve Spicklemire

unread,
Feb 20, 2016, 11:36:51 AM2/20/16
to Bruce Sherwood, Steve Spicklemire, VPython-users
That would re-introduce a dependency on numpy. I’m not necessarily *opposed* to such a dependency, but it does add somewhat of a wrinkle. If you’re going to add arange, I would suggest that linspace is probably more useful in most circumstances.

-steve

Bruce Sherwood

unread,
Feb 20, 2016, 11:39:54 AM2/20/16
to Steve Spicklemire, VPython-users
Good point. linspace is indeed a better choice, though its name is utterly opaque.

Maybe "from numpy import linspace as range"?

Steve Spicklemire

unread,
Feb 20, 2016, 12:05:03 PM2/20/16
to Bruce Sherwood, Steve Spicklemire, VPython-users
I fear that would cause even more confusion since the arguments/semantics of arange and linspace are pretty different. ;-(

-steve

Bruce Sherwood

unread,
Feb 20, 2016, 12:40:41 PM2/20/16
to Steve Spicklemire, VPython-users

Yeah. Sigh.

Reply all
Reply to author
Forward
0 new messages