Skip to main content
Mathematics LibreTexts

3.3: Scalars, Vector and Tensors

  • Page ID
    63612
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    The two primary mathematical entities that are of interest in linear algebra are the vector and the matrix. They are examples of a more general entity known as a tensor. The following video gives a basic introduction of scalars, vectors, and tensors. It is fine if you can not understand all of this video. We will learn most of it in this course.

    Note

    The terms vectors and tensors can get fairly confusing. For the purpose of this class, we will not use the term Tensor all that much. Instead we will treat everything as vectors (more on this later).

    from IPython.display import YouTubeVideo
    YouTubeVideo("ml4NSzCQobk",width=640,height=360, cc_load_policy=True)

    Think of a scalar as a single number or variable that is an example of a 0th-order tensor. The following are all scalars:

    \[ 1, \frac{1}{2}, 3.1416 \nonumber \]

    Defining a scalar in Python is easy. For example

    a = 8
    a
    8

    A vector, on the other hand, is an ordered list of values which we typically represent with lower case letters. Vectors are ordered arrays of single numbers and are an example of 1st-order tensor. The following are all vectors:

    Row Vector: \( v = [v_1, v_2, \dots, v_n] \) here \(v_1\), \(v_2\), \(\dots\), \(v_n\) are single numbers.

    \[ f = [1,2,3,5,8] \nonumber \]

    Here \(f\) in the above example is a vector of numbers, and it is the common way we think of vectors.

    Note, it is often more common to write vecors vertically. These are often called column vectors:

    Column Vector: \( v = \begin{bmatrix} v_1 \cr v_2 \cr \vdots \cr v_m \end{bmatrix} \) here \(v_1\), \(v_2\), \(\dots\), \(v_m\) are single numbers.

    Introducing Vectors in Python

    In Python, there are multiple ways to store a vector. Knowing how your vector is stored is very important (especially for debugging). Probably the easiest way to store a vector is using a list, which are created using standard square brackets as follows:

    f = [1, 2, 3, 5, 8]
    f
    [1, 2, 3, 5, 8]

    Another common way to store a vector is to use a tuple.

    b = (2, 4, 8, 16)
    b
    (2, 4, 8, 16)

    You can access a particular scalar in your Python object using its index. Remember that Python index starts counting at zero. For example, to get the fourth element in f and b vectors, we would use the following syntax:

    print(f[3])
    print(b[3])
    5
    16
    

    Later in this course, we may discuss which data format is better (and introduce new ones). At this point let’s not worry about it too much. You can always figure out a variable’s data type using the type function. For example:

    type(f)
    list
    type(b)
    tuple

    Finally, I am not sure if you will need this but always remember, it is easy to convert from a tuple to a list and vice versa (this is called “casting”):

    #Convert tuple to list
    b_list = list(b)
    b_list
    [2, 4, 8, 16]
    #Convert list to tuple
    f_list = tuple(f)
    f_list
    (1, 2, 3, 5, 8)

    Vector size

    A vector can be used to represent quantities or values in an application. The size (also called dimension or length) of the vector is the number of elements it contains. The size of the vector determines how many quantities are in the vector. We often refer to the size of a vector using the variable n. So an n-vector has n values. A 3-vector only has 3 values.

    The length (len) function returns the size of a vector in Python:

    len(f)
    5
    len(b)
    4

    Special Vectors

    The following are special vectors with special names.

    Standard Unit Vectors

    Vectors with a single 1 and the rest of the values are zero have a special name called “Standard Unit Vectors”. The number of different standard unit vectors there are is equal to the size of the vector. For example, a 3-vector has the following standard unit vectors:

    \[ e_1 = \begin{bmatrix} 1 \cr 0 \cr 0 \end{bmatrix}, e_2 = \begin{bmatrix} 0 \cr 1 \cr 0 \end{bmatrix}, e_3 = \begin{bmatrix} 0 \cr 0 \cr 1 \end{bmatrix} \nonumber \]

    Zero Vectors

    Vectors with all values of zero also have a special name called “Zero Vectors”. Typically we just use a zero to represent the zero vector. For example: \( 0 = \begin{bmatrix} 0 \cr 0 \cr 0 \end{bmatrix} \)

    Examples

    Vectors are used to represent all types of data that has structures. Here are some simple examples from the Boyd and Vandenberghe textbook:

    Location and displacement

    A 2-vector can be used to represent a position or location in a space. The first value is the distance in one direction (from the origin) and the second value is the distance in a different direction. Probably most students are famiar with the 2D Cartesian coordinate system where a location can be defined by two values in the x and y directions. Here is a simple scatter plot in python which show the concept:

    %matplotlib inline
    import matplotlib.pylab as plt
    p1 = [2, 1]
    p2 = [1, 3]
    p3 = [1, 1]
    
    plt.plot(p1[0],p1[1],'*k')
    plt.plot(p2[0],p2[1],'*k')
    plt.plot(p3[0],p3[1],'*k')
    
    ## Add some labels (offset slightly)
    plt.text(p1[0]+0.1,p1[1],'$p_1$')
    plt.text(p2[0]+0.1,p2[1],'$p_2$')
    plt.text(p3[0]+0.1,p3[1],'$p_3$')
    
    ## Fix the axis so you can see the points
    plt.axis([0,4,0,4])
    
    ## 'Run' this cell to view the plot
    (0.0, 4.0, 0.0, 4.0)

    Color

    A 3-vector can represent a color, with its entries giving the Red, Green, and Blue (RGB) intensity values (often between 0 and 1). The vector (0,0,0) represents black, the vector (0, 1, 0) represents a bright pure green color, and the vector (1, 0.5, 0.5) represents a shade of pink.

    The Python matplotlib library uses this type of vector to define colors. For example, the following code plots a point at the origin of size 10000 (the size of the circle, and the value does not have exact meaning here) and color c = (0,1,0). You can change the values for c and s to see the difference.

    import warnings
    warnings.filterwarnings("ignore")
    
    c = (0, 1, 0)
    plt.scatter(0,0, color=c, s=10000);

    Just for fun, here is a little interactive demo that lets you play with different color vectors.

    Note

    This demo uses the ipywidgets Python library which works by default in Jupyter notebook (which is installed on the MSU jupyterhub)) but NOT in the newer jupyter lab interface which some students may have installed on their local computers. To get these types of examples working in jupyter lab requires the installation of the ipywidgets plug-in.

    %matplotlib inline
    import matplotlib.pylab as plt
    from ipywidgets import interact, fixed
    
    def showcolor(red,green,blue):
        color=(red,green,blue)
        plt.scatter(0,0, color=color, s=20000);
        plt.axis('off');
        plt.show();
        return color
    
    color = interact(showcolor, red=(0.0,1.0), green=(0.0,1.0), blue=(0.0,1.0));
    

    Vector Addition

    Two vectors of the same size can be added together by adding the corresponding elements, to form another vector of the same size, called the sum of the vectors. For example:

    \[ \begin{bmatrix} 1 \cr 20 \end{bmatrix} + \begin{bmatrix} 22 \cr -3 \end{bmatrix} = \begin{bmatrix} 23 \cr 17 \end{bmatrix} \nonumber \]

    Python Vector Addition

    Here is where things get tricky in Python. If you try to add a list or tuple, Python does not do the vector addition as we defined above. In the following examples, notice that the two lists concatenate instead of adding by element:

    ## THIS IS WRONG
    a = [1, 20]
    b = [22,-3]
    c = a+b
    c
    [1, 20, 22, -3]
    ## THIS IS ALSO WRONG
    a = (1, 20)
    b = (22,-3)
    c = a+b
    c
    (1, 20, 22, -3)

    To do proper vector math you need either use a special function (we will learn these) or loop over the list. Here is a very simplistic example:

    a = (1, 20)
    b = (22,-3)
    c = []
    for i in range(len(a)):
        c.append(a[i] + b[i])
    c
    [23, 17]

    For fun, we can define this operation as a function in case we want to use it later:

    def vecadd(a,b):
        """Function to add two equal size vectors."""
        if (len(a) != len(b)):
            raise Exception('Error - vector lengths do not match')
        c = []
        for i in range(len(a)):
            c.append(a[i] + b[i])
        return c
    #Lets test it
    
    vecadd(a,b)
    [23, 17]

    Scalar-Vector multiplication

    You can also multiply a scalar by a vector, which is done by multiplying every element of the vector by the scalar.

    \[ 3 \begin{bmatrix} 3 \cr -7 \cr 10 \end{bmatrix} = \begin{bmatrix} 9 \cr -21 \cr 30 \end{bmatrix} \nonumber \]

    Scalar-Vector Multiplication in Python

    Again, this can be tricky in Python because Python lists do not do what we want. Consider the following example that just concatenates three copies of the vector.

    ##THIS IS WRONG## 
    z = 3
    a = [3,-7,10]
    c = z*a
    c
    [3, -7, 10, 3, -7, 10, 3, -7, 10]

    Again, in order to do proper vector math in Python you need either use a special function (we will learn these) or loop over the list.

    Do This

    See if you can make a simple function with a loop to multiply a scalar by a vector. Name your function sv_multiply and test it using the cells below:

    #put your sv_multiply function here
    
    # 'Run' this cell afterwards to register your sv_multiply function
    #Test your function here
    z = 3
    a = [3,-7,10]
    sv_multiply(z,a)

    Let us use the following code to test your functon further. Note that this uses the anchsercheck function provided by your instructors. Please review Python Linear Algebra Packages for instructions on installing and using answercheck.

    from answercheck import checkanswer
    checkanswer.vector(sv_multiply(10,[1,2,3,4]),'414a6fea724cafda66ab5971f542adf2')
    
    from answercheck import checkanswer
    checkanswer.vector(sv_multiply(3.14159,(1,2,3,4)),'f349ef7cafae77c7c23d6924ec1fd36e')
    

    This page titled 3.3: Scalars, Vector and Tensors is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform.

    • Was this article helpful?