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.
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;
>
joe:=joe,like;
> joe:=joe,math;
>
>
joe:=NULL;
>
joe:=joe,I;
>
joe:=joe,like;
> joe:=joe,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];
>
How to Build a list by hand
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;
> harry:=joe,sally,1;
> whattype(joe);
> whattype(harry);
> joe:= harry,dog, Pi;
> whattype(joe);
> sam:=[joe];
> whattype(sam);
>
> tim:=op(sam);
> whattype(tim);
Now suppose we want to make the list
stuff:= [sally,1,dog,
, 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;
>
whattype(stuff);
> stuff:=[sam,Lance];
>
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,
]" 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);
>
tmp:=tmp,Lance;
>
stuff:=[tmp];
> whattype(stuff);
>
We can be more economical by combining steps by composing commands:
> stuff:=[op(sam),Lance];
>
>
junk:=[a,b,cow];
> nops(junk);
>
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;
> nops(sally);
Error, wrong number (or type) of parameters in function nops
>
>
sally:=1,2,3;
>
nops([sally]);
> whattype(sally);
>
> bird:=turkey;
> animals:=cat,dog,bird;
> print([animals]);
> bird:=pigeon;
> print([animals]);
> animals:=cat,dog,bird;
> print([animals]);
>
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;
>
tmpdog:=Beagle;
>
dogs:=dogs,tmpdog;
>
tmpdog:=Airdale;
> dogs:=dogs,tmpdog;
>
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];
>
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:=[seq(i,i=672345..672714)];
>
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);
>
We can convert it into a list
> cubelist:=[cubesequence];
>
if the list was all we wanted we could have skipped naming the sequence and simply entered
> cubelist:=[seq(i^3,i=-3..13)];
>
Some further examples of lists:
Make a list if the odd integers from 1 to 99
> oddlist:=[seq( 2*j+1,j=0..49)];
>
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);
>
Make a list of lists of numbers so that the nth element in your list is the list
, starting with n=10 and ending with n=30.
> listoflists:=[seq([n,1/n^2],n=10..30)];
>
Exercises: Sequences and Lists
>
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,
]. 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 : 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
>
>
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]);
>
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);
>
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);
>
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) ]);
>
> plots[display] ([seq(BL([n,0,2*n]), n=0..9)]);
>
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) ]);
>
EXERCISES: Making Brick Patterns With seq
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.