# Introduction to Linear Algebra 

## Vectors

In the introduction to calculus, we made a distinction between vector and scalar quantities. Vector quantities involve both direction and magnitude (in our case distance), thus we represented them geometrically, with arrows. For two vectors to be equal, their arrows must point in the same direction and have the same length.  For that reason, the vectors on the image below are all different from each other.



### Conventions of Representation 

#### Cartesian Representation

In linear algebra, we deal extensively with vectors, and we need a convention to represent them. We will start by rooting every vector's tail to the coordinate origin of the space. In the case of two-dimensional space, we will represent vectors as arrows whose tail is fixed at the coordinate $(0,0)$. Their tip will then land at a certain point in space, which we can capture it with a pair of coordinates $(x,y)$, as shown in the illustration below.



We have an analogous situation in the 3-dimensional space. A vector's tail is fixed at the coordinate centre $(0,0,0)$ and we note where its tip lands using 3 coordinates $(x,y,z)$. 



#### Symbolic Representation

By using this convention, each vector becomes uniquely associated with a list of numbers containing its tip's coordinates. Thus, we can deduce that two vectors (rooted at the origin) are equal if their their tip lands on the same coordinate in space. To represent a vector numerically, we will introduce another convention and write them as a vertical arrangement of numbers (coordinates) within square brackets. To each spatial dimension corresponds a single row. The convention is that rows go from the top to bottom, and dimensions from lower to higher, thus $x$-coordinate is represented with the first row, $y$ with the second, $z$ with the third, and so on. The five vectors from the illustrations above can be represented as:

$$\vec{a}=\begin{bmatrix} 3\\4\end{bmatrix}; \quad 
\vec{b}=\begin{bmatrix} 2\\-1\end{bmatrix}; \quad 
\vec{c}=\begin{bmatrix} -3\\-3\end{bmatrix}; \quad 
\vec{d}=\begin{bmatrix} -2\\3\end{bmatrix}; \quad
\vec{e}=\begin{bmatrix} 2\\3\\5\end{bmatrix}
$$

#### Representing vectors in Python

To represent vectors in Python programming language, we will use the linear algebra library called NumPy. To import it we write the following line of code:


```python
import numpy as np
```

Now we can use NumPy's data structure called array to represent vectors:


```python
a = np.array([[3],
              [4]])

b = np.array([[2],
              [-1]])

c = np.array([[-3],
              [-3]])

d = np.array([[-2],
              [3]])

e = np.array([[2],
              [3],
              [5]])
```

With Python's print command, we can display our variables and confirm that the vectors we defined are indeed represented as a vertical arrangement of numbers—coordinates. Let us print the vector $\vec{a}$:


```python
print (a)
```

    [[3]
     [4]]



```python
print (e)
```

    [[2]
     [3]
     [5]]


#### Computing the magnitude

From the coordinates, we can easily derive a vector's direction and magnitude (length). For example, to find the length of the vector $\vec{a}$, we can take the square root of the sum of its coordinates squared.  This, of course, comes from applying the Pythagorean theorem.



$$\|\vec{a}\|=\sqrt{3^2+4^2}=\sqrt{25}=5$$

The same procedure applies to 3-dimensional vectors such is $\vec{e}$, but also scales seamlessly into higher dimensions!

$$\|\vec{e}\|=\sqrt{2^2+3^2+5^2}=\sqrt{38}\approx6.16$$

To compute this in NumPy, we can use NumPy's built-in function `linalg.norm`:


```python
np.linalg.norm(a)
```




    5.0




```python
np.linalg.norm(e)
```




    6.164414002968976



### Vector Operations

The two elementary vector operations are adding (and subtracting) vectors, and multiplying a vector by a constant.

#### Vector addition

First, let's examine the vector addition from the geometric point of view. The animation below shows how we can add the vector $\vec{b}$ to $\vec{a}$. Both vectors are first rooted at the origin. Then we translate the vector $\vec{b}$ such that its tail lands at the tip of the vector $\vec{a}$. The resulting vector $\vec{a+b}$ has its tail at the origin, while its tip points at the same place as the translated vector $\vec{b}$.



