Sage Interact: Sierpinski Carpet

Graphics for the Sierpinski Carpet were posted earlier, up to (and including) 5 iterations. I really wanted to get a sixth iteration so I let the code run and run. All I got were warnings from my computer about a script which was either unresponsive or busy. After about 80 minutes I gave up. But that sort of behavior was similar to what I encountered with Sierpinski's Carpet code. That problem was lessened when I  experimented with multiple Graphics() assignments which led to about double the iterations; maybe something similar could be done with Sierpinski Carpet code? It sure seemed like at least 1 more iteration seemed possible.

With a little extra tinkering I was able to get that extra iteration by creating other Graphics() objects that get used when the length gets smaller. The basic idea which I learned from Sierpinski's triangle is that it seems like a "fresh" Graphics object is needed to get the extra iterations. This was worked into the code by making it use the other Graphics() objects when the length got small. The sixth iteration of Sierpinsksi's Carpet has added to the others. I quit trying to get a 7th iteration after 30 minutes.

I've posted both codes for Sierpinski's Carpet on the Python/Sage page so you can compare them. Maybe this will be helpful in things you're working on.

Depending on the code and the iterations, be prepared to wait minutes for your output, so you might want to increase that slider one iteration at a time and see how your computer handles it. The screenshot from the original code is posted here.

I might have gotten a glimpse of 7 iterations (count the squares) but, unfortunately, it wasn't saved to PDF, just PNG. After looking at it, it seems like the squares blend together. Here is the output. Click on the image and then (again) use the magnifying glass.



For high school mathematics, I consider the 3 most important concepts to be sets, functions, and limits so you might not be surprised that I'm especially concerned about students' poor understanding of functions. In any given year, the best students I've taught:

  • are unable to give the definition of a function
  • think functions go from the real numbers to the real numbers
  • have no idea they can find functions everywhere in the world around them
  • aren't aware that a function can have a restricted domain

So, for example, when asked for the definition the best ones might say something like, "one x for one y" and if you ask them about the graph of range of y=x^2 for x \geq 0, expect problems. But the real danger to me is when students haven't been told about the functions all around them because mathematics can lose its relevancy to them. A student who has been exposed to a function that scrambles a message will remember that functions aren't just from the real numbers to the real numbers. They'll also appreciate the inverse of a function, too, when it comes time to unscramble the message. Everyone should see some form of Caesar's cipher when they are learning about functions.

To help students understand functions a little better, I have 2 teaching points to address the problem:

  1. A function goes from one set to another set.
  2. You can find functions all around you in the world.

I try to reinforce those points throughout whatever material I have the opportunity to teach: a coloring of a graph is a function, the modulus of a complex number is a function, a 2 by 2 matrix can be thought of as a function from the coordinate plane to the coordinate plane, and so on. If you give them enough examples throughout the year then some are bound to pick it up. Here's an example to show how you can even find functions while you're travelling: This function tells you how much to pay depending on how far away you're travelling.

The table below is also a function  which associates a fare for any 2 distinct stations that you start and finish at. What's the cardinality of the domain? The range?

I've added the 2 teaching points to the teaching points page along with lots of examples for you to pick and choose from depending on the math background of your class.

Odds and Ends: 12/28/2011

I've made various changes:

The Beauty of LaTeX, a nice explanation of LaTeX versus WYSIWYG, has been added to the LaTeX page.

Links for Asymptote (link1 and link2 called, respectively Asymptote and have been added to the sidebar.

The first 5 iterations of Sierpinki's Carpet (different from Sierpinki's Triangle) have been added to the Graphics page. These were also created with Sage Interact. I'll post that code later.

I've been creating various fractals with Sage Interact because students like fractals and they're a great topic if you're learning geometric series (finite or infinite). Since more fractals are coming, I've created  a page, Sage Interact: Fractals, which can be accessed by the link on the Sage Output page as well as by a link on the sidebar. The output of Sierpinski's Triangle has been moved to that page.

Sage Interact: Sierpinksi Triangle

As promised, a modified version of the code which produced the images of Sierpinkski's Triangle (which were posted on the Graphics page) has been posted on the Python/Sage page. Although my version went up to 9 iterations, I've changed the code so that the maximum iterations is at 4. Not knowing how fast your computer is I figure it is better that you figure out how many iterations are best. Be prepared to wait minutes for the output. Nothing is wrong, it just takes time.

I'm a lousy programmer, but let me warn you about the code. The 3 lines

T1 = Graphics()
T2 = Graphics()
T3 = Graphics()

make the code appear rather clunky. In fact, my first version through didn't have these lines (and the line S=T1+T2+T3), it just used S to handle everything. That caused problems which I don't have the expertise to explain. At 4 iterations my quad core computer with 8 GB of RAM was brought to a standstill; the memory was all used up. Creating T1, T2, and T3 was my attempt to fix the problem. I don't know why it gives me so many additional iterations but modify it at your own risk.

Finally, even in the version which allowed me get 9 iterations, it took minutes to get the output. So experiment slowly with your machine!

The basic idea behind recursion is to identify the process that takes you from one step to the other; this is revealed in the first step. For Sierpinski's triangle you take a black triangle, identify the midpoint of each side, connect the midpoints to create a triangle which you color white. Those midpoints are vertices of the 3 resulting black triangles which are needed when you repeat the process. The essential information in all of this are the 3 starting vertices. From them we can compute the 3 midpoints by the Midpoint Formula: The midpoint of (x_1, y_1) and (x_2, y_2) is (\frac{x_1+x_2}{2}+\frac{y_1+y_2}{2}).

The first IF statement: [ if (N>0): ] draws the initial triangle. If there are still more iterations to be done [ if (N>1): ] then there are 3 triangles to create which will need to be processed again, one triangle for each of the 3 corners (A, B, and C of the original triangle).

