Matrices avec R et applications


library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()


Saisir une matrice

data<-
  c(0.617,0.545,0.496,0.493,0.437,0.408,
731,680,621,591,617,615,140,139,143,128,
186,184,3.24,4.13,3.68,4.00,4.80,4.80)
data %>% length()
## [1] 24

Imprimer avec kable

data%>% matrix(nrow = 6) -> X
X%>%as.data.frame()->DF
rownames(DF)<-c("Tigers","Dragons","BayStars","Swallows","Giants","Carp")
colnames(DF)<-c("Win","Runs","HR","ERA")
DF%>%rownames_to_column(var="Team")->DF
DF%>%knitr::kable("simple")
Team Win Runs HR ERA
Tigers 0.617 731 140 3.24
Dragons 0.545 680 139 4.13
BayStars 0.496 621 143 3.68
Swallows 0.493 591 128 4.00
Giants 0.437 617 186 4.80
Carp 0.408 615 184 4.80

Transposer des matrices

X%>%t()
##         [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
## [1,]   0.617   0.545   0.496   0.493   0.437   0.408
## [2,] 731.000 680.000 621.000 591.000 617.000 615.000
## [3,] 140.000 139.000 143.000 128.000 186.000 184.000
## [4,]   3.240   4.130   3.680   4.000   4.800   4.800
print(X)
##       [,1] [,2] [,3] [,4]
## [1,] 0.617  731  140 3.24
## [2,] 0.545  680  139 4.13
## [3,] 0.496  621  143 3.68
## [4,] 0.493  591  128 4.00
## [5,] 0.437  617  186 4.80
## [6,] 0.408  615  184 4.80
knitr::kable(t(X),caption = "**X'**")
X’
0.617 0.545 0.496 0.493 0.437 0.408
731.000 680.000 621.000 591.000 617.000 615.000
140.000 139.000 143.000 128.000 186.000 184.000
3.240 4.130 3.680 4.000 4.800 4.800

Somme de matrices et multiplication par un scalaire

X<-matrix(c(3,-2,6,8,0,-2),nrow=2,byrow = T)
X%>%knitr::kable(caption =   "**X**")
X
3 -2 6
8 0 -2
Y<-matrix(c(2,1,-9,-7,2,-3),nrow=2,byrow = T)
Y%>%knitr::kable(caption =   "**Y**")
Y
2 1 -9
-7 2 -3
(X+Y)%>%knitr::kable(caption =   "**X+Y**")
X+Y
5 -1 -3
1 2 -5
Z<-matrix(c(8,-2,6,-5,0,-3),nrow=2,byrow = T)
Z%>%knitr::kable(caption =   "**Z**")
Z
8 -2 6
-5 0 -3
(-0.1*Z)%>%knitr::kable(caption = "-0.1**Z**")
-0.1Z
-0.8 0.2 -0.6
0.5 0.0 0.3
X<-matrix(c(4,-2,6,8,0,-2),nrow=2,byrow = T)
X%>%knitr::kable(caption =   "**X**")
X
4 -2 6
8 0 -2
Y<-matrix(c(2,1,-9,-7,2,-3),nrow=2,byrow = T)
Y%>%knitr::kable(caption =   "**Y**")
Y
2 1 -9
-7 2 -3
(0.5*X+(-2)*Y)%>%knitr::kable(caption =   "0.5**X**+(-2)**Y**")
0.5X+(-2)Y
-2 -3 21
18 -4 5

Produit de matrices