What is interesting is that the same result can be achieved if we carried out the addition the other way around—if we translated $\vec{a}$ such that its tail landed on the tip of the vector $\vec{b}$. This tells us that the vector addition is __commutative__ or in other words: $\vec{a}+\vec{b} = \vec{b}+\vec{a}$.

#### Multiplying a vector by a scalar

Multiplying a vector by a scalar does not affect vector's direction, rather its magnitude and orientation. The magnitude of the scalar will change the vector's magnitude. If we multiply a vector  $\vec{u}$ by the number 2, the resulting vector will be twice as long. If we multiply it by 1/2 or 0.5, it will be half as long. The sign of the scalar (negative or positive number) will by itself not change its magnitude but it will change its direction. If we multiply the vector $\vec{u}$ by -1, the resulting vector will be of the same length but it will point in the opposite direction.



In the animation below, we are multiplying the vector $\vec{u}$ by the scalar $a$, whose value is oscillating between -2 and 2. This, in turn, scales and flips the resulting vector $a\vec{u}$.



##### Vector subtraction

We can think of vector subtraction in terms of two operations we already know how to do: vector addition and multiplication of a vector by a (negative) scalar. Instead of subtracting $\vec{b}$ from $\vec{a}$, we can simply add $\vec{a}$ to $-\vec{b}$, which is $\vec{b}$ multiplied by -1 . In geometric terms, we would be adding the reversed vector $\vec{b}$ to the vector $\vec{a}$, and call this subtraction.



##### Adding vectors numerically

Adding and subtracting vectors numerically is quite similar to adding and subtracting numbers. What changes is that we have more than one number representing each vector that we need to add/subtract. In the case of two-dimensional vectors, we simply need to add or subtract vectors' respective $x$ and $y$ components. In case of n-dimensional vectors, we have $n$ numbers to add/subtract. To perform these operations, the vectors need to match: one can only add/subtract vectors of the same size (dimensionality). The consequence of this rule is that the final vector will preserve the dimensionality of the vectors we are adding or subtracting.

To add the vectors $\vec{a}$ and $\vec{b}$, we simply add $\vec{a}$'s $x$ coordinate 3 to $\vec{b}$'s $x$ coordinate 2. The resulting vector's $x$ coordinate will be $3+2=5$. The same procedure is then applied to all vector's dimensions.

\begin{align}\vec{a}+\vec{b}&=\begin{bmatrix} 3\\4\end{bmatrix}+\begin{bmatrix} 2\\-1\end{bmatrix}=\begin{bmatrix} 3+2\\4+(-1)\end{bmatrix}\\\\
\vec{a}+\vec{b}&=\begin{bmatrix} 5\\3\end{bmatrix}
\end{align}

We can express this in a general form (for 2-dimensional vectors) as:

\begin{align}\vec{a}+\vec{b}&=\begin{bmatrix} a_x+b_x\\a_y+b_y\end{bmatrix}
\end{align}

#####  Multiplying a vector by a scalar numerically

To multiply a vector by a scalar, we simply multiply each of the vector's components by that scalar. 

\begin{align}
3\vec{a}&=3\cdot\begin{bmatrix} 3\\4\end{bmatrix}=\begin{bmatrix} 3\cdot3\\3\cdot4\end{bmatrix}\\\\
3\vec{a}&=\begin{bmatrix} 9\\12\end{bmatrix}
\end{align}

We can express this in a general form (for two dimensional vectors) as:

\begin{align}
p\vec{a}&=\begin{bmatrix} p a_x\\p a_y\end{bmatrix}
\end{align}

##### Subtracting vectors numerically

To subtract the vector $\vec{b}$ from $\vec{a}$, we can do the same as we did geometrically: reverse the vector $\vec{b}$'s direction and add it to $\vec{a}$. To achieve this numerically, we need to multiply the vector $\vec{b}$'s components by -1.

