02 In-Class Assignment: Vectors#


1. Pre-class assignment review#

Vector Addition Properties#

For any vectors x, y, and z of the same size/dimension, we have the following properties:

  1. Vector addition is commutative: x + y = y + x.

  2. Vector addition is associative: (x + y) + z = x + (y + z). We can therefore write both as x + y + z.

  3. Adding the zero vector to a vector results in the original vector: x + 0 = 0 + x = x. (This is an example where the size of the zero vector follows from the context, i.e., its size must be the same as the size of x)

  4. x x = 0. Subtracting a vector from itself yields the zero vector. (Here too the size of 0 is the size of x.)

Scalar-Vector Multiply Properties#

For any vectors x, y, and scalars a, b, we have the following properties

  • Scalar-vector multiply is commutative: ax  = x * a; This means that scalar-vector multiplication can be written in either order.

  • Scalar-vector multiply is associative: (ab)x = a(bx)

  • Scalar-vector multiply is distributive: a(x + y) = ax + ay, (x+y)a = xa + ya, and (a+b)x = ax + bx.

DO THIS: Discuss which of the following operations are valid operations in Linear Algebra. In the case when an operation is well defined, determine the result. In the case when an operation is not well defined, explain why.

  • \((-\pi, 3, \sqrt{2}) + \pi\)

  • \((-\pi, 3, \sqrt{2}) + (0,1,2)\)

  • \(\pi(7,2) + 3(0,0,0)\)

  • \(\pi(7,2,0) + 3(0,0,0)\)

  • \(2/(1,1,1)\)

  • \((1,1,1)/2\)

Put your answer here:

Note that we have not defined multiplication of two vectors yet! Soon we will define the dot product of two vectors of the same dimension, the result of which will be a scalar.

Linear Combinations#

Let \(x\) and \(y\) be two n-dimesional vectors. We can define the linear combination of these two vectors as \(ax + by\) where \(a\) and \(b\) are scalar coefficients. Since a vector space is closed under addition and scalar multipication, it is closed under linear combitations. Explain why!

DO THIS: Express the following as linear combinations of the vectors \(x\) and \(y\), i.e., find the coefficients \(a\) and \(b\) in the linear combination.

  • \(x+y\)

  • \(x-y\)

  • \(3x\)

  • \(y/2\)

Put your answer here:


2. Vectors in Python#

For those who are new to Python, there are many common mistakes happen in this course. Try to fix the following codes.

SyntaxError#

It means that the code does not make sense in Python. We would like to define a vector with four numbers.

DO THIS: Fix the following code to create three vectors with four numbers.

x = [1 2 3.4 4]
y = [1, 2, 3, 5]]
z = [[1, 2, 3, 6.3]

Although you may have been able to get rid of the error messages the answer to your problem may still not be correct. Throughout the semester we will be using a Python program called answercheck to allow you to check some of your answers. This program doesn’t tell you the right answer but it is intended to be used as a way to get immediate feedback and accelerate learning.

DO THIS: First we will need to download answercheck.py to your current working directory. You only really need to do this once, so if answercheck.py is already in your working directory, you can skip running the cell below. However, if you delete this file by mistake sometime during the semester, you can come back to this notebook and download it again by running the following cell:

from urllib.request import urlretrieve

urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
            'answercheck.py');

DO THIS: How just run the following command to see if you got x, y and z correct when you fixed the code above.

from answercheck import checkanswer

checkanswer([x,y,z],'e80321644979a873b273aebbbcd0e450');

NOTE make sure you do not change the checkanswer commands. The long string with numbers and letters is the secret code that encodes the true answer. This code is also called the HASH. Feel free to look at the answercheck.py code and see if you can figure out how it works?

Numpy#

Numpy is a common way to represent vectors, and you are suggested to use numpy unless otherwise specified. The benefit of numpy is that it can perform the linear algebra operations listed in the previous section.

For example, the following code uses numpy.array to define a vector of four elements.

import numpy as np
x_np = np.array([-1, 0, 2, 3.1])
x_np

Scalars versus 1-vectors#

In mathematics, 1-vector is considered as a scalar. But in Python, they are not the same.

