Tutorial 4:  Lists and Tables

by R. Dover, IMSA

Lists and tables

Curly braces are used quite often in Mathematica.  They are used primarily for lists of objects, but they may also be used for indices and for domains of functions.
	First, we'll create a short list and perform several operations on it.  Execute each cell below.

{2,  3,  5,  8,  13}

\(%\^2 - \ 1\)

%%  x  +  1

As you recall, the "%" refers to the last output. "%%" refers to the second to the last output.  Had we planned on doing much with our list, it would have been simpler to give it a name.
	Clearly, the need may come to generate a list - here, called a "table."  We'll name this one.

ourlist  =  Table[2  i,  {i,  1,  10}]

Note that output is in fact a list with curly braces.  Square each of the values in ourlist.  Then divide each term by 6 and find numerical approximations.

Execute the following.  Note that the symbol ■[[□]] may be found on the palette.

ourlist[[4]]

Edit the line above to change the number 4.  Be sure you understand what this notation does.

Now we'll check out a little trig.

Table[Cos[n π],  {n,  0,  1,  1/6}]

Note the effect of the 1/6 above, the optional extra term inside the curly braces for the index.

We may also create a list of lists by creating a table of a list.

Table[{i,  (i^2),  (i^3)},  {i,  1,  20}]

This is rather difficult to view.  Use the command "TableForm" on the last output.  (Either type "//TableForm" at the end of the line above or type "TableForm[   ]" around the command so that it begins "TableForm[Table[..."  Watch the capital letters on both words.  There is no space between the words since it is one command.)

Define a function f.  Then create a table and display is using "TableForm" which will show the ordered pairs (x, f(x)) for 0 ≤ x ≤ 5 by steps of 0.2.

When you are done, be sure to clear f and ourlist.

More numerics

Did you notice that the terms in the last table you created came out in decimal form?  Curious.  Previously, we had to tell Mathematica to give answers in decimal form.  Here, note what you entered for the step size.  Edit that command, replacing 0.2 by 1/5.  Mathematica assumes that if you put decimals in, you want decimals out.  In other words, it's another method to ask for a decimal output.  Try this on a couple other commands.

Vectors

Let's create and name two vectors, v and w.  (The letters are not capitalized to distinguish them from any built-in names.)  Vectors are simply lists.

v  =  {2,  -5,  1};  w  =  {1,  2,  -3}

Find the scalar product 4v.  Then find 2v - 3w.

Execute the next cell. What does the program assume about c?

c +  v

The dot product is denoted by v.w, where that's a period between the two vectors.  Try it.  Then ask Mathematica if this operation on these vectors is commutative.

Try the cross product.

Cross[v,  w]

Alternatively, use the following notation with the cute little "×".

v \[Cross]w

When finished, clear v and w.

Matrices

Matrices may be expressed in two ways.  Either way, Mathematica see a matrix as a list of lists.  We define mat1 by using the palette.

mat1 = (({{3, 5}, {-1, 2}}))

Curious output.  This looks like the list of lists mentioned earlier with braces within braces. Select the output cell by clicking on the bracket at the right.  Under the "Cell" menu, select "Convert to" and choose "TraditionalForm."  This will look nicer, but Mathematica is sometimes a little less confident of this notation.  Note the new markings in the cell bracket.  Alternatively, create mat2 as follows and, on another line, use the command "MatrixForm[mat2]".

mat2  = {{2,  0},  {3,  1}}

Several commands work as expected on matrices.  Find 3mat1, the determinant by using 
Det[mat1], and MatrixPower[mat1, 3] to represent the 3rd power of the matrix.  Express these in MatrixForm. (Alternatively, one may type "//MatrixForm" after any expression.)

Then find the "Transpose" and the "Inverse" of mat2.

Now find mat1 + mat2  and  mat1.mat2, using the period to represent multiplication as we did before. What happens if one executes the command (mat1^2)?

So check this out.  What should the result be?

(({{a, b}, {c, d}})).Inverse[(({{a, b}, {c, d}}))]

"Fix" that by using Simplify on the command above.  Look better?
	Want a different size or shape?  Obviously, this may be created using the curly braces, but it may also be entered in matrix form.  From the "Input" menu, choose "Create Table/Matrix/Palette..."  Under "Make," choose "Matrix" and choose the size and shape you wish.  Fill in values as desired.  Name this mat3.

Now determine what each of the following commands does.  (Hopefully your matrix isn't too small!)

mat3[[2]]

mat3[[3,  2]]