\begin{align}
\vec{a}-\vec{b}&=\vec{a}+(-1\vec{b})\\\\
\vec{a}-\vec{b}&=\begin{bmatrix} 3\\4\end{bmatrix}+(-1)\begin{bmatrix} 2\\-1\end{bmatrix}=\begin{bmatrix} 3\\4\end{bmatrix}+\begin{bmatrix} -2\\1\end{bmatrix}\\\\
\vec{a}-\vec{b}&=\begin{bmatrix} 3-2\\4+1\end{bmatrix}\\\\
\vec{a}-\vec{b}&=\begin{bmatrix} 1\\5\end{bmatrix}
\end{align}

We can express this in a general form (for two dimensional vectors) as:

\begin{align}\vec{a}-\vec{b}&=\begin{bmatrix} a_x-b_x\\a_y-b_y\end{bmatrix}
\end{align}

The same principle is not specific to vector's dimensionality and it applies to vectors of any size.

#####  Vector dot product

Geometrically, the dot product of vectors $\vec{a}$ and $\vec{b}$ is defined as a product of vectors' lengths and the cosine of an angle between them.

$$\vec{a} \cdot \vec{b}= \|\vec{a}\| \;\|\vec{b}\| \; cos(\theta)$$ 



The dot product is useful because it can be used to find an angle between any two vectors or determine the length of the projection of one vector onto another. Those methods, however,  will not be covered in this presentation. <br> By looking at the dot product formula we can deduce that it will be a scalar quantity, since the lengths and the cosine of the angle $\theta$ are all scalars. Luckily, to compute this number, there is a very convenient numerical procedure. We need to multiply the vectors' respective components elementwise, and sum them together. 

\begin{align}
\vec{a}\cdot\vec{b}&=\begin{bmatrix} 3\\4\end{bmatrix}\cdot\begin{bmatrix} 2\\-1\end{bmatrix} = 3\cdot2 + 4\cdot(-1)\\\\
\vec{a}\cdot\vec{b}&=2
\end{align}

We can express this in a general form (for 2-dimensional vectors) as:

\begin{align}\vec{a}\cdot\vec{b}&=a_x b_x+a_y b_y
\end{align}

Multiplying vector components elementwise and adding them takes part in the Pythagorean theorem. To find the length of the vector $\vec{e}$ we squared each of its components and took the square root of their sum.

$$\|\vec{e}\|=\sqrt{2^2+3^2+5^2}=\sqrt{38}\approx6.16$$

We can generalise this procedure by using dot product:

$$\|\vec{e}\|^2=\vec{e}\cdot\vec{e}$$

This gives us:

\begin{align}
\|\vec{e}\|^2&=\begin{bmatrix} 2\\3\\5\end{bmatrix}\cdot \begin{bmatrix} 2\\3\\5 \end{bmatrix} \\\\
\|\vec{e}\|^2&= 2^2+3^3+5^5 = 38 \\\\
\|\vec{e}\|&= \sqrt{38}\approx6.16
\end{align}

----------

##### Adding, subtracting and multiplying vectors by using code

All of these operations are very easy to perform by using code. To add the vectors $\vec{a}$ and $\vec{b}$, we simply type:


```python
a+b
```




    array([[5],
           [3]])



To make the output look somewhat nicer, we can use the `print` command:


```python
print (a+b)
```

    [[5]
     [3]]


To subtract $\vec{b}$ from $\vec{a}$, we can write:


```python
print (a-b)
```

    [[1]
     [5]]


To multiply the vector $\vec{a}$ by a scalar, we simply write:


```python
print (5*a)
```

    [[15]
     [20]]


##### Multiplying vectors elementwise

In the light of vector addition and scalar multiplication, we can ask, what would happen if we typed the command `a*b`? 


```python
a*b
```




    array([[ 6],
           [-4]])



This command multiplies vectors' respective coordinates, similar to vector addition, thus produces the product vector of the same size as $\vec{a}$ and $\vec{b}$. Although this operation is very useful in computer science, it is somewhat obscured in elementary mathematics. It is known as the __Hadamard product__, and denoted as $\vec{a}\odot \vec{b}$. In our case:

\begin{align}
\vec{a}\odot \vec{b}&=\begin{bmatrix}3\\4\end{bmatrix}\odot\begin{bmatrix}2\\-1\end{bmatrix}=\begin{bmatrix}3\cdot 2\\4\cdot (-1)\end{bmatrix}\\\\
\vec{a}\odot \vec{b}&=\begin{bmatrix}6\\-4\end{bmatrix}
\end{align}

