-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmath.html
More file actions
126 lines (116 loc) · 7.78 KB
/
Copy pathmath.html
File metadata and controls
126 lines (116 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" --><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>VPython Help</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
<!-- InstanceEndEditable -->
</head>
<body>
<div id="wrapper">
<div id="leftmenu">
<p class="Normal"><a href="index.html"> Home</a></p>
<p class="Normal"> If you're new to Python <br />
and VPython: <a href="VisualIntro.html">Introduction</a></p>
<p class="Normal"> A VPython <a href="VPython_Intro.pdf" target="_blank">tutorial</a></p>
<p class="Normal"><a href="videos.html"> Introductory Videos</a></p>
<p class="Normal"><a href="primitives.html"> Pictures</a> of 3D objects</p>
<p> <select id="menu1" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu2" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu3" onChange="jumpMenu(this)"></select></p>
<p class="Normal"><a href="https://vpython.org" target="_blank"> VPython 7 web site</a><br />
<a href="license.txt" target="_blank"> VPython license</a><br />
<br />
</p>
</td>
</div>
<div id="content">
<!-- InstanceBeginEditable name="content" -->
<div>
<h1 class="Heading-1"> <font color="#0000A0">Math Functions</font></h1>
</div>
<div>
<p class="Normal"> 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.</p>
<div> </div>
</div>
<p class="Normal">The following math functions are provided:</p>
<p class="program">abs(x)<br />
sqrt(x)<br />
sin(x)<br />
cos(x)<br />
tan(x)<br />
asin(x) # arc sine<br />
acos(x) # arc cosine<br />
atan(x) # arc tangent; -pi/2 to pi/2<br />
atan2(y,x) # angle whose tangent is y/x; -pi to pi<br>
sqrt(x) # square root<br />
exp(x) # e to the x<br />
log(x) # natural log, base e<br />
# log(x)/log(10) gives log base 10<br />
pow(x,y) # x to the power y<br />
pi # 3.14159.... <br />
ceil(x) # round up to nearest integer<br />
floor(x) # round down to nearest integer<br />
sign(x) # +1 if x > 0, -1 if x < 0, 0 if x == 0<br />
round(x) # round to nearest integer <br />
max(x,y,z) # the largest of x,y,z<br />
min(x,y,z) # the smallest of x,y,z<br />
random() # pseudorandom number 0 to 1<br />
factorial(x) # x! = x*(x-1)*(x-2)....(1)<br />
combin(x,y) # x!/(y!*(x-y)!) <br />
max(a,b,c,..) # maximum of these<br />
min(a,b,c,..) # minimum of these</p>
<p class="Normal">Many of the quantities above are short forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc. </p>
<div>
<div></div>
</div>
<p class="Normal">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 <strong><a href="https://docs.python.org/3/library/random.html">documentation</a></strong> 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.</p>
<p class="Normal">There are functions for converting between degrees and
radians, where there are 2*pi radians in 360 degrees: </p>
<p class="program">radians(360) # equal to 2*pi<br />
degrees(2*pi) # equal to 360</p>
<p class="Normal">See JavaScript documentation on the <strong><a href="https://www.w3schools.com/jsref/jsref_obj_date.asp" target="_blank">Date</a></strong> object which provides methods for determining the current day etc. </p>
<p class="Normal">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:</p>
<p class="program">t1 = clock()<br />
sleep(1.5)<br />
t2 = clock()<br />
print(t2-t1) </p>
<p class="Normal">There is also a function msclock() which gives the time in milliseconds instead of seconds.</p>
<p class="Normal">You can <a href="MathJax.html" target="_blank"><strong>use LaTeX</strong></a> to display mathematical expressions on the page.</p>
<p class="Normal">Here are functions that are useful in statistical calculations:</p>
<div>
<div>
<p class="program"> print(factorial(4)) # gives 24 <br />
print(combin(10,2)) # gives 45 </p>
<p class="Normal"><span class="program">The factorial function factorial(N)
is N!; 4! is (4)(3)(2)(1) = 24, and 0! is defined to be 1.<br />
The combin function is combin(a,b) = a!/(b!*(a-b)!)</span>.</p>
<p class="Normal">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 <span class="attribute">factorial(5)</span> =
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). </p>
<p class="Normal">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 <span class="attribute">combin(5,2)</span> = 5!/(3!*2!) = 10.</p>
<p class="Normal">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, <span class="attribute">combin(5,2)</span> = 5!/(3!*2!)
= (5*4)/(2*1) = (2.5*4) = 10, and we didn't have to evaluate any of the factorials fully.</p>
<p class="Normal"> </p>
<p class="Normal"><br />
</p>
</div>
</div>
<!-- InstanceEndEditable -->
</div>
</div>
</body>
<script type="text/javascript" language="javascript" src="navigation.js"></script>
<!-- InstanceEnd --></html>