Laying Bricks: Getting the Computer to do Repetitive Tasks

Working with expression sequences and lists

The process of analyzing or solving a problem can involve a lot of repetition. Anyone who has graphed many different functions knows the tedious repetition of that process. Maple's built-in "plot" commands allow the computer do the mindless repetition, freeing time for people to look at the relationships implied by the graph - something the machine can't do. Graphing, however is only one example of this of this capability which is absolutely critical to effective use of the computer for solving general problems. Lets look at another - building lists.

We have already made and used a lot of Maple lists. A more primitive notion than that of a list is that of an expression sequence.

Definition: An expression sequence is a comma-separated expression of the form "a,b,c,...,z" where the elements "a", "b" , etc. can be any symbols recognizable by Maple.

The order of elements in an expression sequence is important: "a,b" is a different expression sequence from "b,a"

For our immediate purposes the most important thing about expression sequences is how easy it is to extend them. If joe is an expression sequence and we want to add "Hortense" to the end of joe we can simply enter joe:=joe, Hortense; . If we wanted to place Hortense at the beginning we would enter joe:= Hortense,joe; This idea also allows us to "build" sequences.

If we initially have a sequence named "joe" and need, in the course of a calculation to extend "joe" then this can be done through steps like

> joe:=I;

[Maple Math]

> joe:=joe,like;

[Maple Math]

> joe:=joe,math;

>

Sometimes it is useful as a starting point to have an empty expression. That is something which is an expression but has no items in it. This is evidently analogous to the empty set in set theory. This idea is so useful that Maple has a special name for the emptyexpression sequence. It is called NULL.

[Maple Math]

> joe:=NULL;

[Maple Math]

> joe:=joe,I;

[Maple Math]

> joe:=joe,like;

[Maple Math]

> joe:=joe,math;

[Maple Math]

>

With the notion of expression sequence we can observe that a list is simply an expression sequence delimited by brackets "[ ]". Most important, to convert an expression sequence , joe to a list sam we simply enclose it in brackets

> sam:=[joe];

[Maple Math]

>

Since a list is simply an expression sequence enclosed in brackets there is a simple way to extract the undellying expression sequence from a list with a command called op. We will encounter op quite often. This is but one of its many uses. ( op stands for operands )

How to Build a list by hand

Maple has a word whattype which we can use to ask what type it has assigned to various objects (there are many kinds of types). Now let's illustrate.

First let's make a simple sequence. We will start with the NULL sequence to indicate how it is used as a starting point for building sequences

> joe:=NULL;

[Maple Math]

> harry:=joe,sally,1;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(joe);

[Maple Math]

> whattype(harry);

[Maple Math]

> joe:= harry,dog, Pi;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(joe);

[Maple Math]

> sam:=[joe];

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(sam);

>

[Maple Math]

> tim:=op(sam);

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(tim);

[Maple Math]

Now suppose we want to make the list stuff:= [sally,1,dog, [Maple Math] , Lance]; . We observe that we should be able to do this simply by adding "Lance" to the end of the list "sam". We would like to do something like stuff:=sam,Lance; or stuff:=[sam,Lance]; but neither of these will work. This is not to say that Maple won't accept the commands

> stuff:=sam,Lance;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(stuff);

[Maple Math]

> stuff:=[sam,Lance];

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

>

Neither of these works. The first produces an expression sequence, not a list. Moreover it has only two elements, the first being the list "sam" and and the second the expression "Lance". The second does produce a list but it is a list with two elements: the first element of this is the list " [sally,1,dog, [Maple Math] ]" and the second is "Lance".

In order to produce the list we want we have to extract the expression sequence from "sam", append "Lance " to it and then convert that sequence to a list.

> tmp:=op(sam);

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> tmp:=tmp,Lance;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> stuff:=[tmp];

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> whattype(stuff);

[Maple Math]

>

We can be more economical by combining steps by composing commands:

> stuff:=[op(sam),Lance];

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

>

Incidentally, to go with "op" there is a very useful commnad nops (number of operands) which we can use to tell us how many items there are in a list

> junk:=[a,b,cow];

[Maple Math]

> nops(junk);

[Maple Math]

>

Note that nops doesn't work on an expression sequence.

> nops(NULL);

Error, wrong number (or type) of parameters in function nops

> sally:=1,2,3;

[Maple Math]

> nops(sally);

Error, wrong number (or type) of parameters in function nops

>