We can express this in a general form (for two dimensional vectors) as:

\begin{align}\vec{a}\odot \vec{b}&=\begin{bmatrix} a_x \, b_x\\a_y \, b_y\end{bmatrix}
\end{align}

##### Computing the dot product by using code

To compute the dot product of $\vec{a}$ and $\vec{b}$ we can multiply two vectors elementwise and then sum the multiples.


```python
np.sum(a*b)
```




    2



This also gives us an alternative way to compute the length of a vector. We simply multiply the vector by itself elementwise, sum the multiples and compute the square root of the sum. To compute the length of the vector $\vec{a}$ instead of using the function `linalg.norm` we can type:


```python
np.sqrt(np.sum(a*a))
```




    5.0



### Basis Vectors

Amid all the possible vectors that can be represented in a space of given dimensionality, some vectors are more unique than the others. They are called __basis vectors__, and we can think of them as the elementary units constituting the given vector space. They emerge from the vector's scaling ability. Let's illustrate this with an example. Let's start with the vector $\vec{a}$ whose coordinates are (3,4). Now, let's introduce two new vectors: $\boldsymbol{\hat{\imath}}$, parallel with the $x$-axis and $\boldsymbol{\hat{\jmath}}$ parallel to the $y$-axis. The most important property of $\boldsymbol{\hat{\imath}}$ (i-hat) and $\boldsymbol{\hat{\jmath}}$ (j-hat) is that their magnitude (length) is equal to one. 



We can represent them numerically as:

\begin{align}
\boldsymbol{\hat{\imath}}=\begin{bmatrix} 1\\0\end{bmatrix};  \quad \boldsymbol{\hat{\jmath}}=\begin{bmatrix} 0\\1\end{bmatrix}
\end{align}

Vector $\vec{a}$'s horizontal projection is 3 times as long as $\boldsymbol{\hat{\imath}}$'s and they point in the same direction. Vector $\vec{a}$'s vertical projection is 4 times as long as $\boldsymbol{\hat{\jmath}}$'s and they point in the same direction. By scaling the basis vectors $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$ (multiplying them by 3 and 4 respectively) and then adding them, we can recreate the vector $\vec{a}$! This is shown in the illustration below:



Since the vectors $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$ are orthogonal to each other, their dot product will be equal to zero. 

\begin{align}
\boldsymbol{\hat{\imath}} \cdot \boldsymbol{\hat{\jmath}} &= \begin{bmatrix} 1\\0 \end{bmatrix} \cdot \begin{bmatrix} 0\\1 \end{bmatrix} \\\\
\boldsymbol{\hat{\imath}} \cdot \boldsymbol{\hat{\jmath}} &= 1 \cdot 0 + 0 \cdot 1 = 0
\end{align}

### Linear combination of Vectors

We can write the vector $\vec{a}$ symbolically as $\vec{a}=3\boldsymbol{\hat{\imath}}+4\boldsymbol{\hat{\jmath}}$. Not only can we represent the vector $\vec{a}$ by means of $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$, but also any possible two-dimensional vector!

$$
\vec{a}=3\boldsymbol{\hat{\imath}}+4\boldsymbol{\hat{\jmath}}\\
\vec{b}=-2\boldsymbol{\hat{\imath}}+1\boldsymbol{\hat{\jmath}}\\
\vec{c}=-3\boldsymbol{\hat{\imath}}-3\boldsymbol{\hat{\jmath}}\\
\vec{d}=-2\boldsymbol{\hat{\imath}}+3\boldsymbol{\hat{\jmath}}\\
$$

In doing so, we are describing these vectors as a __linear combination__ of vectors $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$.This idea seamlessly extends into higher dimensions. For three dimensions, we need another basis vector parallel to the Z-axis, which is usually denoted as $\boldsymbol{\hat{k}}$ (k-hat). 