A<-matrix(c(2,-4,1,7),nrow=2,byrow = T)
A%>%knitr::kable(caption =   "**A**")
A
2 -4
1 7
B<-matrix(c(-3,1,2,-5),nrow=2,byrow = T)
B%>%knitr::kable(caption =   "**B**")
B
-3 1
2 -5
(A%*%B)%>%knitr::kable(caption =   "**AB**")
AB
-14 22
11 -34
X<-matrix(c(2,3,-1,-2,0,4),nrow=2,byrow = T)
X%>%knitr::kable(caption =   "**X**")
X
2 3 -1
-2 0 4
Y<-matrix(c(3,5,4,-1,0,-2,0,6,0),nrow=3,byrow = T)
Y%>%knitr::kable(caption =   "**B**")
B
3 5 4
-1 0 -2
0 6 0
(X%*%Y)%>%knitr::kable(caption =   "**XY**")
XY
3 4 2
-6 14 -8
F<-matrix(c(2,-1,-3,0,1,3,-2,-3),nrow=4,byrow = T)
F%>%knitr::kable(caption =   "**F**")
F
2 -1
-3 0
1 3
-2 -3
A<-matrix(c(-4,1,6,-3,2,5),nrow=3,byrow = T)
A%>%knitr::kable(caption =   "**A**")
A
-4 1
6 -3
2 5
(F%*%t(A)) %>%knitr::kable(caption =   "**FA'**")
FA’
-9 15 -1
12 -18 -6
-1 -3 17
5 -3 -19
A<-matrix(c(-4,1,6,-3,2,5),nrow=3,byrow = T)

A%>%knitr::kable(caption =   "**A**")
A
-4 1
6 -3
2 5
(A%*%t(A)) %>%knitr::kable(caption =   "**S=AA'**")
S=AA’
17 -27 -3
-27 45 -3
-3 -3 29
(t(A)%*%A) %>%knitr::kable(caption =   "**T=A'A**")
T=A’A
56 -12
-12 35
u<-matrix(c(2,-1,3),nrow=3,byrow = T)

u%>%knitr::kable(caption =   "**u**")
u
2
-1
3
v<-matrix(c(-2,3,-4),nrow=3,byrow = T)

v%>%knitr::kable(caption =   "**v**")
v
-2
3
-4
(t(u)%*%v) %>%knitr::kable(caption =   "**u'v**")
u’v
-19
(u%*%t(v)) %>%knitr::kable(caption =   "**uv'**")
uv’
-4 6 -8
2 -3 4
-6 9 -12
data.frame(X)%>%ggplot(aes(x =X1 ,y=X2,z=X3))+geom_point()

X<-matrix(c(3,-2,6,8,0,-2),nrow=2,byrow = T)
X%>%knitr::kable(caption =   "**X**")
X
3 -2 6
8 0 -2
install.packages("scatterplot3d") # Install
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library("scatterplot3d") # load
scatterplot3d(data.frame(X),xlim = c(-10,10),ylim = c(-10,10),zlim = c(-10,10),type="h")

install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')))

fig
n=10
(J<-diag(n)-(1/n)*matrix(1, nrow = n, ncol = n))
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  -0.1
##  [2,] -0.1  0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  -0.1
##  [3,] -0.1 -0.1  0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  -0.1
##  [4,] -0.1 -0.1 -0.1  0.9 -0.1 -0.1 -0.1 -0.1 -0.1  -0.1
##  [5,] -0.1 -0.1 -0.1 -0.1  0.9 -0.1 -0.1 -0.1 -0.1  -0.1
##  [6,] -0.1 -0.1 -0.1 -0.1 -0.1  0.9 -0.1 -0.1 -0.1  -0.1
##  [7,] -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  0.9 -0.1 -0.1  -0.1
##  [8,] -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  0.9 -0.1  -0.1
##  [9,] -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1  0.9  -0.1
## [10,] -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1   0.9
J%>%knitr::kable()
0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
-0.1 0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
-0.1 -0.1 0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
-0.1 -0.1 -0.1 0.9 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
-0.1 -0.1 -0.1 -0.1 0.9 -0.1 -0.1 -0.1 -0.1 -0.1
-0.1 -0.1 -0.1 -0.1 -0.1 0.9 -0.1 -0.1 -0.1 -0.1
-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 0.9 -0.1 -0.1 -0.1
-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 0.9 -0.1 -0.1
-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 0.9 -0.1
-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 0.9