You can see the results on the Sage Output page.



Fractals: Sierpinski's Triangle

The sagemath wiki has code to create Sierpinski's Triangle (bottom of the page)

using the Rule 90 elementary cellular automaton

which means nothing to me until I ran the code and looked closer at the image. Basically elementary cellular automaton is just geek speak for making things out of squares but I found the results weren't satisfying. If I'm going to use Sierpinksi's Triangle in the classroom then I'd want to show how it's created iteration by iteration. For a small number of iterations the elementary cellular automaton method just doesn't look very nice to me, so I created an interact version which would create Sierpinski's Triangle the traditional way. Zoom in on these images and and there is no "blockiness".  I've posted the results on the Graphics page, although it's technically Sage output, because I'm planning on using that page to post a screenshot of the code along with Sage Interact output (sometime soon).

Free e-books: (La)Tex, math, chess, Python, and more

The E-books Directory is a great site for getting "...freely downloadable ebooks, documents and lecture notes...". In some cases, the content isn't available as a PDF but can be read online for free. Today they have a link to read "Making TeX Work" by Norman Walsh. That book "...assumes that you have already read the Advanced LaTeX document" so we give the link to all the free TeX resources available here. E-books Directory also has e-books on math, chess, Python, and other topics, too, so I've added the link to the home page as well.

Calculators: The Good

Since many current calculators are now CAS (computer algebra systems) with graphing capabilities, the term calculator can be used interchangeably with math technology. Here are some reasons to require the use of calculators/technology in the classroom:

  1. Technology can reinforce math skills (e.g. order of operations).
  2. Calculators can help you get answers to the problems in your book.
    Not sure about the answer for problem 16? Maybe a calculator can
  3. Technology can save us a lot of time: They allow us to focus on the
    steps for solving a problem without having to worry about a lot of the
    little details.
  4. Graphing calculators can graph most functions quicker and better than
    we can.
  5. Technology is useful in testing conjectures.
  6. Technology forces students to learn a new, practical set of skills and
    concepts which are important issues in jobs relying on math.
  7. Calculators are a necessary part of the sciences. Physics and Chemistry
    have many problems that are too difficult or too time consuming to do
    by hand. Any person studying in a science needs to know how to use
    a calculator properly.
  8. Technology lets us examine more complicated and/or realistic prob-
    lems: For example, most equations and integrals that arise in ”real”
    world applications can’t be solved. Now realistic problems can be stud-
    ied because technology can help simplify the difficult parts.
  9. Using technology can make math more interesting to students which in turn will make them more likely to take more math classes.

These reasons have been added to the Calculator page and can/should be used as teaching points because students need to learn to use the calculator judiciously as a tool and not a crutch. Since you can't trust the students to know this, the teacher must oversee how the calculator is being used. So, for example, the teacher should insist on a step by step solution and not let students give unsupported answers. Using the calculator to check an answer or to take a complicated expression and convert it into a meaningful answer (e.g. 6.45 meters) are the good uses. but using a calculator as the first method of solution promotes sloppy, disorganized thinking and ultimately undermines a student's mathematical foundation.

Unfortunately, the poor math background of many math teachers means that calculators are not used appropriately and students are being harmed. I'm assuming the teacher has a good math background; then the calculator can enhance a student's learning of mathematics.

Did I miss a good reason? Let me know!

Sage Interact: Primes Table

I stumbled onto Wolfram Demonstrations Project a month or so ago. They've got a lot of demonstrations that have been put together showing the power of Mathematica. Since Mathematica is expensive to buy it seems like they're trying to promote themselves to organizations rather than people. Some of those demonstrations involve high school mathematics, but I found very few that I could imagine an actual high school math teacher would want to use.

One demonstration I liked was Table of Primes, so I created a prime table of my own using Sage Interact. I wanted my "Primes Table" to have several properties:

  • Be able to vary the number of rows and columns
  • have the text size adjust to the size of the boxes
  • have the numbers in the boxes start in the upper lefthand corner of the page
  • be big enough so that I could show a "large" number of primes

I've posted a text file with the code on the Python/Sage page; copy it into a Sage cell and press Shift and Enter at the same time to get this (assuming you set the rows to 9 and columns to 35):

Let's look at some areas of the code:

def the_primes(rows = (9..55), columns = (9..55)):

creates 2 sliders, the row slider will go from 9 through 55 inclusive and the column slider will also go from 9 through 55. Since 55*55=3025, you can study the numbers up to 3025, which is good enough for me. The line

G+=line([(2*i,2*j),(2*(i+1),2*j),(2*(i+1),2*(j+1)),(2*i,2*(j+1))], rgbcolor=(0,0,0))

creates the boxes holding the integers while the complicated looking


creates the proper integer to put into each box so that the smallest number will be in the upper lefthand corner. The sequence of IF statements determines the appropriate size of the text depending on how big the boxes are while

if is_prime(abs((j-1)-rows+1)*columns+i):
G+=polygon([(2*i,2*j),(2*(i+1),2*j),(2*(i+1),2*(j+1)),(2*i,2*(j+1))], rgbcolor=(0,.7,0))

colors the box green if the number inside is prime. Finally, we use "figsize=19" to make the  display big and "aspect_ratio=1" to make sure the squares look like squares, rather than rectangles:, figsize=19,axes=False)

With Sage and a little code,  you can create your own demonstrations for free. Now that you understand parts of the code, you can modify it to suit the needs of your own classroom.

Latest additions

A chess post covering very basic information on naming the squares is located on the Chess page and a new problem has been posted on the Problems page:

How many triangles are contained in the graph K_5 -e? Hint: It might help to count the triangles in  K_5 and throw out the "triangles" containing the missing edge.