Now we can describe $\vec{e}$ as a linear combination of $\boldsymbol{\hat{\imath}}$, $\boldsymbol{\hat{\jmath}}$ and $\boldsymbol{\hat{k}}$:
$$\vec{e}=2\boldsymbol{\hat{\imath}}+3\boldsymbol{\hat{\jmath}}+5\boldsymbol{\hat{k}}$$

Vectors $\boldsymbol{\hat{\imath}}$, $\boldsymbol{\hat{\jmath}}$ and $\boldsymbol{\hat{k}}$ are __orthogonal__ to each other and their linear combinations can describe any vector in their respective space. For example, the linear combination of $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$ can describe any two dimensional vector, and the linear combination of $\boldsymbol{\hat{\imath}}$, $\boldsymbol{\hat{\jmath}}$ and $\boldsymbol{\hat{k}}$ can describe any 3-dimensional vector.  This is possible precisely because neither one of the vectors themselves can be described by combining the other two. This makes sense, as $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$ alone simply cannot describe anything stretching in the $z$-direction. We call the vectors that have this property, __linearly independent vectors__. 

On the other side, __linearly dependent__ vectors in a set of vectors would be those that can be described by a linear combination of the others. Let's see an example. The image below contains 3 __unit vectors__—vectors whose length is equal to one, located on a plane.



To describe any vector in this plane, one vector is redundant! A linear combination of the vectors $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{\jmath}}$ can describe the vector $\boldsymbol{\hat{p}}$.

$$\boldsymbol{\hat{p}} = -0.88\boldsymbol{\hat{\imath}} - 0.47\boldsymbol{\hat{\jmath}} $$



To describe any point in 2-dimensional space, we do need exactly two vectors, but they need not necessarily be orthogonal to each other! They only must not be parallel (If two vectors are parallel, then each of them can be used to describe the other). For example, we can describe $\boldsymbol{\hat{\jmath}}$ as a linear combination of $\boldsymbol{\hat{\imath}}$ and $\boldsymbol{\hat{p}}$.

$$\boldsymbol{\hat{\jmath}} = -1.87\boldsymbol{\hat{\imath}} - 2.13\boldsymbol{\hat{p}} $$



And we can also describe $\boldsymbol{\hat{\imath}}$ as a linear combination of $\boldsymbol{\hat{\jmath}}$ and $\boldsymbol{\hat{p}}$.

$$\boldsymbol{\hat{\imath}} = -1.14\boldsymbol{\hat{p}} - 0.52\boldsymbol{\hat{\jmath}} $$



If we have two vectors $\vec{u}$ and $\vec{v}$, a set of all vectors that can be reached with this linear combination is called the __span__ of the vectors $\vec{u}$ and $\vec{v}$. To fully span two-dimensional vector space requires two-linearly independent vectors. If these two vectors are linearly dependent, they do not fully span the two-dimensional space, rather only a single line in this space. The animation below aims to illustrate how a linear combination of two linearly independent non-orthogonal vectors $a\vec{u}+b\vec{v}$ can be used to reach any 2-dimensional vector.



The same happens in higher dimensions. Fully spaning a 3-dimensional vector space requires 3 linearly independent vectors. If 2 out of 3 vectors are linearly dependent, their span will be a 2-D plane in 3-D space, like in the animation below. If all three are linearly dependent, their span will be a line in 3-dimensional space. Now we can give a technical definition of a __vector basis__: It is a set of linearly independent vectors that span the full space.



f234a27090abd2abf19f08d05c6797fe4c01df1a
ipynb
Jupyter Notebook
09. Introduction to Vectors.ipynb
Mistrymm7/machineintelligence
7629d61d46dafa8e5f3013082b1403813d165375
Apache-2.0
2019-09-23T11:25:41.000Z
2022-03-29T22:56:10.000Z
09. Introduction to Vectors.ipynb
Iason-Giraud/machineintelligence
b34a070208c7ac7d7b8a1e1ad02813b39274921c
Apache-2.0
09. Introduction to Vectors.ipynb
Iason-Giraud/machineintelligence
b34a070208c7ac7d7b8a1e1ad02813b39274921c
Apache-2.0
2019-09-30T16:08:46.000Z
2022-02-19T10:29:07.000Z
Qwen/Qwen-72B
1. YES
2. YES