From bd23cb16e08edff58f4471f3b7ff45238fb60c21 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Wed, 13 Sep 2023 05:45:16 +0300 Subject: [PATCH] Add content for vectors-matrix-ops/vectors-matrix-operations Add actual content to the skeleton of the `vectors-matrix-ops/vectors-matrix-operations` section. Signed-off-by: Eggert Karl Hafsteinsson Signed-off-by: Teodor Dutu Signed-off-by: Razvan Deaconescu --- .../media/25_4_Matrix_multiplication.png | Bin 0 -> 7265 bytes .../reading/README.md | 1 - .../vectors-matrix-operations/reading/read.md | 285 ++++++++++++++++++ 3 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 chapters/vectors-matrix-ops/vectors-matrix-operations/media/25_4_Matrix_multiplication.png delete mode 100644 chapters/vectors-matrix-ops/vectors-matrix-operations/reading/README.md create mode 100644 chapters/vectors-matrix-ops/vectors-matrix-operations/reading/read.md diff --git a/chapters/vectors-matrix-ops/vectors-matrix-operations/media/25_4_Matrix_multiplication.png b/chapters/vectors-matrix-ops/vectors-matrix-operations/media/25_4_Matrix_multiplication.png new file mode 100644 index 0000000000000000000000000000000000000000..45fbbbbb4b72c026efac7e94eb466b8d238daa3c GIT binary patch literal 7265 zcmbVxc{G$^*!ReqT}hHH6_ZBRknB;BZDe0lk(~);&=A?vV&6gvG4^F_*|L?1!NjPs zPl&OO?1uN&d*1iF-}(Od-t#@@Idh(KKl9w%bzj%-_q(oWBZFIP%zVrc2!u`Nww5sj za!egOzhq(nzloAO{SXL0R7Xq0#Q)dQn2HzaPm}zrTPQ@P{Mz|XZ!<>d8JB;@=sKET zHz9d4ya~~nxum%xEEJ-=N3+R=mZK4j(#Ke<(=D2~cKj4>{dE2^`ecVD{-H%`ju2gW zIn!TmL5cIxoZRxAXI+z-sxRmwbl* z`Yf9s-eyk`XhFsr+%_jJLD(ZniJ;e$NqO)+7u%PW# znw9aU_MM$;28IUS3TdsCuw;{-hglPU{l`3?qG!bBl_uYtp=;Kp<@CcGE≺@W$PA zuCZEG_L?a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?L6_`^{59`ue0SB+GHZNvRPWY@0h9QTg~CkMYCK#67-B^=BWvuh zMr{vUz81qD9wJX9@JD|OY#KL#CrS-CTB1%IXH>9Bo^fd$b6V-}Ra){@!e?Y;q%lKU zt>FHN-5_BxL~OM6BA1iTd>nGszmsR2dMT6@UPed>(8iD<-$%u?;aS`{kxeRS{@AFf zX_u9eVQEOZLBZAoxNhFy-?CsTSx-s}N8Sjys}r9UbRflKCBJRy3j-V#({!097C%8< zetLak7;*Wf7XZcVgTG??QyjtS1^6j8OU(}h7IkffMMZBe90$%nUUOMu)2cW1bpBppj6$lU_u9H;L6 z4Yu7B9yTfFzcl1pbC0)cRQOLG*m_VC$t?l7b`S-?U1rzB94z>1Wx$X6Ej3%1DWLA{ zqzo8YB!;E?sDd)k_sx&v%^VVVGAM&g9Y~OwH$#vc3#VLbepQjIz#K-#r6IrgYzaW^ z10e+|ZmP(bL-zX#fRqJ@q^;Cy56~OtDM%=HA~Mqi?rXzekQChnuyVcLHnGA?&b35u ze!vPUe}K`5*BR93&oO;Tps6V_BY#^dEZY4TDEUX1wSkkovHQIuv~#!4S%{0v@qLqZ zehIN=^U79LD}VR%q9T?Lgl>GbkRi#A``n%Qf3+c*f$Z}Pkz{gU>V`Prbn;H6QQ++*>5*Vv$&V6zJGk#u=MtKKRxb6ZwSJytpR{J#$kIw|K z{%2ul3N86>-x{L>pZ~MNl;`UIf7R=MRVMr2l+XSj9hEE{(zg5DDSlt3S_?{t5FKp; Kty0Z X <- 3:6 +> X +[1] 3 4 5 6 +``` + +A matrix can be generated in R as follows, + +```text +> matrix(X) + [,1] +[1,] 3 +[2,] 4 +[3,] 5 +[4,] 6 +``` + +::: + +:::note Note + +We note that R distinguishes between vectors and matrices. + +::: + +## Elementary Operations + +We can define multiplication of a real number $k$ and a vector $v=(v_1,\ldots,v_n)$ by $k\cdot v=(kv_1,\ldots,kv_n)$. +The sum of two vectors in $\mathbb{R}^n$, $v=(v_1,\ldots,v_n)$ and $u=(u_1,\ldots,u_n)$ is defined as the vector $v+u=(v_1+u_1,\ldots,v_n+u_n)$. +We can define multiplication of a number and a matrix and the sum of two matrices (of the same sizes) similarly. + +### Examples + +:::info Example + +```text +> A <- matrix(c(1,2,3,4), nr=2, nc=2) + +> A + [,1] [,2] +[1,] 1 3 +[2,] 2 4 + +> B <- matrix(c(1,0,2,1), nr=2, nc=2) + +> B + [,1] [,2] +[1,] 1 2 +[2,] 0 1 + +> A+B + [,1] [,2] +[1,] 2 5 +[2,] 2 5 +``` + +::: + +## The Tranpose of a Matrix + +In R, matrices may be constructed using the `matrix` function and the transpose of $A$, $A^\prime$, may be obtained in R by using the `t` function: + +```text +> A <- matrix(1:6, nrow=3) + +> t(A) + [,1] [,2] [,3] +[1,] 1 2 3 +[2,] 4 5 6 +``` + +### Details + +If $A$ is an $n \times m$ matrix with element $a_{ij}$ in row $i$ and column $j$, then $A^\prime$ or $A^T$ is the $m\times n$ matrix with element $a_{ij}$ in row $j$ and column $i$. + +### Examples + +:::info Example + +Consider a vector in R + +```text +> x <- 1:4 + +> x +[1] 1 2 3 4 + +> t(x) + [,1] [,2] [,3] [,4] +[1,] 1 2 3 4 + +> matrix(x) + [,1] +[1,] 1 +[2,] 2 +[3,] 3 +[4,] 4 +> t(matrix(x)) + [,1] [,2] [,3] [,4] +``` + +::: + +:::note Note + +The first solution gives a $1 \times n$ matrix and the second solution gives a $n \times 1$ matrix. + +::: + +## Matrix Multiplication + +Matrices $A$ and $B$ can be multiplied together if $A$ is an $n \times p$ matrix and $B$ is an $p\times m$ matrix. +The general element $c_{ij}$ of $n\times m$, $C=AB$, is found by pairing the $i^{th}$ row of $C$ with the $j^{th}$ column of $B$, and computing the sum of products of the paired terms. + +![Fig. 39](../media/25_4_Matrix_multiplication.png) + +### Details + +Matrices $A$ and $B$ can be multiplied together if $A$ is a $n\times p$ matrix and $B$ is a $p\times m$ matrix. +Given the general element $c_{ij}$ of $n \times m$ matrix, $C=AB$ is found by pairing the $i^{th}$ row of $C$ with the $j^{th}$ column of $B$, and computing the sum of products of the paired terms. + +### Examples + +:::info Example: Matrices in R + +```text +> A <- matrix(c(1,3,5,2,4,6),3,2) + +> A + [,1] [,2] +[1,] 1 2 +[2,] 3 4 +[3,] 5 6 + +> B <- matrix(c(1,1,2,3),2,2) + +> B + [,1] [,2] +[1,] 1 2 +[2,] 1 3 + +> A%*%B + [,1] [,2] +[1,] 3 8 +[2,] 7 18 +[3,] 11 28 +``` + +::: + +## More on Matrix Multiplication + +Let $A$, $B$, and $C$ be $m\times n$, $n\times l$, and $l\times p$ matrices, respectively. +Then we have + +$$(AB)C=A(BC)$$ + +In general, matrix multiplication is not commutative, that is $AB\neq BA$. + +We also have + +$$(AB)'=B'A'$$ + +In particular, $(Av)'(Av)=v'A'Av$, when $v$ is a $n\times1$ column vector + +More obvious are the rules + +1. $A+(B+C)=(A+B)+C$ + +1. $k(A+B)=kA+kB$ + +1. $A(B+C)=AB+AC$ + +where $k\in\mathbb{R}$ and when the dimensions of the matrices fit. + +## Linear Equations + +### Details + +General linear equations can be written in the form $Ax=b$. + +### Examples + +:::info Example + +The set of equations + +$$2x+3y=4$$ + +$$3x+y=2$$ + +can be written in matrix formulation as + +$$ +\begin{bmatrix} + 2 & 3 \\ + 3 & 1 +\end{bmatrix} +\begin{bmatrix} + x \\ + y +\end{bmatrix} = +\begin{bmatrix} + 4 \\ + 2 +\end{bmatrix} +$$ + +i.e. $A\underline{x} = \underline{b}$ for an appropriate choice of $A, \underline{x}$ and $\underline{b}$. + +::: + +## The Unit Matrix + +The $n\times n$ matrix + +$$ +I = + \left[ + \begin{array}{cccc} + 1 & 0 & \ldots & 0 \\ + 0 & 1 & 0 & \vdots \\ + \vdots & 0 & \dots & 0 \\ + 0 & \ldots & 0 & 1 + \end{array} + \right] +$$ + +is the identity matrix. +This is because if a matrix $A$ is $n\times n$ + +then $A I = A$ and $I A = A$ + +## The Inverse of a Matrix + +If $A$ is an $n \times n$ matrix and $B$ is a matrix such that + +$$BA = AB = I$$ + +then $B$ is said to be the inverse of $A$, written + +$$B = A ^{-1}$$ + +Note that if $A$ is an $n \times n$ matrix for which an inverse exists, then the equation $Ax = b$ can be solved and the solution is $x = A^{-1} b$. + +### Examples + +:::info Example + +If matrix $A$ is: + +$$ +\begin{bmatrix} + 2 & 3 \\ + 3 & 1 +\end{bmatrix} +$$ + +then $A ^{-1}$ is: + +$$ +\begin{bmatrix} + \displaystyle\frac{-1}{7} & \displaystyle\frac{3}{7} \\ + \displaystyle\frac{3}{7} & \displaystyle\frac{-2}{7} +\end{bmatrix} +$$ + +:::