Skip to content

Commit 64c0e7a

Browse files
committed
Implement factorial and combin functions; minor corrections to graph documentation
1 parent 5fbe31a commit 64c0e7a

File tree

8 files changed

+66
-28
lines changed

8 files changed

+66
-28
lines changed

docs/GlowScriptDocs/factorial.html

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ <h1 class="Heading-1"> <font color="#0000A0">The factorial and combin Functions<
3737
</div>
3838
<div>
3939
<div>
40-
<p class="program"> from visual import *<br />
41-
from visual.factorial import * # import after visual<br />
42-
print(factorial(4)) # gives 24 <br />
43-
print(combin(10,2)) # gives 45</p>
44-
<p class="Normal"> </p>
45-
<p class="Normal"><em>Note: To avoid confusion between the module named &quot;factorial&quot;
46-
and the function named &quot;factorial&quot;, import the factorial module <strong>after</strong> importing the visual module itself.</em></p>
40+
<p class="program"> print(factorial(4)) # gives 24 <br />
41+
print(combin(10,2)) # gives 45 </p>
4742
<p class="Normal"><span class="program">The factorial function factorial(N)
4843
is N!; 4! is (4)(3)(2)(1) = 24, and 0! is defined to be 1.<br />
4944
The combin function is combin(a,b) = a!/(b!*(a-b)!)</span>.</p>
@@ -69,7 +64,7 @@ <h1 class="Heading-1"> <font color="#0000A0">The factorial and combin Functions<
6964

7065
numbers. For example, <span class="attribute">combin(5,2)</span> = 5!/(3!*2!)
7166

72-
= (5*4)/2 = 10, and we didn't have to evaluate 5! fully.</p>
67+
= (5*4)/(2*1) = (2.5*4) = 10, and we didn't have to evaluate any of the factorials fully..</p>
7368
</div>
7469
</div>
7570
<div> </div>

docs/GlowScriptDocs/math.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ <h1 class="Heading-1"> <font color="#0000A0">Math Functions</font></h1>
5656
max(x,y,z) # the largest of x,y,z<br />
5757
min(x,y,z) # the smallest of x,y,z<br />
5858
random() &nbsp;&nbsp;# pseudorandom number 0 to 1<br />
59-
ceil(x) &nbsp;# integer closest to x, &lt;=x<br />
60-
floor(x) # integer closest to x, &gt;=x<br />
61-
round(x) # integer closest to x <br />
59+
ceil(x) &nbsp;&nbsp;&nbsp;# integer closest to x, &lt;=x<br />
60+
floor(x) &nbsp;&nbsp;# integer closest to x, &gt;=x<br />
61+
round(x) &nbsp;&nbsp;# integer closest to x<br />
62+
factorial(x) &nbsp;# x! = x*(x-1)*(x-2)....(1)<br />
63+
combin(x,y) &nbsp;&nbsp;# x!/(y!*(x-y)!)<br />
6264
max(a,b,c,..) # maximum of these<br />
6365
min(a,b,c,..) # minimum of these</p>
64-
<p class="Normal">The quantities above are simplified forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc.</p>
66+
<p class="Normal">Many of the quantities above are simplified forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc. <strong><a href="factorial.html">Here are details</a></strong> of the factorial and combin functions.</p>
6567
<p class="Normal">There are functions for converting between degrees and
6668
radians, where there are 2*pi radians in 360 degrees: </p>
6769
<p class="program">radians(360) &nbsp;# equal to 2*pi<br />

docs/GlowScriptDocs/navigation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ s += '<option value="math.html">Math Functions</option>'
3838
s += '<option value="vector.html">Vector Operations </option>'
3939
s += '<option value="MathJax.html">LaTeX Math Displays</option>'
4040
s += '<option value="files.html">Libraries &amp; Files</option>'
41+
s += '<option value="factorial.html">factorial/combin</option>'
4142
document.getElementById("menu2").innerHTML = s
4243

4344
s = ''

docs/VPythonDocs/factorial.html

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,8 @@ <h1 class="Heading-1"> <font color="#0000A0">The factorial and combin Functions<
4545
</div>
4646
<div>
4747
<div>
48-
<p class="program"> from visual import *<br />
49-
from visual.factorial import * # import after visual<br />
50-
print(factorial(4)) # gives 24 <br />
51-
print(combin(10,2)) # gives 45</p>
52-
<p class="Normal"> </p>
53-
<p class="Normal"><em>Note: To avoid confusion between the module named &quot;factorial&quot;
54-
and the function named &quot;factorial&quot;, import the factorial module <strong>after</strong> importing the visual module itself.</em></p>
55-
<p class="Normal">&nbsp;</p>
56-
<p class="Normal">This is not yet available in GlowScript.</p>
57-
<p class="Normal">&nbsp;</p>
48+
<p class="program"> print(factorial(4)) # gives 24 <br />
49+
print(combin(10,2)) # gives 45 </p>
5850
<p class="Normal"><span class="program">The factorial function factorial(N)
5951
is N!; 4! is (4)(3)(2)(1) = 24, and 0! is defined to be 1.<br />
6052
The combin function is combin(a,b) = a!/(b!*(a-b)!)</span>.</p>
@@ -80,7 +72,7 @@ <h1 class="Heading-1"> <font color="#0000A0">The factorial and combin Functions<
8072

8173
numbers. For example, <span class="attribute">combin(5,2)</span> = 5!/(3!*2!)
8274

