This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Advanced Mathematics / Linear Algebra  /

3.7.4 Operations on Scalars, Vectors and Matrices

Most mathematical functions in Mathematica are set up to apply themselves separately to each element in a list. This is true in particular of all functions that carry the attribute Listable.
A consequence is that most mathematical functions are applied element by element to matrices and vectors.

  • The Log applies itself separately to each element in the vector.
  • In[1]:= Log[ {a, b, c} ]

    Out[1]=

  • The same is true for a matrix, or, for that matter, for any nested list.
  • In[2]:= Log[ {{a, b}, {c, d}} ]

    Out[2]=

  • The differentiation function D also applies separately to each element in a list.
  • In[3]:= D[ {x, x^2, x^3}, x ]

    Out[3]=

  • The sum of two vectors is carried out element by element.
  • In[4]:= {a, b} + {ap, bp}

    Out[4]=

  • If you try to add two vectors with different lengths, you get an error.
  • In[5]:= {a, b, c} + {ap, bp}

    Thread::tdlen: Objects of unequal length in {a, b, c} + {ap, bp} cannot be combined.

    Out[5]=

  • This adds the scalar 1 to each element of the vector.
  • In[6]:= 1 + {a, b}

    Out[6]=

  • Any object that is not manifestly a list is treated as a scalar. Here c is treated as a scalar, and added separately to each element in the vector.
  • In[7]:= {a, b} + c

    Out[7]=

  • This multiplies each element in the vector by the scalar k.
  • In[8]:= k {a, b}

    Out[8]=

    It is important to realize that Mathematica treats an object as a vector in a particular operation only if the object is explicitly a list at the time when the operation is done. If the object is not explicitly a list, Mathematica always treats it as a scalar. This means that you can get different results, depending on whether you assign a particular object to be a list before or after you do a particular operation.

  • The object p is treated as a scalar, and added separately to each element in the vector.
  • In[9]:= {a, b} + p

    Out[9]=

  • This is what happens if you now replace p by the list {c,d}.
  • In[10]:= % /. p -> {c, d}

    Out[10]=

  • You would have got a different result if you had replaced p by {c,d} before you did the first operation.
  • In[11]:= {a, b} + {c, d}

    Out[11]=