= function(arr){
array_to_LaTeX = apply(arr, MARGIN=1, paste, collapse = " & ")
rows <- paste(rows, collapse = " \\\\ ")
matrix_string return(paste("\\begin{bmatrix}", matrix_string, "\\end{bmatrix}"))
}
Esempio di fattorizzazione LU
Appunti di calcolo numerico
Il metodo di eliminazione di Gauss, applicato ad un sistema lineare della forma:
\left\{ \begin{matrix} x_2 + 3 x_3 + 4 x_4 &= 6 \\ 2x_1 + 2x_2 + 4 x_4 &= 10 \\ x_2 + 3 x_3 + x_4 &= 3 \\ x_1 + 3 x_3 - x_4 &= 0 \\ \end{matrix} \right.
riscritto nella forma \bm{A}\bm{x} = \bm{b}
\bm{A}= \begin{pmatrix} 0 & 1 & 3 & 4 \\ 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 1 \\ 1 & 0 & 3 & -1 \end{pmatrix}, \quad \bm{x} = \begin{pmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \end{pmatrix}, \quad \bm{b} = \begin{pmatrix} 6 \\ 10 \\ 3 \\ 0 \end{pmatrix},
Alloco il sistema lineare in R
# R program to create a matrix
= matrix(
A # Taking sequence of elements
c(0, 1, 3, 4,
2, 2, 0, 4,
0, 1, 3, 1,
1, 0, 3, -1),
nrow = 4, # No of rows
ncol = 4, # No of columns
byrow = TRUE # arrange elements in row-wise order
)= matrix( c(6, 10, 3, 0), nrow = 4, ncol = 1, byrow = TRUE )
b = matrix( c(1,2,3,4), nrow = 4, ncol = 1, byrow = TRUE )
P
cat("The 4x4 matrix:\n")
The 4x4 matrix:
print(A)
[,1] [,2] [,3] [,4]
[1,] 0 1 3 4
[2,] 2 2 0 4
[3,] 0 1 3 1
[4,] 1 0 3 -1
conosco gia la soluzione, controllo il residuo:
= matrix( c(1, 2, 0, 1), nrow = 4, ncol = 1, byrow = TRUE )
xsol rownames(xsol) = c("x_1", "x_2", "x_3", "x_4")
= b - A %*% xsol;
res
print(res)
[,1]
[1,] 0
[2,] 0
[3,] 0
[4,] 0
Inizio l’algoritmo di Gauss
Step 1 scambio righe
= matrix(
S1 c(0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1),
nrow=4,ncol=4,byrow=TRUE
)= S1 %*% A;
A1 = S1 %*% P;
P1 print(A1)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 1 3 1
[4,] 1 0 3 -1
print(P1)
[,1]
[1,] 2
[2,] 1
[3,] 3
[4,] 4
\bm{S}_1\bm{A} = \begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 1 & 3 & 1 \\ 1 & 0 & 3 & -1 \end{bmatrix}
Faccio lo stesso lavoro sulla tabella TABLE
= A
TABLE = S1 %*% TABLE;
TABLE print(TABLE)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 1 3 1
[4,] 1 0 3 -1
Step 1b applica eliminazione
= matrix( c(0, 0, 0, 1/2), nrow=4,ncol=1, byrow=TRUE )
v1 = matrix( c(1, 0, 0, 0), nrow=1,ncol=4, byrow=TRUE )
e1T = matrix(
ID c(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1),
nrow=4,ncol=4,byrow=TRUE
)= ID - v1 %*% e1T;
L1 = L1 %*% A1;
A1b print(L1)
[,1] [,2] [,3] [,4]
[1,] 1.0 0 0 0
[2,] 0.0 1 0 0
[3,] 0.0 0 1 0
[4,] -0.5 0 0 1
print(A1b)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 1 3 1
[4,] 0 -1 3 -3
\bm{A}_1=\bm{L}_1\bm{S}_1\bm{A} = \begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 1 & 3 & 1 \\ 0 & -1 & 3 & -3 \end{bmatrix}
Applico eliminazione sulla tabella TABLE
2:4,1] = TABLE[2:4,1] / TABLE[1,1]
TABLE[2,2:4] = TABLE[2,2:4]-TABLE[2,1]*TABLE[1,2:4]
TABLE[3,2:4] = TABLE[3,2:4]-TABLE[3,1]*TABLE[1,2:4]
TABLE[4,2:4] = TABLE[4,2:4]-TABLE[4,1]*TABLE[1,2:4]
TABLE[print(TABLE)
[,1] [,2] [,3] [,4]
[1,] 2.0 2 0 4
[2,] 0.0 1 3 4
[3,] 0.0 1 3 1
[4,] 0.5 -1 3 -3
Step 2 nessuno scambio
L’elemento A_{2,2}\neq 0 quindi non sevre newssuno scambio di righe.
= ID
S2 = S2 %*% P1;
P2 = S2 %*% A1b;
A2 print(P2)
[,1]
[1,] 2
[2,] 1
[3,] 3
[4,] 4
Step 2b applica eliminazione
= matrix( c(0, 0, 1, -1), nrow=4,ncol=1, byrow=TRUE )
v2 = matrix( c(0, 1, 0, 0), nrow=1,ncol=4, byrow=TRUE )
e2T = ID - v2 %*% e2T;
L2 = L2 %*% A2;
A2b print(L2)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 -1 1 0
[4,] 0 1 0 1
print(A2b)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 0 0 -3
[4,] 0 0 6 1
\bm{A}_2=\bm{L}_2\bm{S}_2\bm{L}_1\bm{S}_1\bm{A} = \begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 0 & -3 \\ 0 & 0 & 6 & 1 \end{bmatrix}
Applico eliminazione sulla tabella TABLE
# Attenzione con byrow=TRUE indici riga e colonna sono scambiati
3:4,2] = TABLE[3:4,2] / TABLE[2,2]
TABLE[3,3:4] = TABLE[3,3:4]-TABLE[3,2]*TABLE[2,3:4]
TABLE[4,3:4] = TABLE[4,3:4]-TABLE[4,2]*TABLE[2,3:4]
TABLE[print(TABLE)
[,1] [,2] [,3] [,4]
[1,] 2.0 2 0 4
[2,] 0.0 1 3 4
[3,] 0.0 1 0 -3
[4,] 0.5 -1 6 1
Step 3 scambio righe
= matrix(
S3 c(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1,
0, 0, 1, 0),
nrow=4,ncol=4,byrow=TRUE
)= S3 %*% A2b;
A3 = S3 %*% P2;
P3 print(A3)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 0 6 1
[4,] 0 0 0 -3
print(P3)
[,1]
[1,] 2
[2,] 1
[3,] 4
[4,] 3
\bm{A}_3=\bm{S}_3\bm{L}_2\bm{S}_2\bm{L}_1\bm{S}_1\bm{A} = \begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 6 & 1 \\ 0 & 0 & 0 & -3 \end{bmatrix}
Applico scambio sulla tabella TABLE
= S3 %*% TABLE
TABLE print(TABLE)
[,1] [,2] [,3] [,4]
[1,] 2.0 2 0 4
[2,] 0.0 1 3 4
[3,] 0.5 -1 6 1
[4,] 0.0 1 0 -3
Step 3b eliminazione
Matrice hia in forma triangolare \bm{L}_3 = \bm{I} non serve fare niente.
= ID;
L3 = L3 %*% A3;
A3b print(L3)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
\bm{A}_3=\bm{L}_3\bm{S}_3\bm{L}_2\bm{S}_2\bm{L}_1\bm{S}_1\bm{A} = \begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 6 & 1 \\ 0 & 0 & 0 & -3 \end{bmatrix}
Applico eliminazione sulla tabella TABLE
4,3] = TABLE[4,3] / TABLE[3,3]
TABLE[4,4] = TABLE[4,4]-TABLE[4,3]*TABLE[3,4]
TABLE[print(TABLE)
[,1] [,2] [,3] [,4]
[1,] 2.0 2 0 4
[2,] 0.0 1 3 4
[3,] 0.5 -1 6 1
[4,] 0.0 1 0 -3
Abbiamo quindi ottenuto
\begin{aligned}&\underbrace{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}_{\bm{L}_3}\underbrace{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}}_{\bm{S}_3}\underbrace{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & -1 & 1 & 0 \\ 0 & 1 & 0 & 1 \end{bmatrix}}_{\bm{L}_2}\underbrace{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}_{\bm{S}_2}\\&\underbrace{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ -0.5 & 0 & 0 & 1 \end{bmatrix}}_{\bm{L}_1}\underbrace{\begin{bmatrix} 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}_{\bm{S}_1}\underbrace{\begin{bmatrix} 0 & 1 & 3 & 4 \\ 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 1 \\ 1 & 0 & 3 & -1 \end{bmatrix}}_{\bm{A}}=\underbrace{\begin{bmatrix} 2 & 2 & 0 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 6 & 1 \\ 0 & 0 & 0 & -3 \end{bmatrix}}_{\bm{A}_3}\end{aligned}
= TABLE;
LL upper.tri(TABLE)] = 0;
LL[diag(LL) = 1;
= TABLE;
UU lower.tri(TABLE)] = 0;
UU[print(LL)
[,1] [,2] [,3] [,4]
[1,] 1.0 0 0 0
[2,] 0.0 1 0 0
[3,] 0.5 -1 1 0
[4,] 0.0 1 0 1
print(UU)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 0 0 6 1
[4,] 0 0 0 -3
print(LL %*% UU)
[,1] [,2] [,3] [,4]
[1,] 2 2 0 4
[2,] 0 1 3 4
[3,] 1 0 3 -1
[4,] 0 1 3 1