Thus in order to count the number of items in an expression sequence we must ask nops to act on a list of which the expression sequence is the operand.

> sally:=1,2,3;

[Maple Math]

> nops([sally]);

[Maple Math]

> whattype(sally);

[Maple Math]

>

It is important to note that writing "[sally]" above does not change "sally" into a list. The assignment command sally:=[sally]; would do that but above we have not assigned a new value to "sally". All we have done is ask nops to act on an unnamed list constructed from "sally". This leads to a comment we must make regarding what happens when we make an expression sequence some of whose elements are objects that have assigned values. For instance suppose we have executed bird:=turkey; and subsequently make an expressions sequence animals:=cat,dog,bird; The sequence animals will then be cat,dog,turkey . That is, the value that has been assigned to any of the terms in the sequence we write down will be substituted immediately. If we then redefine "bird", say bird:=pigeon; the sequence animals will still be cat,dog,turkey unless we do something which re-defines animals (e.g. we might re-execute the command animals:=cat,dog,bird; at which time animals will become cat,dog,pigeon .

> bird:=turkey;

[Maple Math]

> animals:=cat,dog,bird;

[Maple Math]

> print([animals]);

[Maple Math]

> bird:=pigeon;

[Maple Math]

> print([animals]);

[Maple Math]

> animals:=cat,dog,bird;

[Maple Math]

> print([animals]);

[Maple Math]

>

This property of sequences is extremely important for it allows us to build a sequence in steps re-using a temporary name for the item we are adding.

> dogs:=NULL;

[Maple Math]

> tmpdog:=Beagle;

[Maple Math]

> dogs:=dogs,tmpdog;

[Maple Math]

> tmpdog:=Airdale;

[Maple Math]

> dogs:=dogs,tmpdog;

[Maple Math]

>

Automatically Generating Sequences and Lists

Now suppose we wanted to make a list of the first 25 positive intgers. One way to do this is simply to enter it:

> numlist:=[1,2,3,4,5,6,7,8,9,10,11, 12,13,14,15,17,17,18,19,20,21,22,23,24,24,25];

[Maple Math]

>

This is tedious and fraught with opportunity to make mistakes. Indeed the careful reader will have noted that this isn't the correct list, "17" was entered twice. Even that careful soul might not take the time to check whether this sequence which clams to list the numbers from 672345 to 677345 is actually correct.

>

Boring List of Numbers from 672345 To 677345

"boringlist" is actually correct because it was not typed in by hand but rather done by a Maple command seq which generates expression sequences according to a "formula" we prescribe which tells Maple how to find the "nth" element. A few examples suffice to illustrate this very useful command. First, here is the command that generated "boringlist"

> boringlist:=[seq(i,i=672345..672714)];

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

>

Note that seq returns an expression sequence. The brackets in turn make that a list. An expression sequence of the cubes of the integers from -3 to 13 is simply

> cubesequence:=seq(i^3,i=-3..13);

[Maple Math]

>

We can convert it into a list

> cubelist:=[cubesequence];

[Maple Math]

>

if the list was all we wanted we could have skipped naming the sequence and simply entered

> cubelist:=[seq(i^3,i=-3..13)];

[Maple Math]

>

Some further examples of lists:

Make a list if the odd integers from 1 to 99

> oddlist:=[seq( 2*j+1,j=0..49)];

[Maple Math]
[Maple Math]

>

Make an expression sequence of all the natural numbers less than 137 which are 2 plus a multiple of 7.

> twomodsevenseq:=seq(2+7*q,q=0..137/7);

[Maple Math]

>

Make a list of lists of numbers so that the nth element in your list is the list [Maple Math] , starting with n=10 and ending with n=30.

> listoflists:=[seq([n,1/n^2],n=10..30)];

[Maple Math]
[Maple Math]
[Maple Math]

>

Exercises: Sequences and Lists

>

Exercise : Make a list of all the natural numbers which are less than 1000 and are the square of another natural number.

Exercise : Make a sequence of length 100 such that the jth element of list whose first element is the integer j and who second element is the factorization of j into primes.

Exercise : Make a list of length 5 whose jth element is a plot of the function sin(j*x) on the interval [0, [Maple Math] ]. Display all of the graphs at once with plots[display].

Exercise : Make a sequence of 100 random integrers between 0 and 10. ( If necessary, use "help" to look up the topic "random").

Exercise : If "n" is a natural number then n factorial , denoted "n!" is the product n(n-1)(n-2)... (3)(2)(1). For instance 3!=(3)(2)(1) = 6, 1! = 1, 4!=(4)(3)(2)(1) = 24. Maple recognizes the notation n! and will compute the factorial of a natural number. Make a list of the factorials of the numbers from 1 to 20.

Exercise : The Maple command nextprime(n) will return the next prime after the integer "n". Make a list of pairs of numbers so that the nth element of your list is the pair whose first element is an integer and whose second element is the next prime after "n!". Let the first elements of your lists be the even integers from 8 to 20.

Laying bricks. Applications of sequences and lists

>

We can now generate sequences and lists with any command which takes in a single integer and produces an output. It even works with pictures. For instance suppose we want to lay bricks to make (a small part) of a sidewalk,a wall, or the like. First we modify our cube maker from the last worksheet to make only red bricks that a eight units long, four units wide, and two units high. Lets call it "BL" (for bricklayer).

> BL := proc (shift)
local v1, v2, v3, v4, v5, v6, v7, v8, front, back, left, right, bottom, top, box;
v1 := [0, 0, 0]+shift; v2 := [8, 0, 0]+shift; v3 := [8, 4, 0]+shift;
v4 := [0, 4, 0]+shift; v5 := [0, 0, 2]+shift; v6 := [8, 0, 2]+shift;
v7 := [8, 4, 2]+shift; v8 := [0, 4, 2]+shift;
front := [v1, v2, v3, v4]; back := [v5, v6, v7, v8];
left := [v4, v8, v5, v1]; right := [v3, v2, v6, v7];
bottom := [v3, v4, v8, v7]; top := [v1, v2, v6, v5];
box := [front, back, left, right, top, bottom];
plots[polygonplot3d](box,color=red,style=patch, scaling=constrained )
end:

> BL([0,0,0]);

[Maple Plot]

>

Suppose we want to make a stack of 10 bricks. Of course we could "personally" make each brick:

> b1:=BL([0,0,0]):
b2:=BL([0,0,2]):
b3:=BL([0,0,4]):
b4:=BL([0,0,6]):
b5:=BL([0,0,8]):
b6:=BL([0,0,10]):
b7:=BL([0,0,12]):
b8:=BL([0,0,14]):
b9:=BL([0,0,16]):
b10:=BL([0,0,18]):

>

Then we produce a list to feed to plots[display] . Note that we terminate with a semicolon. The reader might want to change it to a colon and execute the command to remind him/herself why - then change it back to a colon and execute again.

> tenbricks:=[b1,b2,b3,b4,b5,b6,b7,b8,b9,b10]:

>

Now we can look at the picture:

> plots[display](tenbricks);

[Maple Plot]

>

We observe that what we are doing is in making the nth brick is just BL([0,0,2*n]); and this depends only on the integer "n" (we are starting with n=0). So to get our list all we need do is the following. Again we terminate with a colon ":" as we don't want to see the list in this form - we only want the picture.

> bricklist:=[seq(BL([0,0,2*n]),n=0..9)]:plots[display](bricklist);

[Maple Plot]

>

We can improve even further with a compound command which simply combines the previous steps

> plots[ display ]([ seq(BL([0,0,2*n]), n=0..9) ]);

[Maple Plot]

>

Suppose we want our bricks in a "staircase" pattern, say each brick is to shift over by one unit in the positive "X" direction. Since the description of the nth brick depends solely on "n" we can do it.

> plots[display] ([seq(BL([n,0,2*n]), n=0..9)]);

[Maple Plot]

>

Suppose we want only the even numbered bricks to shift

> plots[display] ([ seq( BL([n*(1+(-1)^n)/2,0,2*n] ), n=0..9) ]);

[Maple Plot]

>

EXERCISES: Making Brick Patterns With seq

Exercise : Stack the ten bricks in so that they shift both in the "X" and "Y" directions, making each brick after the first shift 1/3 of a unit in the positive "X" direction and 1/4 of a unit in the negative "Y" direction.

Exercise : Stack the ten bricks so that they don't shift in the "Y" direction but we want each odd numbered brick to be shifted 1 units more than the previous odd numbered brick in the positive "X" direction and the ith even numbered brick to be shifted 1 unit more than the previous even-numbered brick in the negative "X" direction.

Exercise : Using the "seq" command place the ten bricks so that are end to end.

Exercise : Using the "seq" command place the ten bricks so that they are in a rectangular pattern of five rows and two columns.

Table of contents