83-
= (5*4)/2 = 10, and we didn't have to evaluate 5! fully.</p>
75+
= (5*4)/(2*1) = (2.5*4) = 10, and we didn't have to evaluate any of the factorials fully.</p>
8476
</div>
8577
</div>
8678
<div> </div>

docs/VPythonDocs/graph.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@
7272
<p class="program"> f1.plot(pos=(100,-30)) # add a single point<br />
7373
f1.plot(pos=[(100,-30),(20,50),(0,-10)]) # add a list<br />
7474
</p>
75-
<p class="Normal">When you add points, you can optionally specify a color for these points that is different from the default color of the object:</p>
76-
<p class="program"> f1.plot(pos=(100,-30), color=color.red)</p>
75+
<p class="Normal">In classic VPython, when you add points you can optionally specify a color for these points that is different from the default color of the object: <span class="program">f1.plot(pos=(100,-30), color=color.red)</span>. However, this is not currently possible in GlowScript VPython. Once you have created a graphing object such as gcurve, its initial color will be used throughout.</p>
7776
<p class="Normal"> You can plot more than one thing on the same graph:</p>
7877
<p class="program"> f1 = gcurve(color=color.cyan) <br />
7978
f2 = gvbars(delta=0.05, color=color.blue)<br />

docs/VPythonDocs/math.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ <h1 class="Heading-1"> <font color="#0000A0">Math Functions</font></h1>
7272
random() &nbsp;&nbsp;# pseudorandom number 0 to 1<br />
7373
ceil(x) &nbsp;# integer closest to x, &lt;=x<br />
7474
floor(x) # integer closest to x, &gt;=x<br />
75-
round(x) # integer closest to x <br />
75+
round(x) # integer closest to x<br />
76+
factorial(x) &nbsp;# x! = x*(x-1)*(x-2)....(1)<br />
77+
combin(x,y) &nbsp;&nbsp;# x!/(y!*(x-y)!) <br />
7678
max(a,b,c,..) # maximum of these<br />
7779
min(a,b,c,..) # minimum of these</p>
78-
<p class="Normal">The quantities above are simplified forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc.</p>
80+
<p class="Normal">Many of the quantities above are simplified forms of the JavaScript functions Math.abs(x), Math.sqrt(x), etc. <strong><a href="factorial.html">Here are details</a></strong> of the factorial and combin functions.</p>
7981
<p class="Normal">There are functions for converting between degrees and
8082
radians, where there are 2*pi radians in 360 degrees: </p>
8183
<p class="program">radians(360) &nbsp;# equal to 2*pi<br />

lib/glow/api_misc.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,45 @@
286286
function clock() {
287287
return 0.001*msclock()
288288
}
289+
290+
function factorial(x) {
291+
if (x <= 0) {
292+
if (x === 0) return 1
293+
else throw new Error('Cannot take factorial of negative number ' + x)
294+
}
295+
var fact = 1
296+
var nn = 2
297+
while (nn <= x) {
298+
fact = fact*nn
299+
nn += 1
300+
}
301+
if (nn != x+1) throw new Error('Argument of factorial must be an integer, not ' + x)
302+
return fact
303+
}
304+
305+
function combin(x, y) {
306+
// combin(x,y) = factorial(x)/[factorial(y)*factorial(x-y)]
307+
var z = x-y
308+
var num = 1
309+
if (y > z) {
310+
var temp = y
311+
y = z
312+
z = temp
313+
}
314+
var nn = z+1
315+
var ny = 1
316+
while (nn <= x) {
317+
num = num*nn/ny
318+
nn += 1
319+
ny += 1
320+
}
321+
if (nn != x+1) throw new Error('Illegal arguments ('+x+','+y+') for combin function')
322+
return num
323+
}
324+
325+
//console.log('factorial(6) = 6! =', factorial(6)) // should be 720
326+
//console.log('combin(6,2) = 6!/(2!(6-2)!) =', combin(6,2)) // should be 15
327+
//console.log('combin(1000,500) = 1000!/(500!(1000-500)!) =', combin(1000,500)) // should be 2.70288240945e+299
289328

290329
var exports = {
291330
// Top-level math functions
@@ -308,6 +347,8 @@
308347
min: Math.min,
309348
random: Math.random,
310349
round: Math.round,
350+
factorial: factorial,
351+
combin: combin,
311352
radians: function radians(deg) { return (deg * Math.PI / 180) },
312353
degrees: function degrees(rad) { return (rad * 180 / Math.PI) },
313354

lib/glow/graph.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@
428428
this.plot = function (data) {
429429
// Accept plot(x,y) or plot([x,y], [x,y], ...) or plot([[x,y], [x,y], ...]])
430430
this.__graph.changed = true
431+
if (data.color !== undefined) {
432+
throw new Error("Cannot currently change color in a plot statement.")
433+
//this.__color = data.color
434+
//this.options.color = color.to_html(data.color)
435+
//delete data.color
436+
}
431437
if (typeof arguments[0] == 'number') { // x,y
432438
this.options.data.push([arguments[0], arguments[1]])
433439
} else if (typeof arguments[0][0] == 'number') { // [x,y], [x,y], ....
@@ -447,7 +453,7 @@
447453
this.options.data.push(xy)
448454
}
449455
} else {
450-
throw new Error("must be plot(x,y) or plot([x,y]) or plot([x,y], ...) or plot([ [x,y], ... ])")
456+
throw new Error("must be plot(x,y) or plot(pos=(x,y)) or plot([x,y]) or plot([x,y], ...) or plot([ [x,y], ... ])")
451457
}
452458
}
453459
}

0 commit comments

Comments
 (0)