Mathematica 9 is now available
 Documentation / Mathematica / The Mathematica Book / Practical Introduction / Lists  /

1.8.3 Vectors and Matrices

Vectors and matrices in Mathematica are simply represented by lists and by lists of lists, respectively.


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.
  • In[2]:= m[[1]]

    Out[2]=




  • Here is the element


    .
  • In[3]:= m[[1,2]]

    Out[3]=

  • This is a two-component vector.
  • In[4]:= v = {x, y}

    Out[4]=

  • The objects p and q are treated as scalars.
  • In[5]:= p v + q

    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.
  • In[8]:= m . v

    Out[8]=

  • Or a matrix by a matrix.
  • In[9]:= m . m

    Out[9]=

  • Or a vector by a matrix.
  • In[10]:= v . m

    Out[10]=

  • This combination makes a scalar.
  • In[11]:= v . m . v

    Out[11]=

    Because of the way Mathematica uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors.


    Functions for vectors.


    Functions for matrices.










  • This builds a matrix with elements


    .
  • In[12]:= s = Table[i+j, {i, 3}, {j, 3}]

    Out[12]=

  • This displays s in standard two-dimensional matrix format.
  • In[13]:= MatrixForm[s]

    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.
  • In[14]:= Array[a, 4]

    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.
  • In[16]:= Dimensions[%]

    Out[16]=




  • This generates a


    diagonal matrix.
  • In[17]:= DiagonalMatrix[{a, b, c}]

    Out[17]=


    Some mathematical operations on matrices.




  • Here is the


    matrix of symbolic variables that was defined above.
  • In[18]:= m

    Out[18]=

  • This gives its determinant.
  • In[19]:= Det[m]

    Out[19]=

  • Here is the transpose of m.
  • In[20]:= Transpose[m]

    Out[20]=

  • This gives the inverse of m in symbolic form.
  • In[21]:= Inverse[m]

    Out[21]=




  • Here is a particular


    rational matrix known as a "Hilbert matrix".
  • In[22]:= h = Table[1/(i+j-1), {i, 3}, {j, 3}]

    Out[22]=

  • This gives its inverse.
  • In[23]:= Inverse[h]

    Out[23]=

  • Taking the dot product of the inverse with the original matrix gives the identity matrix.
  • In[24]:= % . h

    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.
  • In[26]:= Eigenvalues[r]

    Out[26]=

  • This gives a numerical approximation to the matrix.
  • In[27]:= rn = N[r]

    Out[27]=

  • Here are numerical approximations to the eigenvalues.
  • In[28]:= Eigenvalues[rn]

    Out[28]=

    Section 3.7 discusses other matrix operations that are built into Mathematica.



    Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
    THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
    SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.