x = 2.4
y = [2.4]
x == y
x == y[0]

Lists of vectors#

We have a list of numpy arrays or a list of list. In this case, the vectors can have different dimensions.

DO THIS: Modify the print statement using indexing to only print the value 3 from the list_of_vectors defined below.

x_np = np.array([-1,0, 2 , 3.1])
y_np = np.array([1,-1,3])
z_np = np.array([0,1])
list_of_vectors = [x_np,y_np,z_np]

print(list_of_vectors)

Indexing#

The index of a vector runs from 0 to \(n-1\) for a \(n\)-vector.

DO THIS: The following code tries to get the third element of x_np, which is the number 2.0. Fix this code to provide the correct answer.

print(x_np(3))

DO THIS: Replace only the third element of x_np with the number 20.0 such that the new values of x_np is [-1, 0, 20., 3.1]

# Replace the third element using 20.0, then the resulting element is 
#####Start your code here #####

#####End of your code here#####
print(x_np)
from answercheck import checkanswer

checkanswer(x_np,'993d5cbc6ddeb10776ed48159780a5d3');

There is a special index -1, which represents the last element in an array. There are several ways to get more than one consecutive elements.

  • x_np[1:3] gives the 2nd and 3rd elements only. It starts with the first index and ends before the second index. So the number of element is just the difference between these two numbers.

  • x_np[1:-1] is the same as x_np[1:3] for a 4-vector.

  • If you want the last element also, then you do not need to put the index, e.g., x_n[1:] gives all elements except the first one. You can do the same thing as the first one.

DO THIS: you are given a vector (x_np) of \(n\) elements, define a new vector (d) of size \(n-1\) such that \(d_i = x_{i+1}-x_i\) for \(i=1,\dots,n-1\). Hint try doing this without writing your own loop. You should be able to use simple numpy indexing as described above.

x_np = np.array([1,8,3,2,1,9,7])

## Put your answer to the above question here.
from answercheck import checkanswer

checkanswer(d,'14205415f0ed56e608d0a87e7253fa70');

Vector equality in numpy and list#

The relational operator (==, <, >, !=, etc.) can be used to check whether the vectors are same or not. However, they will act differently if the code is comparing numpy.array objects or a list. For example, in numpy, == checks whether the corresponding elements in the arrayare equal and returns an array whose components have True or False values. For list, == checks whether the corresponding components in the two lists are equal and returns a single True or False value. The result will be true only if all components in the first list are equal to the corresponding components in the second list.

x = [-1, 0, 2, 3.1]
y = x.copy()
y[2] = 20.2

x_np = np.array(x)
y_np = np.array(y)
x == y
np.array(x_np) == np.array(y_np)

Zero vectors and Ones vectors in numpy#

  • np.zeros(n) creates a vector of size n with all 0s

  • np.ones(n) creates a vector of size n with all 1s

DO THIS: Create a zero vector (called zero_np) with the same dimension as vector x_np. Create a ones vector (called ones+np) also with the same dimension as vector x_np.

x_np = np.array([-1, 0, 2, 3.1])
### Define zero_np and ones_np here 
from answercheck import checkanswer

checkanswer([zero_np, ones_np],'7f874c2e446786655ff96f0bbae8a0c6');

Vector addition and subtraction#

In this section, you will understand why we use numpy for linear algebra opeartions. If x and y are numpy arrays of the same size, we can have x + y and x-y for their addition and subtraction, respectively.

x_np = np.array([1,2,3])
y_np = np.array([100,200,300])

v_sum = x_np + y_np
v_diff = x_np - y_np

print (f'Sum of vectors: {v_sum}')
print (f'Difference of vectors: {v_diff}')

For comparison, we also put the addition of two lists below. Recall from the pre-class assignment, we have to define a function to add two lists for linear algebra.

DO THIS: Modify the following code to properly add and subtract the two lists. You can copy the relevant code from the pre-class assignment.

x = [1,2,3]
y = [100,200,300]


v_sum = x + y
v_diff = x - y

print (f'Sum of vectors: {v_sum}')
print (f'Difference of vectors: {v_diff}')

