# Linear Algebra

## 2018/08/10

library(ggplot2)

The fundamental problem of linear algebra which is to solve a system of linear equations.The most important ease, and the simplest, is when the number of unknowns equals the number of equations.We have $$n$$ equations with $$n$$ unknowns.

$2x - y = 0 \\ -x + 2y = 3$

The matrix is below.

$\begin{bmatrix} 2 & 1 \\ -1 & 2 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 0 \\ 3 \end{bmatrix}$

The unknowns are $$x$$ and $$y$$. The matrix of coefficients is $$A$$, the vector of unknowns is $$x$$ and right hand side is the vector $$b$$.

$Ax = b$

#### Solve equation by elimination

The row picture.

1. The points that satisfies the equation $$2x - y = 0$$.

Asking the question what is $$X$$ when $$y = 0$$. It is $$x = 0$$

The first point is $$(0, 0)$$

$$x = 1$$ then $$y = 2(1)=2$$

The second point is $$(1, 2)$$

1. The points that satisfies the equation $$-x+2y=3$$.

It is obvious for this equation that it does not go through the origin because if $$x$$ and $$y$$ are zero we do not get $$3$$.

if $$y = 0, x=-3$$. $$(-3, 0)$$

if $$x = 1$$ then $$y=(3+1)/2=2$$. $$(1,2)$$

x = seq(-4,2)
df_line <- data.frame(x, y=2*x)
df_line2 <- data.frame(x , y =(3 + x) / 2 )
df1 <- data.frame(x = c(0, 1), y = c(0, 2))

df2 <- data.frame(x = c(1, -3), y = c(2, 0))
ggplot() +
geom_line(data = df_line, aes(x,y), color="blue") +
geom_line(data = df_line2, aes(x,y), color="red") +
geom_point(data = df1, aes(x,y), color = 'blue') +
geom_point(data = df2, aes(x,y), color = 'red') +
geom_hline(yintercept=0) + geom_vline(xintercept=0) +
annotate("text", x=1.2, y=1.8, label= paste("(1, 2)")) The points $$(1,2)$$ satisfies both equations.

$x\begin{bmatrix} 2 \\ -1 \end{bmatrix} + y \begin{bmatrix} -1 \\ 2 \end{bmatrix} = \begin{bmatrix} 0 \\ 3 \end{bmatrix}$

### Linear combination of columns.

The coordinates are two-dimensional, and describes the movements required to get to the end point (of head) of the vector - in this case, we need to move 2 units in the x dimension, and 1 unit in the y dimension.

## data frame for origin and end of vector
v = data.frame(x = c(0,2), y = c(0,-1))
s = data.frame(x = c(0,-1), y = c(0,2))
z = data.frame(x = c(0,0), y = c(0,3))
## Plot the vector
ggplot() +
geom_line(
data = v, aes(x, y), arrow = arrow(
length = unit(0.5, "cm")), color="red", size = 1) +
annotate("text", x=-1, y=2.1, label= "v") +
geom_line(
data = s, aes(x, y), arrow = arrow(
length = unit(0.5, "cm"), ends = "first"), color="blue", size = 1) +
annotate("text", x=2.05, y=-1, label= "s") Since the points $$(1,2)$$ satisfies both equations. We replace $$x$$ and $$y$$.

$1\begin{bmatrix} 2 \\ -1 \end{bmatrix}+ 2 \begin{bmatrix} -1 \\ 2 \end{bmatrix} = \begin{bmatrix} 0 \\ 3 \end{bmatrix}$

z <- v + 2*s
ggplot() +
geom_line(
data = v, aes(x, y), arrow = arrow(
length = unit(0.5, "cm")), color="red", size = 1) +
annotate("text", x=-1, y=2.1, label= "v") +
geom_line(
data = s, aes(x, y), arrow = arrow(
length = unit(0.5, "cm"), ends = "first"), color="blue", size = 1) +
annotate("text", x=2.05, y=-1, label= "s") +
geom_line(
data = z, aes(x, y), arrow = arrow(
length = unit(0.5, "cm")), color="green", size = 1) +
annotate("text", x=0, y=3.1, label= "z") z
##   x y
## 1 0 0
## 2 0 3

### Three equations and three unknowns

$\!2x-y=0 \\ \qquad-x+2y-z\ \ =-1 \\ \:\quad\qquad -3y+4z=4$

#### Matrix form

$A= \begin{bmatrix} 2 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -3 & 4 \end{bmatrix} \qquad b= \begin{bmatrix} 0 \\ -1 \\ 4 \end{bmatrix}$

### Solving system of equations by elimination

sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
##
## Matrix products: default
## BLAS: /home/michael/anaconda3/lib/R/lib/libRblas.so
## LAPACK: /home/michael/anaconda3/lib/R/lib/libRlapack.so
##
## locale:
##  en_CA.UTF-8
##
## attached base packages:
##  stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
##  ggplot2_3.0.0        RevoUtils_11.0.1     RevoUtilsMath_11.0.0
##
## loaded via a namespace (and not attached):
##   Rcpp_0.12.18     compiler_3.5.1   pillar_1.3.0     plyr_1.8.4
##   bindr_0.1.1      tools_3.5.1      digest_0.6.15    evaluate_0.11
##   tibble_1.4.2     gtable_0.2.0     pkgconfig_2.0.1  rlang_0.2.1
##  yaml_2.2.0       blogdown_0.9.8   xfun_0.4.11      bindrcpp_0.2.2
##  withr_2.1.2      stringr_1.3.1    dplyr_0.7.6      knitr_1.20
##  rprojroot_1.3-2  grid_3.5.1       tidyselect_0.2.4 glue_1.3.0
##  R6_2.2.2         rmarkdown_1.10   bookdown_0.7     purrr_0.2.5
##  magrittr_1.5     codetools_0.2-15 backports_1.1.2  scales_0.5.0
##  htmltools_0.3.6  assertthat_0.2.0 colorspace_1.3-2 labeling_0.3
##  stringi_1.2.4    lazyeval_0.2.1   munsell_0.5.0    crayon_1.3.4