02 Pre-Class Assignment: Vectors
Contents
02 Pre-Class Assignment: Vectors#
Readings for this topic (Recommended in bold)#
Assignment Overview#
1. Introducing the Course Textbooks#
Student self guided learning through assigned readings are required for students to be successful. The course strives to use Open Educational Resources (OER) to help reduce financial burden on the students. To this end we have selected the following textbooks for reading assignments and supplemental examples:
Introduction to Applied Linear Algebra by Boyd and Vandenberghe
Linear Algebra by Jim Heffron
A First Course in Linear Algebra by Robert A. Beezer
DO NOT WORRY You will not be expected to read all three textbooks in this course! In fact, we try to keep the reading at a reasonable level and focus on problem solving. However, most students benefit from seeing material in multiple ways (Reading, Lecture, Practice, etc). Different students (and instructors) also prefer different writing styles and may learn better with different text (which is why we provide more than one).
Students are encouraged to review and become familiar with the style and layout of each text to act as a common reference for the course. If you get stuck on a topic try looking it up and reviewing it in one of the other texts. If you are still stuck you can search the Internet. You are strongly encouraged to ask your instructors questions, utilize the Piazza forum, and come to the help sessions, staffed by all MTH 314 instructors, GTAs, and ULAs. We are here to help you!!!
✅ Do This: Download a copy of each textbooks onto your preferred reading device and review the Table of Contents in each text.
As you can see each textbook approaches Linear algebra in a slightly different way. This variety reflects the different philosophies of teaching and different ways of individual learning. One way to evaluate the focus of a particular textbook is to look at its very first chapter. For Example:
The Beezer and Heffron texts start out with “Systems of linear Equations” and “Linear Systems.” These topics are basically the same idea with the focus of defining linear systems as just sets of “linear combinations”. Clearly this is a core concept and a good place to start.
The Boyd and Vandenberghe text choose to start with “Vectors”. In linear algebra the “vector” is a mathematical tool for which all of the mechanics of the math is built. Again, not a bad place to start.
In the first few assignments this course we will be looking at both concepts. You will want to learn and be able to identify linear systems and how to represent them as vectors.
✅ Question: Find three additional topics (besides “Linear Systems” and “Vectors”) that seem to be common between the three textbooks. You can probably assume that these topics will be important to our course as well.
Put your answer to the above question here.
2. Today’s Reading#
Quite a bit of this pre-class assignment about vectors is motivated from Chapter 1 of the Stephen Boyd and Lieven Vandenberghe Applied Linear Algebra book. This material may be review for some students and may be new for others. It is expected that students review the chapter to help them understand the material better.
✅ Do This: Review Sections 1.1, 1.2 and 1.3 in Boyd and Vandenberghe and become familiar with the contents and the basic terminology. If you find this material is difficult make sure you take advantage of the survey at the end of this assignment to ask your instructor questions about things that are confusing.
HINT Many computers and smart phones have a “read to me feature”. Some students find it helpful to download the pdf of the textbook and have the computer read to them out loud while they follow along.
3. Scalars, Vector and Tensors#
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 talk about scalars, vectors, and matrices (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 (it is an example of a 0th-order tensor). The following are all scalars:
Defining a scalar in Python is easy. For example
a = 8
a
A vector, for the purposes of this class, is an ordered list of scalars 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 scalars.
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= \left[ \begin{matrix} v_1 \\ v_2 \\ \vdots \\ v_m \end{matrix} \right] \)\( here \)v_1, v_2, \dots, v_n$ are scalars.
1.a: 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. Lists are created using standard square brackets as follows:
f = [1, 2, 3, 5, 8]
f
Another common way to store a vector is to use a tuple.
b = (2, 4, 8, 16)
b
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])
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)
type(b)
Finally, 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
#Convert list to tuple
f_list = tuple(f)
f_list
1.b: 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 components a vector has. We often refer to the size of a vector using the variable n
. So an n-vector
has n
components. A 3-vector
has 3 components.
The length (len
) function returns the size of a vector in Python:
len(f)
len(b)
1.c: 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, the zero vector in \(\mathbb R^3\) is
1.d: 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])
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));
2. \(\mathbb R^n\) as a Vector Space#
Read the following theoretical part and we’ll discuss this in detail in class.
Note that by \(\mathbb R^n\) we denote the \(n\)-tuples of real numbers. For example, \((10,20,3.2)\in \mathbb R^3\), \((0,-0.7)\in\mathbb R^2\), \((1,2,3,500)\in\mathbb R^4\).
In order for a set to be a vector space, we need to have defined addition (i.e., have defined how we add two vectors together) and scalar multiplication (i.e. we have defined how we multiply a vector by a scalar). A vector space is
Closed under addition
Closed under scalar multiplication
There are much more general vectors spaces than \(\mathbb R^n\) - vectos can be almost any type of object as long as it maintains the two above properties. We will get into this concept later in the semester. In the case of \(\mathbb R^n\), the above concepts can be described as follows:
Closed under addition means that if we add any two real vectors vectors (i.e. \(u,v \in \mathbb R^n\)) then the result is also in \(\mathbb R^n\)). Addition in \(\mathbb R^n\) is defined by \((u_1, u_2, \dots, u_n)+(v_1, v_2, \dots, v_n) = (u_1+v_1, u_2+v_2, \dots, u_n+v_n)\). Closure under addition is easy to understand - adding any two real n-vectors there is no way to get a result that is not also a real n-vector. A way to say this mathematically is as follows:
Closed under scalar multiplication means that if we have any scalar number (\(s \in \mathbb R\)) and we multiply it by a vector (\(v \in \mathbb R^n\)) then the result is also a vector in \(\mathbb R^n\). Since scalar multiplication is defined by \(s(v_1, v_2, \dots, v_n) = (sv_1, sv_2, \dots, sv_n)\) and multiplying a real number by a real number results in a real number this one is also true. Or we can say it as follows:
The following are some properties of vector addition and scalar multiplication for vectors \(u\) and \(v\) and scalars \(c\) and \(d\):
\(u + v = v + u\) Commutative property
\(u + (v + w) = (u + v) + w\) Associative property
\(u+0 = 0 + u = u\) Property of zero vector
\(u + (-u) = 0\) Property of the additive inverse
\(c(u+v) = cu + cv\) Distributive properties
\((c+d)u = cu+du\) Distributive Properties
\(c(du) = (cd)u\) Distributed Properties
\(1u = u\) Scalar multiplication by 1
2.a: Vector Addition#
Two vectors in \(\mathbb R^n\) (of the same size) can be added together by adding the corresponding elements, to form another vector in \(\mathbb R^n\), called the sum of the vectors. For example:
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
## THIS IS ALSO WRONG
a = (1, 20)
b = (22,-3)
c = a+b
c
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
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)
Caution: Scalar-vector addition is not a valid operation in Linear Algebra#
For example, consiider the vector \(v\) in \(\mathbb R^3\), given by
then
$\(v+5\)$
is not well defined! However,
is well defined.
1.b: 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.
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
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
#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 answercheck
function provided by your instructors. The cell below including answercheck.py
in your working directory.
import os.path
from urllib.request import urlretrieve
if not os.path.isfile("answercheck.py"):
urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 'answercheck.py');
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')
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
###STARTFOOTER###
Congratulations, we’re done!#
Be sure to complete the quiz on D2L for this PCA by 11:59pm ET the day before class. One of the questions will ask you to turn in this notebook as an .html file.