Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Introductory Videos

Pictures of 3D objects

Math Functions

For versions of Python prior to 3.0, Python performs integer division with truncation, so that 3/4 is 0, not 0.75. This is inconvenient when doing scientific computations, and can lead to hard-to-find bugs in programs. In the GlowScript environment, where VPython code is compiled to JavaScript, 3/4 is always 0.75.

The following math functions are provided:

abs(x)
sqrt(x)
sin(x)
cos(x)
tan(x)
asin(x)    # arc sine
acos(x)    # arc cosine
atan(x)    # arc tangent; -pi/2 to pi/2
atan2(y,x) # angle whose tangent is y/x; -pi to pi
sqrt(x)    # square root
exp(x)     # e to the x
log(x)     # natural log, base e
# log(x)/log(10) gives log base 10
pow(x,y)   # x to the power y
pi         # 3.14159....
ceil(x)    # round up to nearest integer
floor(x)   # round down to nearest integer
sign(x)    # +1 if x > 0, -1 if x < 0, 0 if x == 0
round(x)   # round to nearest integer
max(x,y,z) # the largest of x,y,z
min(x,y,z) # the smallest of x,y,z
random()   # pseudorandom number 0 to 1
factorial(x)  # x! = x*(x-1)*(x-2)....(1)
combin(x,y)   # x!/(y!*(x-y)!)
max(a,b,c,..) # maximum of these
min(a,b,c,..) # minimum of these

Many of the quantities above are short forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc.

In general, Python modules cannot be imported into VPython programs, because GlowScript VPython functions in a JavaScript environment. However, one can import the Python "random" module into a VPython program, provided by the RapydScript-NG tool that converts Python to JavaScript. One cannot use the form "from random import *" but must use "import random" or "import random as rr" (or other name) or "from random import randint, uniform" (or other list of functions). Here is documentation of the Python random module, however only seed_state, get_random_byte, seed, random, randrange, randint, uniform, choice, shuffle, and sample have been implemented in GlowScript. You do not need to import the random module in order to be able to use the random() function described above.

There are functions for converting between degrees and radians, where there are 2*pi radians in 360 degrees:

degrees(2*pi) # equal to 360

See JavaScript documentation on the Date object which provides methods for determining the current day etc.

Here is a way to determine elapsed time in seconds, with microsecond accuracy, using the clock() function that is also available in the standard Python "time" module. The displayed text is approximately 1.5:

t1 = clock()
sleep(1.5)
t2 = clock()
print(t2-t1)

There is also a function msclock() which gives the time in milliseconds instead of seconds.

You can use LaTeX to display mathematical expressions on the page.

Here are functions that are useful in statistical calculations:

print(factorial(4)) # gives 24
print(combin(10,2)) # gives 45

The factorial function factorial(N) is N!; 4! is (4)(3)(2)(1) = 24, and 0! is defined to be 1.
The combin function is combin(a,b) = a!/(b!*(a-b)!)
.

A major use of these functions is in calculating the number of ways of arranging a group of objects. For example, if there are 5 numbered balls in a sack, there are factorial(5) = 5! = 5*4*3*2*1 = 120 ways of taking them sequentially out of the sack (5 possibilities for the first ball, 4 for the next, and so on).

If on the other hand the 5 balls are not numbered, but 2 are green and 3 are red, of the 120 ways of picking the balls there are 2! indistinguishable ways of arranging the green balls and 3! ways of arranging the red balls, so the number of different arrangements of the balls is combin(5,2) = 5!/(3!*2!) = 10.

Logically, the combin function is just a combination of factorial functions. However, cancellations in the numerator and denominator make it possible to evaluate the combin function for values of its arguments that would overflow the factorial function, due to the limited size of floating-point numbers. For example, combin(5,2) = 5!/(3!*2!) = (5*4)/(2*1) = (2.5*4) = 10, and we didn't have to evaluate any of the factorials fully.