1.8.3 Vectors and MatricesVectors and matrices in Mathematica are simply represented by lists and by lists of lists, respectively.
| {a, b, c} | vector | | {{a, b}, {c, d}} | matrix |
The representation of vectors and matrices by lists. This is a matrix. | |
In[1]:=
m = {{a, b}, {c, d}}
|
Out[1]=
|
|
| Here is the first row. | |
Out[2]=
|
|
Here is the element . | |
Out[3]=
|
|
| This is a two-component vector. | |
Out[4]=
|
|
| The objects p and q are treated as scalars. | |
Out[5]=
|
|
| Vectors are added component by component. | |
In[6]:=
v + {xp, yp} + {xpp, ypp}
|
Out[6]=
|
|
| This takes the dot ("scalar") product of two vectors. | |
In[7]:=
{x, y} . {xp, yp}
|
Out[7]=
|
|
| You can also multiply a matrix by a vector. | |
Out[8]=
|
|
| Or a matrix by a matrix. | |
Out[9]=
|
|
| Or a vector by a matrix. | |
Out[10]=
|
|
| This combination makes a scalar. | |
Out[11]=
|
|
Because of the way Mathematica uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors.
| Table[f, {i, n}] | build a length- vector by evaluating f with i = 1, 2, ... , n | | Array[a, n] | build a length- vector of the form {a[1], a[2], ... } | | Range[n] | create the list {1, 2, 3, ... , n} | Range[ , ] | create the list { , +1, ... , } | Range[ , , dn] | create the list { , +dn, ... , } | | list[[i]] or Part[list, i] | give the i element in the vector list | | Length[list] | give the number of elements in list | | ColumnForm[list] | display the elements of list in a column | | c v | multiply by a scalar | | a . b | vector dot product | | Cross[a, b] | vector cross product (also input as a b) | | Norm[v] | norm of a vector |
Functions for vectors.
| Table[f, {i, m}, {j, n}] | build an matrix by evaluating f with i ranging from 1 to m and j ranging from 1 to n | | Array[a, {m, n}] | build an matrix with  element a[i, j] | | IdentityMatrix[n] | generate an identity matrix | | DiagonalMatrix[list] | generate a square matrix with the elements in list on the diagonal | | list[[i]] or Part[list, i] | give the i row in the matrix list |
list[[All, j]] or Part[list, All, j]
| | give the j column in the matrix list | | list[[i, j]] or Part[list, i, j] | give the  element in the matrix list | | Dimensions[list] | give the dimensions of a matrix represented by list | | MatrixForm[list] | display list in matrix form |
Functions for matrices. | This displays s in standard two-dimensional matrix format. | |
Out[13]//MatrixForm=
|
|
| This gives a vector with symbolic elements. You can use this in deriving general formulas that are valid with any choice of vector components. | |
Out[14]=
|
|
This gives a matrix with symbolic elements. Section 2.2.6 will discuss how you can produce other kinds of elements with Array. | |
In[15]:=
Array[p, {3, 2}]
|
Out[15]=
|
|
| Here are the dimensions of the matrix on the previous line. | |
Out[16]=
|
|
This generates a diagonal matrix. | |
In[17]:=
DiagonalMatrix[{a, b, c}]
|
Out[17]=
|
|
| c m | multiply by a scalar | | a . b | matrix product | | Inverse[m] | matrix inverse | | MatrixPower[m, n] | n power of a matrix | | Det[m] | determinant | | Tr[m] | trace | | Transpose[m] | transpose | | Eigenvalues[m] | eigenvalues | | Eigenvectors[m] | eigenvectors |
Some mathematical operations on matrices. Here is the matrix of symbolic variables that was defined above. | |
Out[18]=
|
|
| This gives its determinant. | |
Out[19]=
|
|
| Here is the transpose of m. | |
Out[20]=
|
|
| This gives the inverse of m in symbolic form. | |
Out[21]=
|
|
Here is a rational matrix. | |
In[22]:=
h = Table[1/(i+j-1), {i, 3}, {j, 3}]
|
Out[22]=
|
|
| This gives its inverse. | |
Out[23]=
|
|
| Taking the dot product of the inverse with the original matrix gives the identity matrix. | |
Out[24]=
|
|
Here is a matrix. | |
In[25]:=
r = Table[i+j+1, {i, 3}, {j, 3}]
|
Out[25]=
|
|
| Eigenvalues gives the eigenvalues of the matrix. | |
Out[26]=
|
|
| This gives a numerical approximation to the matrix. | |
Out[27]=
|
|
| Here are numerical approximations to the eigenvalues. | |
Out[28]=
|
|
Section 3.7 discusses many other matrix operations that are built into Mathematica.
|