Scalar-vector addition in Python (not in LA!)#

A scalar-vector addition means that the scalar (or a 1-vector) is added to all elements of the vector. Note that this is not a valid operation in Linear Algebra, meaning that if

\[v=(1,2,3)\]

is a given vector, then

$\(v+5\)$

is not well defined! However,

\[v+(5,5,5)\]

is well defined.

DO THIS: Add a scalar 20.20 to all elements of the following vector x_np and store the result back into x_np

x_np = np.array([1.0,2.0,3.0])
from answercheck import checkanswer

checkanswer(x_np,'2f8cbcce405fa12b8608422ff28544bb');

Linear combination with Numpy#

In the following example, we are given two vectors (x_np and y_np), and two scalars (alpha and beta), we obtain the linear combination alpha*x_np + beta*y_np.

x_np = np.array([1,2])
y_np = np.array([3,4])
alpha = 0.5
beta = -0.8
c = alpha*x_np + beta*y_np
print(c)

We can also define a function lincomb to perform the linear combination.

DO THIS: Finish the following code for lincomb and compare the results we just get.

def lincomb(coef, vectors): 
    n = len(vectors[0])  # get the dimension of the vectors. note they have to be of the same dimension
    comb = np.zeros(n)   # initial the value with all zeros.
    ### Add code here to calculate the linear combination of the input vecotrs and the coefficients. 
    ### Note that the number of elements in coef should be equal to the number of elements in vectors
    return comb
from answercheck import checkanswer

combination = lincomb([alpha, beta], [x_np,y_np])

checkanswer(combination,'8bab7329c94f3e3cda423add411685c2');

We can also test the functions ourselves by using values for which we know the answer. For example, the following tests are multiplying by the zero scalars and adding the zero vector - know what these answers should be and can check them.

combination = lincomb([0, 0], [x_np,y_np])

combination == np.zeros(combination.shape)
combination = lincomb([2, 2], [combination,np.zeros(combination.shape)])

combination == 2*combination

If you want to check that all values in a numpy.array are the same you could convert it to a list or there is a method called alltrue which checks if everything is true. It is a good idea to use this method if vectors get big.

combination = lincomb([2, 2], [combination,np.zeros(combination.shape)])

np.alltrue(combination == 2*combination)

3. Linear Combinations Relation to Linear Systems#

In the following example we will start exploring the relationship between linear combinations of vectors and finding a solution of a linear system.

DO THIS: Given the following vectors \(\mathbf u\), \(\mathbf v\), and \(\mathbf b\), find scalars \(x\) and \(y\), so that \(x \mathbf u+y\mathbf v=\mathbf b\). In other words, we are trying to express the vector \(\mathbf b\) as a linear combination of the vectors \(\mathbf u\) and \(\mathbf v\). If the request is possible, find the values of \(x\) and \(y\) that satisfy it and state if they are unique. If it is impossible, explain why. It may be helpful to represent the vectors graphically.

  • \(\mathbf u = \left[\begin{array}{rr}1 \\ 1\end{array}\right], \qquad \mathbf v = \left[\begin{array}{rr}1 \\ -1\end{array}\right], \qquad \mathbf b = \left[\begin{array}{rr}3 \\ 5\end{array}\right]\)

  • \(\mathbf u = \left[\begin{array}{rr}1 \\ 2\end{array}\right], \qquad \mathbf v = \left[\begin{array}{rr}-2 \\ -4\end{array}\right], \qquad \mathbf b = \left[\begin{array}{rr}3 \\ 6\end{array}\right]\)

  • \(\mathbf u = \left[\begin{array}{rr}1 \\ 2\end{array}\right], \qquad \mathbf v = \left[\begin{array}{rr}-2 \\ -4\end{array}\right], \qquad \mathbf b = \left[\begin{array}{rr}3 \\ 5\end{array}\right]\)

###STARTFOOTER###


Congratulations, we’re done!#

Be sure to turn in this notebook as an .html file on D2L. If you finished the in-class assignment early, be sure to show it to an instructor/TA/LA before you leave.

Written by Dr. Dirk Colbry, Michigan State University and Dr. Ming Yan, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.