Lists and Matrices

Basic Introduction

The basic element in Mathematica is the list.   Lists can contain numbers and/or symbolic expressions.  They are always enclosed in "curly brackets":

bigList = {element1, element2,.... }

The elements of a list can also be lists themselves:

bigTable={{element11, element12}, {element121,element22, element23}}

You can think of a list of lists as a matrix - each element is a row of the matrix.  Mathematica just refers to this as a table.  You can also make lists of lists of lists of lists of....   well, you get the idea.

Let's make some lists:

smallList={1,2,3,4,5}
smallTable={{a11,a12,a13},{a21,a22,a23},{a31,a32,a33}}

To view our lists, we can put them through some filtering commands that make them look pretty:

TableForm[smallList]

MatrixForm[smallList]

TableForm[smallTable]

MatrixForm[smallTable]

Note: The output of this formating commands are not mathematical expressions - they are like a pretty picture.   You can't use them for subsequent analysis.

Advanced Table Construction:

You usually don't want to type in every element in a list/table.   There's typically a formula you are trying to implement.  You can create such a list using the Table command.  Look up the Table command now in the help browser.

biglist=Table[(x^3),{x,1,10}]

bigTable=Table[i + (Sqrt[j]) ,{i,1,10},{j,1,10}];
MatrixForm[bigTable]

The Table command is like a procedure in other languages. There can be many intermediate steps inside the table command.  As long as they do not have output (they have a trailing semicolon)  Mathematica  simply evaluates them.  Only the final output is put in the table:

listExample=Table[x1=Sinh[k]; x2=Cosh[k];  foo= (x1^2)-(x2^2), {k,0,10}]

Element Extraction

There are many, many commands to manipulate and extract elements from lists in Mathematica.  Usually you simply look in the Help browser to find the command you need.   For example, to get the  first or last element:

First[smallList]
Last[smallList]

What is the first element in smallTable?

First[smallTable]

It is itself a list!   How do we get the first element?  (Think about it...)

There is a very useful way to refer to elements explicitly by their location in a list or array:  You can reference the location of the element, using double square brackets:

smallList[[1]]

smallList[[2]]

smallTable[[1]]

smallTable[[1,3]]

Note carefully the format of the last command:  it is the third element of the first list in the table.   Thus matrix[[i,j]]  will give you the i-j-th element of a matrix.   This gives us yet another way to create matrices, using Do commands.

The Do command is a lot like the Table command, but it does not have any output.  It's the closest to a "Do" loop in Fortran, or a "For-Next" loop in Basic.     The idea is that we create a blank table, and then poke in the values we want that are not zero.  This is often handy for solving sparse matrix equations without resorting to the Sparse package.

hmat=Table[0,{i,1,20},{j,1,20}];
Do[hmat[[k,k+1]]=b;hmat[[k+1,k]]=b;,{k,1,19}];
Do[hmat[[k,k]]=a,{k,1,20}]
MatrixForm[hmat]

Matrix operations

All the matrix operations you know and love are now quite simple:

MatrixForm[Inverse[smallTable]]

Here's a numerical example:

matrix=Table[RandomReal[],{i,1,10},{i,1,10}]
invMatrix=Inverse[matrix]

Let's check:

matrix.invMatrix // MatrixForm

Do you know what went wrong?  Note the use of the "pipe"  // construction.

matrix.invMatrix // Chop // MatrixForm

hamil=hmat/. {a-> -2., b-> 1.}

eout=Eigensystem[hamil];
eval=eout[[1]]
evecs=eout[[2]];

ListPlot[evecs[[20]]]

Things to do:

(1)  Prove that the 20th eigenvector above is indeed an eigenvector of the matrix, hamil.

(2)  Calculate the inverse of a series of random matrices,  size (2^n) x (2^n)for n=1 to 15.   Look up and use the Timing command to see how long it takes as a function of n.

Interesting example

Let's orthogonalize the first 10 powers of x over the range 0<x<1.

basis=Table[(x^n),{n,0,4}]
orthobasis=basis

Do[Do[olap=Integrate[basis[[n]]*orthobasis[[p]],{x,0,1}]; orthobasis[[n]]=orthobasis[[n]]-olap*orthobasis[[p]];,{p,1,n-1}];
cnorm=(Sqrt[Integrate[((orthobasis[[n]])^2),{x,0,1}]]); orthobasis[[n]]=orthobasis[[n]]/cnorm;, {n,2,Length[basis]}]

orthobasis

Plot[Evaluate[orthobasis],{x,0,1}]