R Textbook Companion for Linear Algebra by Jim Hefferon
Total Page:16
File Type:pdf, Size:1020Kb
R Textbook Companion for Linear Algebra by Jim Hefferon1 Created by Ajay Ragh B.Tech. Information Technology APJ Abdul Kalam Technological University Cross-Checked by R TBC Team June 8, 2020 1Funded by a grant from the National Mission on Education through ICT - http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and R codes written in it can be downloaded from the "Textbook Companion Project" section at the website - https://r.fossee.in. Book Description Title: Linear Algebra Author: Jim Hefferon Publisher: Orthogonal Publishing L3C Edition: 3 Year: 2017 ISBN: 978-1944325039 1 R numbering policy used in this document and the relation to the above book. Exa Example (Solved example) Eqn Equation (Particular equation of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means an R code whose theory is explained in Section 2.3 of the book. 2 Contents List of R Codes4 1 Linear Systems5 2 Vector Spaces 16 3 Maps Between Spaces 23 4 Determinants 30 5 Similarity 36 3 List of R Codes Exa 2.1 solution of system of linear equations..........5 Exa 1.3 Solving Linear Systems.................6 Exa 2.3 solution of system of linear equations..........6 Exa 3.3 system of homogeneous equations............7 Exa 1.4 Solving Linear Systems.................8 Exa 2.5 finding the type of solution set of a system of linear equation.........................9 Exa 3.5 gauss method to reduce system of linear equations..9 Exa 1.7 solution of system of linear equations.......... 10 Exa 2.7 solution of system of linear equations.......... 11 Exa 1.8 solution of system of linear equations.......... 12 Exa 1.9 gauss method to reduce system of linear equations.. 12 Exa 1.11 gauss method to reduce system of linear equations.. 13 Exa 1.12 gaussian elimination technique............. 14 Exa 2.12 vector addition...................... 15 Exa 1.13 solution of system of linear equations.......... 15 Exa 3.2 Linear Independence................... 16 Exa 3.5 Basis for the column space of the given matrix.... 17 Exa 1.6 Linear Independence................... 18 Exa 3.7 Basis for the column space of the given matrix.... 18 Exa 1.9 Linear Independence................... 19 Exa 3.9 Finding a basis for the given span........... 20 Exa 3.10 finding basis from reduced echelon form........ 20 Exa 1.16 finding the coordinates of that vector with respect to the basis......................... 21 Exa 1.1 correspondence between vectors............. 23 Exa 4.1.1 Matrix Operations.................... 23 Exa 4.1.2 Matrix Operations.................... 24 4 Exa 3.1.11 Computing linear maps................. 24 Exa 4.2.4 Matrix Operations.................... 25 Exa 4.2.5 Matrix Operations.................... 25 Exa 4.2.6 Matrix Operations.................... 25 Exa 4.3.15 Mechanics of Matrix Multiplication........... 26 Exa 4.4.9 Inverses.......................... 26 Exa 2.5 Computing linear maps................. 27 Exa 1.9 automorphism...................... 27 Exa 2.10 Computing linear maps................. 28 Exa 1.11 value of h on the basis vectors............. 28 Exa 2.11 Computing linear maps................. 29 Exa 3.1 Determinants...................... 30 Exa 1.4 Laplace Formula to find determinant......... 30 Exa 3.4 Determinants...................... 31 Exa 2.5 Determinants....................... 31 Exa 1.6 Laplace Formula to find determinant......... 32 Exa 2.6 Determinants...................... 33 Exa 1.7 Laplace Formula to find determinant......... 34 Exa 1.10 Laplace Formula to find inverse............ 34 Exa 2.2 Complex Vector Spaces................. 36 Exa 2.2.2 Checking if matrix is diagonalizable.......... 36 Exa 1.3 Similar Matrix...................... 37 Exa 2.4 finding null spaces.................... 37 Exa 3.6 Eigenvalues and Eigenvectors.............. 38 Exa 2.10 Nilpotent matrix..................... 38 Exa 2.17 Nilpotence index..................... 39 Exa 2.18 nilpotence index..................... 40 Exa 3.19 Eigenvalues and Eigenvectors.............. 40 5 Chapter 1 Linear Systems R code Exa 2.1 solution of system of linear equations 1 #Example 2.1,chapter1,section 1.2,page 13 2 #package used matlib v0.9.1 3 #install package using command: install.packages(" m a t l i b") 4 #Github reposiory of matlib:https: // g i t h u b.com / f r i e n d l y / m a t l i b 5 6 #installation and loading library 7 #install.packages("matlib") 8 library( " m a t l i b ") 9 10 #program 11 A <- matrix(c(2,1,3,0,-1,-1,1,-1,0),ncol=3) 12 b <-c(3,1,4) 13 Ab <- cbind(A,b) 14 Ab <- rowadd(Ab,1,2,-0.5) 15 Ab <- rowadd(Ab,1,3,-1.5) 16 Ab <- rowadd(Ab,2,3,-1) 17 Ab 18 #from the result we can see that the system doesnot havea unique solution. 6 19 #We can represent the solution set by representing the variables that lead(x,y)by the variable that does not lead(z). 20 #so solution set f ( ( 3 / 2) −(1/ 2)z,(1 / 2) −(3/ 2)z,z) j z b e l o n g s toR g R code Exa 1.3 Solving Linear Systems 1 #Example 1.3 2 #SectionI. Solving Linear Systems,page3 3 A <- matrix(c(3,-1,2,1),ncol = 2) 4 b <-c(7,6) 5 x <- solve(A)%*%b 6 x R code Exa 2.3 solution of system of linear equations 1 #Example 2.3,chapter1,section 1.2,page 13 2 #package used matlib v0.9.1 3 #install package using command: install.packages(" m a t l i b") 4 #Github reposiory of matlib:https: // g i t h u b.com / f r i e n d l y / m a t l i b 5 6 #installation and loading library 7 #install.packages("matlib") 8 library( " m a t l i b ") 9 10 #program 11 A <- matrix(c(1,0,3,0,1,1,0,-1,1,-1,6,1,-1,1,-6,-1), ncol = 4) 12 b <-c(1,-1,6,1) 13 Ab <- cbind(A,b) 7 14 Ab <- rowadd(Ab,1,3,-3) 15 Ab <- rowadd(Ab,2,3,3) 16 Ab <- rowadd(Ab,2,4,1) 17 Ab 18 #from the reduced augmented matrix Ab,we can see t h a t onlyx andy are leading,whilez andw are f r e e. 19 #We represent the solution set by expressing the l e a d i n g variables in terms of the free variables 20 #The solution set: f(2 − 2z+2w, −1+z − w,z,w) j z,w b e l o n g s toR g R code Exa 3.3 system of homogeneous equations 1 #Example 3.3,section 1.3,chapter 1,page 24 2 #package used matlib v0.9.1 3 #install package using command: install.packages(" m a t l i b") 4 #Github reposiory of matlib:https: // g i t h u b.com / f r i e n d l y / m a t l i b 5 6 #installation and loading library 7 #install.packages("matlib") 8 library( " m a t l i b ") 9 10 #program 11 A <- matrix(c(3,2,4,-1),ncol = 2) 12 b <-c(3,1) 13 c <-c(0,0) 14 Ab <- cbind(A,b) # linear system 15 Ac <- cbind(A,c) #system of homogeneous equations 16 #reduction of original system 17 Ab <- rowadd(Ab,1,2,-2/3) 18 #reduction of homogeneous system 19 Ac <- rowadd(Ac,1,2,-2/3) 8 20 #comparing both 21 Ab 22 Ac 23 #Obviously the two reductions go in the same way. 24 #We can study how to reducea linear systems by i n s t e a d studying how to reduce the associated homogeneous system. R code Exa 1.4 Solving Linear Systems 1 #Example 1.4,SectionI. Solving Linear Systems 2 #page3 3 #package used: matlib 4 #installation run command: install.packages("matlib ") 5 #package repo: https: // g i t h u b.com / f r i e n d l y / m a t l i b 6 7 #installing and loading library 8 #install.packages("matlib") 9 library( " m a t l i b ") 10 11 #program 12 A <- matrix(c(0,1,1/3,0,5,2,3,-2,0),ncol =3,nrow= 3) 13 b <-c(9,2,3) 14 Ab <- cbind(A,b) 15 Ab <- rowswap(Ab,1,3) 16 Ab <- rowmult(Ab,1,3) 17 Ab <- rowmult(Ab,1,-1) 18 Ab <- rowadd(Ab,1,2,1) 19 Ab 20 #from Ab(X3=3)(since drom row3,3 *X3=9) 21 #from Ab row2 −1*X2−2*x3= −7,so X2=1 22 #from Ab row1 X1+6*x2=9,so X1=3 9 R code Exa 2.5 finding the type of solution set of a system of linear equa- tion 1 #Example 2.5,section 1.2,page 15 2 #package used matlib v0.9.1 3 #install package using command: install.packages(" m a t l i b") 4 #Github reposiory of matlib:https: // g i t h u b.com / f r i e n d l y / m a t l i b 5 6 #installation and loading library 7 #install.packages("matlib") 8 library( " m a t l i b ") 9 10 #program 11 A <- matrix(c(1,2,3,2,0,2,0,1,1,0,0,-1),ncol = 4) 12 b <-c(1,2,4) 13 Ab <- cbind(A,b) #augmented matrix 14 Ab <- rowadd(Ab,1,2,-2) 15 Ab <- rowadd(Ab,1,3,-3) 16 Ab <- rowadd(Ab,2,3,-1) 17 Ab 18 #The leading variables arex,y, andw.