diff options
author | Prashant S | 2019-10-04 12:27:32 +0530 |
---|---|---|
committer | GitHub | 2019-10-04 12:27:32 +0530 |
commit | ac2986488a9731cff5cbb517d8f0ef98e2561d64 (patch) | |
tree | 7bb3f64824627ef179d5f341266a664fd0b69011 /Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3 | |
parent | cbb2770fb2f88246175add29623103a56ba338b8 (diff) | |
parent | b3f3a8ecd454359a2e992161844f2fb599f8238a (diff) | |
download | R_TBC_Uploads-master.tar.gz R_TBC_Uploads-master.tar.bz2 R_TBC_Uploads-master.zip |
Added R TBC
Diffstat (limited to 'Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3')
14 files changed, 738 insertions, 0 deletions
diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.2/Ex3.2_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.2/Ex3.2_2.r new file mode 100644 index 00000000..fda61595 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.2/Ex3.2_2.r @@ -0,0 +1,59 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 2 Chapter : 3.2 Page No: 132 +# Find the Nullspace of given singular matrix + +library(pracma) +nullspacebasis <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + return(N) + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + #Basis for the nullspace of given matrix + return(N) +} +A<-matrix(c(1,3,2,6),nrow=2,ncol=2) +N<-nullspacebasis(A) +print("Basis vectors for nullspace of given matrix is") +N
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.3/Ex3.2_3.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.3/Ex3.2_3.r new file mode 100644 index 00000000..49568620 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.3/Ex3.2_3.r @@ -0,0 +1,69 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3 Chapter : 3.2 Page No: 133 +# Find the Nullspace of given matrices A,B,C + +library(pracma) +nullspacebasis <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + return(N) + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + #Basis for the nullspace of given matrix + return(N) +} + +A<-matrix(c(1,3,2,8),ncol=2,nrow=2) +N<-nullspacebasis(A) +print("Basis vectors of the nullspace of matrix A is") +N +B<-rbind(A,2*A) +N1<-nullspacebasis(B) +print("Basis vectors of the nullspace of matrix B is") +N1 +C<-cbind(A,2*A) +C +N2<-nullspacebasis(C) +print("Basis vectors of the nullspace of matrix C is") +N2
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.4/Ex3.2_4.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.4/Ex3.2_4.r new file mode 100644 index 00000000..ccf644a4 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.4/Ex3.2_4.r @@ -0,0 +1,60 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 4 Chapter : 3.2 Page No: 136 +# Find the Nullspace of given Upper traingular matrix + +library(pracma) +nullspacebasis <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + return(N) + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + #Basis for the nullspace of given matrix + return(N) +} + +U<-matrix(c(1,0,5,0,7,9),nrow=2) +N<-nullspacebasis(U) +print("Basis vectors for the Nullspace of given upper traingular matrix is ") +N
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.a/Ex3_3.2A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.a/Ex3_3.2A.r new file mode 100644 index 00000000..a181d2de --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.a/Ex3_3.2A.r @@ -0,0 +1,14 @@ +# Example : 3.2A Chapter : 3.2 Page No: 139
+# Find the Matrix having the given special solutions
+s1<-c(-3,1,0,0)
+s2<-c(-2,0,-6,1)
+R<-diag(4)
+R<-R[-4,]
+#As 1st column is pivot column it is not needed to change
+#As 3rd column is next pivot column , row reduced echolon form has 1 in second row of third column
+R[,3]<-R[,2]
+#Two free columns are modified acording to special solutions
+R[,2]<-c(-1*s1[1],0,0)
+R[,4]<-c(-1*s2[1],-1*s2[3],0)
+print("matrix having given special solutions")
+print(R)
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.b/Ex3_3.2b.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.b/Ex3_3.2b.r new file mode 100644 index 00000000..f6b24099 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.2.b/Ex3_3.2b.r @@ -0,0 +1,73 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3.2B Chapter : 3.2 Page No: 140 +# Find the Specialsolution, Pivotcolumns, freecolumns and Reduced row echelon form for each given matrix + +library(pracma) +solution <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + N<-c() + } + else{ + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + } + #Basis for the nullspace of given matrix + print("Special solutions are given by the basis vectors of null space") + print(N) + print("Pivot columns are") + print(pivotcol) + print("Free columns or free variables are") + print(freecol) + print("Row reduced echelon form is") + print(R) +} +A1<-matrix(c(0,0,0,0,0,0,0,0),nrow=2) +print("For the matrix A1") +solution(A1) +A2<-matrix(c(3,1,6,2),nrow=2) +print("For the matrix A2") +solution(A2) +A3<-cbind(A2,A2) +print("For the matrix A3") +solution(A3)
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.a/Ex3_3.3A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.a/Ex3_3.3A.r new file mode 100644 index 00000000..5dbfb740 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.a/Ex3_3.3A.r @@ -0,0 +1,64 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3.3A Chapter : 3.3 Page No: 149 +# Find the row reduced echelon form, rank and special solution for given matrix + +library(pracma) +solution <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + N<-c() + } + else{ + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + } + print("Row reduced echelon form is") + print(R) + print("Rank of the Matrix is") + print(y) + print("Special solutions for given matrix are given by") + print(N) +} + +A<-matrix(c(1,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,-1,1),nrow=4) +solution(A) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.c/Ex3_3.3C.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.c/Ex3_3.3C.r new file mode 100644 index 00000000..8b9d419d --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.3.c/Ex3_3.3C.r @@ -0,0 +1,69 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3.3C Chapter : 3.3 Page No: 151 +# Find the special solutions and row reduced echelon forms + +library(pracma) +solution <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + N<-c() + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + print("row reduced echelon form is") + print(R) + print("Special solution is ") + print(N) +} +#c is not equal to 4 +A<-matrix(c(1,3,4,2,6,8,1,3,2),ncol=3) +solution(A) +#c=4 +A4<-matrix(c(1,3,4,2,6,8,1,3,4),ncol=3,nrow=3) +solution(A4) +#c is not equal to 0 +B<-matrix(c(1,1,1,1),nrow=2) +solution(B) +#c = 0 +B0<-matrix(c(0,0,0,0),nrow=2) +solution(B0) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r new file mode 100644 index 00000000..e14008d0 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.2/Ex3.4_2.r @@ -0,0 +1,67 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 2 Chapter : 3.4 Page No: 158 +# Find the particular solution and special solution of the given system + +library(pracma) +nullspacebasis <- function(A){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + return(N) + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + #Basis for the nullspace of given matrix + return(N) +} +A<-matrix(c(1,1,1,2,1,-1),nrow=2) +b<-c(3,4) +s<-nullspacebasis(A) +Ab<-cbind(A,b) +Rd<-rref(Ab) +p<-Rd[,4] +p<-c(p,0) +print("Particualar solution is") +print(p) +print("special solution is ") +print(s) +print("The complete solution = particular solution + special solution") diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.a/Ex3_3.4A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.a/Ex3_3.4A.r new file mode 100644 index 00000000..f38ad725 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.a/Ex3_3.4A.r @@ -0,0 +1,72 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3.4A Chapter : 3.4 Page No: 160 +# Find the Complete solution of the given system + +library(pracma) +completesolution <- function(A,b){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + N<-c() + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + s<-N + Ab<-cbind(A,b) + Rd<-rref(Ab) + temp<-Rd[,n+1] + p<-c(1:n) + for(i in 1:length(freecol)){ + p[freecol[i]]<-0 + } + for(i in 1:length(pivotcol)){ + p[pivotcol[i]]<-temp[i] + } + print("The special solution is") + print(s) + print("The particular solution is") + print(p) + print("Complete solution = Particular solution + Special solution") +} +A<-matrix(c(1,2,3,2,4,6,3,8,7,5,12,13),nrow=3) +b<-c(0,6,-6) +completesolution(A,b) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.c/Ex3_3.4C.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.c/Ex3_3.4C.r new file mode 100644 index 00000000..1d77f3aa --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.4.c/Ex3_3.4C.r @@ -0,0 +1,72 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 3.4C Chapter : 3.4 Page No: 162 +# Find the Complete solution of the given system + +library(pracma) +completesolution <- function(A,b){ + R<-rref(A) + m<-nrow(A) + n<-ncol(A) + pivotcol<-c() #vector to store the column numbers of pivot columns + freecol<-c() #vector to store the column numbers of free columns + i<-1 + j<-1 + + # to find which columns are pivot and which are free + while(i<=m & j<=n){ + if(R[i,j]==1){ + pivotcol<-c(pivotcol,j) + i<-i+1 + j<-j+1 + } + else{ + j<-j+1 + } + } + y<-length(pivotcol) + freecol<-c(1:n) + freecol<-freecol[!freecol%in%pivotcol] + x<-length(freecol) + N<-c() + #find the basis for null space based on Row reduced echelon form of given matrix + if(y==n){ + N<-c() + } + for(i in 1:x){ + temp<-c(1:n) + for(j in 1:x){ + temp[freecol[j]]<-0 + } + temp[freecol[i]]<-1 + temp[freecol[i]] + for(j in 1:y){ + temp[pivotcol[j]]<-R[j,freecol[i]]*-1 + } + N<-c(N,temp) + } + N<-matrix(N,nrow=n,ncol=x) + s<-N + Ab<-cbind(A,b) + Rd<-rref(Ab) + temp<-Rd[,n+1] + p<-c(1:n) + for(i in 1:length(freecol)){ + p[freecol[i]]<-0 + } + for(i in 1:length(pivotcol)){ + p[pivotcol[i]]<-temp[i] + } + print("The special solution is") + print(s) + print("The particular solution is") + print(p) + print("Complete solution = particular solution + Special solution") +} +A<-matrix(c(1,2,4,2,4,8,1,4,6,0,8,8),nrow=3) +b<-c(4,2,10) +completesolution(A,b)
\ No newline at end of file diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.5.1/Ex3.5_1.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.5.1/Ex3.5_1.r new file mode 100644 index 00000000..a3f3cf30 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.5.1/Ex3.5_1.r @@ -0,0 +1,16 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 1 Chapter : 3.5 Page No: 170 +# Columns of given matrix are dependent +library(pracma) +A<-matrix(c(1,2,1,0,1,0,3,5,3),nrow=3) +if(Rank(A)!=ncol(A)){ + print("The columns of A are dependent") +} +print("As the columns of A are dependent ,there is a nonzero solution to Ax=0") +x<-matrix(c(-3,1,1),ncol=1) +print(A%*%x) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.1/Ex3.6_1.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.1/Ex3.6_1.r new file mode 100644 index 00000000..6e7450fc --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.1/Ex3.6_1.r @@ -0,0 +1,17 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 1 Chapter : 3.6 Page No: 188 +# Dimensions and rank of matrix +library(pracma) +solution<-function(A){ + print(paste("Number of rows , m=",nrow(A))) + print(paste("Number of columns,n=",ncol(A))) + print(paste("Rank of the given matrix",Rank(A))) +} +A<-matrix(c(1,2,3),nrow=1) +solution(A) + diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.2/Ex3.6_2.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.2/Ex3.6_2.r new file mode 100644 index 00000000..71b3fefd --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.2/Ex3.6_2.r @@ -0,0 +1,16 @@ +# Packages used : pracma +# To install pracma,type following in command line while connected to internet +# install.packages("pracma") +# package can be included by command " library(pracma) " +# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html + +# Example : 2 Chapter : 3.6 Page No: 188 +# Dimensions and rank of matrix +library(pracma) +solution<-function(A){ + print(paste("Number of rows , m=",nrow(A))) + print(paste("Number of columns,n=",ncol(A))) + print(paste("Rank of the given matrix",Rank(A))) +} +A<-matrix(c(1,2,2,4,3,6),nrow=2) +solution(A) diff --git a/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.a/Ex3_3.6A.r b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.a/Ex3_3.6A.r new file mode 100644 index 00000000..9bf248a9 --- /dev/null +++ b/Introduction_To_Linear_Algebra_by_Gilbert_Strang/CH3/EX3.6.a/Ex3_3.6A.r @@ -0,0 +1,70 @@ +# Packages used : pracma
+# To install pracma,type following in command line while connected to internet
+# install.packages("pracma")
+# package can be included by command " library(pracma) "
+# for more information about pracma visit https://cran.r-project.org/web/packages/pracma/index.html
+
+# Example : 3.6A Chapter : 3.6 Page No: 190
+# Four Fundamental Spaces of given matrix
+
+library(pracma)
+nullspacebasis <- function(A){
+ R<-rref(A)
+ m<-nrow(A)
+ n<-ncol(A)
+ pivotcol<-c() #vector to store the column numbers of pivot columns
+ freecol<-c() #vector to store the column numbers of free columns
+ i<-1
+ j<-1
+
+ # to find which columns are pivot and which are free
+ while(i<=m & j<=n){
+ if(R[i,j]==1){
+ pivotcol<-c(pivotcol,j)
+ i<-i+1
+ j<-j+1
+ }
+ else{
+ j<-j+1
+ }
+ }
+ y<-length(pivotcol)
+ freecol<-c(1:n)
+ freecol<-freecol[!freecol%in%pivotcol]
+ x<-length(freecol)
+ N<-c()
+ #find the basis for null space based on Row reduced echelon form of given matrix
+ if(y==n){
+ return(N)
+ }
+ for(i in 1:x){
+ temp<-c(1:n)
+ for(j in 1:x){
+ temp[freecol[j]]<-0
+ }
+ temp[freecol[i]]<-1
+ temp[freecol[i]]
+ for(j in 1:y){
+ temp[pivotcol[j]]<-R[j,freecol[i]]*-1
+ }
+ N<-c(N,temp)
+ }
+ N<-matrix(N,nrow=n,ncol=x)
+ #Basis for the nullspace of given matrix
+ return(N)
+}
+l<-matrix(c(1,2,5,0,1,0,0,0,1),ncol=3)
+u<-matrix(c(1,0,0,3,0,0,0,1,0,5,6,0),ncol=4)
+A<-l%*%u
+print("Row space Basis of A")
+print(u[1,])
+print(u[2,])
+print("COlumn space Basis of A ")
+print(l[,1])
+print(l[,2])
+print("Null space Basis of A")
+N<-nullspacebasis(A)
+print(N)
+print("Null space Basis of A transpose")
+NT<-nullspacebasis(t(A))
+print(NT)
|