Friday, October 30, 2009

gsp trick - booleans

A boolean variable (see Java class Boolean) in computer programming can be thought of as true/false or 1/0 or on/off, to be used as a switch. The boolean variable is an element of Boolean Algebra, named for George Boole, an English mathematician who died in 1864 after making significant contributions to the foundation of computer science.

The boolean variable is an indispensible concept in programming and it can be created and used within Sketchpad.

Most recently, I employed this trick in my Riemann Sum sketch to define a single function plot which shows one of three different functions, as selected by clicking one of a set of three action buttons. Here are directions for such a construction.

Construct a line segment BC with a point A on the segment, as shown in the screenshot. Now select (in this order) C, B, A and from the Measure menu, select Ratio. Move point A back and forth to make sure the ratio measurement goes between 0 and 1. If the points are selected in a different order, a different ratio will be measured, which could range from 0 to infinity. We want 0 to 1, and this ratio measurement will be the boolean variable.

Now create two action buttons to "Move A->B" and "Move A->C". On the "Move" tab for the button properties, set the speed to "instant".

Next, create two presentation buttons. Select the "Move A->B" and "Move A->C" buttons (order matters) and create a presention. Make sure it is sequential rather than simultaneous. Construct the second presentation button after selecting "Move A->C" and then "Move A->B". Test the presentation buttons to make sure you know which sets the variable to one and which sets it to zero. Label the presentation buttons accordingly. The variable is complete and its values will be 0 or 1, provided it is changed only using the buttons.

Here is a neat application of the idea. Select the objects of the construction and define a tool to do the work. Using that tool, Sketchpad will define three new boolean variables with just six clicks, since the only independent elements of the construction are the two segment endpoints.


Next, I have renamed the variables f_boolean, g_boolean, and h_boolean. For each variable, I renamed the two presentation buttons as, for example, "f=0" and "f=1" after carefully making sure that I knew which was which.


Define three functions, for example: f(x)=x, g(x)=x2, and h(x)=x3. Now plot the function k(x)=f(x)*f_boolean + g(x)*g_boolean + h(x)*h_boolean. The plot of k(x) can be changed by making three presentation buttons whose labels can be "show f", "show g", and "show h". The presentations should be constructed by selecting these buttons:

"show f" is the presentation of f=1, g=0, h=0
"show g" is the presentation of f=0, g=1, h=0
"show h" is the presentation of f=0, g=0, h=1

Here order is not important and a "simultaneous" presentation is acceptable. Also, it makes a smoother transition to change the six movement buttons {f=1, f=0, g=1, g=0, h=1, h=0} to have medium speed. This is mathematically different from a boolean and in some applications it may be confusing or incorrect to indicate a continuum of values from 0 to 1, but here it looks really cool. Also note that the function k(x) will have the greatest common domain of f, g, and h.



This is a nifty trick that lets you switch back and forth between a few functions for some variety without all of the typing.

No comments:

Post a Comment