Demo #7
Lists, Vectors, and Matrices 
in Mathematica

Lists

Mathematica expresses  vectors, matrices, and tensors in the form of lists. For example, a one-dimensional list is:

a = {5.0,  2.5,  4.6,  7.8}

This is a four element list. We can apply functions to the list directly, for example:

b  =  Sqrt[ a]

To find out whether a Mathematica command can be applied to a list, as opposed to a single object, type ??Command and look for the attribute Listable. e.g.,

?? Sin

So, we can apply Sin in this way:

c  =  Sin[a]

Addressing Elements of a List

Part[list, i] or list[[i]] -> gives the ith element of a list

Part[a, 3]

a[[3]]

The first and last elements are conveniently obtained from the functions First[list] and Last[list], e.g.

First[a]

Take[list,{m,n}] -> gives a sublist with elements m through n

d  =  Take[ a, {2, 4}]

Drop[list, {m,n}] -> gives a sublist without elements m to n

e  =  Drop[a, {1, 2}]

Part works similarly to Take:

?Part

f  =  Part[a, {2, 4}]

g  =  a[[{2, 4}]]

Lists can also be joined using the Join command:

h  =  Join[a, b]

Generating List Elements From a Formula

Often we would like to quickly fill out the elements of a list according to some algorithm.  The Table command helps out here:

?? Table

For example, suppose we wanted a one-dimensional list of the squares of the first ten positive integers:

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

In more complicated cases, we might want to define a function:

Clear[x, f];
f[x_] :=  x^2  -  x^5  +  7 x^3

j  =  Table[ f[x], {x, 0, 4, 2}]

Vectors

Mathematica represents vectors as one-dimensional lists. For example, a three dimensional vector is written:

v1  =  {1, 1, 1}

We can perform scalar multiplication simply:

4 * v1

Vector addition is also as expected:

v2  =  {0, 1, 0}

v1  +  v2

If we want vectors to be expressed in more conventional column form, we can simply append //ColumnForm to our command,e.g.

v1  +  v2 // ColumnForm

Note that this only influences the appearance of the expression, not Mathematica's internal representation.

Vector Products

There are also commands built in for various vector products. To use these, however, one must load the VectorAnalysis package:

<< Calculus`VectorAnalysis`

The Dot Product, for example, is generated by:

s1  =  DotProduct[v1, v2]

The Cross Product generates a new vector:

v3  =  CrossProduct[v1, v2]

Finally, the Triple Scalar Product v1 . ( v2 x v3 ) is generated by:

s2  =  ScalarTripleProduct[v1, v2, v3]

Two-Dimensional Lists

Mathematica also allows us to define two and higher dimensional lists of data or variables. For example,

list1  =  {{2, 4},  {3, 6}, {4, 8}}

If one wants to express this in Tabular form:

list1 // TableForm

We can also use the Table command to generate two-dimensional lists quickly

list2  =  Table[ i^2 j^2, {i, 1, 3}, {j, 1, 2}]

We can address individual elements of two-dimensional lists in the expected way. For example,

list2[[3, 2]]

accesses the 3,2 element of the list (3rd row, 2nd column).The first row is accessed by:

list2[[1]]

We can change the 1,1 element by the command:

list2[[1, 1]] = 75

list2 // TableForm

If we have some data in a data set outside of Mathematica, we also might wish to import it. Suppose we have a file called dataset1.txt. We can read it into our notebook using the command ReadList:

?? ReadList

We demonstrate this in the following command where we read the file dataset1.txt whose contents are as follows:

2 600
6 800
10 1000
12 1500
14 2000
20 4000
30 6000
40 8000
50 10000
55 20000
57 30000
60 40000

Note that the numbers in each row are seperated simply by a space.  Each "carriage return" or "end-of-line" defines a new "record" -- this is what tells Mathematica that it is to construct a "List" with each member of the list containing 2 entries.

list3  =  
      ReadList["C:/texdocs/Classes/Eng_5A_2000Fall/demos/dataset1.txt", 
        Number, RecordLists -> True]

list3 // TableForm

The first column of this data corresponds to a percentage reduction in pump speed and the second column represents a corresponding fluid viscosity.We can make a plot of this discrete data by using the function ListPlot:

plt1  =  ListPlot[list3]

If we want to interpolate between the points, we can use Mathematica's Interpolation command:

?? Interpolation

fnc1  =  Interpolation[list3, InterpolationOrder -> 1]

plt2  =  Plot[ Evaluate[fnc1[x]], {x, 2, 60}]

Show[plt1, plt2]

By playing with the value of the variable InterpolationOrder, one can get a variety of types of interpolation, ranging from linear to parabolic to cubic, etc.

Finally, we note that a convenient command exists for converting two-dimensional lists to one-dimensional form. This is the Flatten command:

?? Flatten

list4  =  Flatten[list3]

Matrices

Matrices in Mathematica are simply represented as two-dimensional lists. For example:

matrix1  =  {{1, 4, 6}, {7, 4,  8}, {9, 4, 2}}

Besides ListForm, we can also display this in so-called MatrixForm:

matrix1 // MatrixForm

Again, this only influences how the matrix looks,not how it is represented by Mathematica.

Besides, the Table command, which is handy for quickly filling a multidimensional list or matrix, the Array command is useful for declaring that a variable is a matrix or two-dimensional list:

?? Array

For example:

vect1  =  Array[ v, 5]

matrix2  =  Array[a, {3, 3}] // MatrixForm

There are also convenient functions for generating the Identity Matrix and diagonal matrices. For example:

IdentityMatrix[5] // MatrixForm

DiagonalMatrix[vect1] // MatrixForm

Matrix Operations

There are a number of useful commands for manipulating matrices in Mathematica. Consider m to be a matrix:
		
c m->multiply by a scalar
m1 . m2 -> matrix multiplication
Inverse[m] -> matrix inverse
MatrixPower[m,n]-> nth power of the matrix m
Det[m] -> determinant of m
Transpose[m] -> transpose of m
Eigenvalues[m] -> eigenvalues of m
Eigenvectors[m] -> eigenvectors of m

Let's try these:

matrix3  =  5 * matrix1 // MatrixForm

matrix4  =  matrix1  .  IdentityMatrix[3] // MatrixForm

Notice that this is just matrix1 back again, as we expect from the definition of the identity matrix.This matrix product is very different than using "*" or nothing!!:

matrix5  =  matrix1 IdentityMatrix[3] // MatrixForm

matrix6  =  Transpose[matrix1] // MatrixForm

matrix7  =  Inverse[matrix1]

matrix7  .  matrix1  // MatrixForm

Eigenvalues[matrix1]

Eigenvalues[matrix1] // N

Det[matrix1]

In summary, we see that Mathematica makes tedious matrix algebra quite easy!