require(matrixcalc)
require(Matrix)
require(ibd)
require(pracma)
require(matlib)
require(MASS)
A matriz inversa generalizada é muito útil em situações em que a matriz original não é invertível, e esta matriz deve satisfazer a propriedade:
\[ \boldsymbol{A} \boldsymbol{A^-}\boldsymbol{A}=\boldsymbol{A} \]
Para uma matriz \(\boldsymbol{A}\) não quadrada ou singular, pode haver muitas matrizes \(\boldsymbol{A}^-\) que satisfazem esta propriedade.
A inversa de Moore-Penrose de uma matriz \(\boldsymbol{A}\) é comumente denotada como \(\boldsymbol{A^+}\). Esta matriz foi introduzida pela primeira vez por E. H. Moore em 1920, e mais tarde, a ideia foi generalizada por Roger Penrose em 1955, satisfazendo quatro condições:
\[ \begin{array}{l} I) \boldsymbol{A} \boldsymbol{A^+} \boldsymbol{A} = \boldsymbol{A}\\ II) \boldsymbol{A^+} \boldsymbol{A} \boldsymbol{A^+} = \boldsymbol{A^+}\\ III) \left(\boldsymbol{A} \boldsymbol{A^+}\right)^t = \boldsymbol{A} \boldsymbol{A^+} (\boldsymbol{A} \boldsymbol{A^+} \mbox{é simétrica})\\ IV) \left(\boldsymbol{A^+} \boldsymbol{A}\right)^t = \boldsymbol{A^+} \boldsymbol{A} (\boldsymbol{A^+} \boldsymbol{A} \mbox{ é simétrica})\\ \end{array} \]
Exemplo 1.1: (De Luna & De Olinda, 2015) Seja a matriz \[ \boldsymbol{X}=\begin{pmatrix} 1 & 1 & 0\\ 1 & 1 & 0 \\ 1 & 0 & 1\\ 1 & 0 & 1 \end{pmatrix} \Rightarrow \boldsymbol{X^-}= \begin{pmatrix} 0 & 0 & 1 & 0 \\ 1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix} \mbox{ e } \boldsymbol{X^+}= \begin{pmatrix} 1/6 & 1/6 & 1/6 & 1/6\\ 1/3 & 1/3 & -1/6 & -1/6\\ -1/6 & -1/6 & 1/3 & 1/3 \end{pmatrix} \]
X=matrix(c(1,1,0,1,1,0,1,0,1,1,0,1),byrow=T,nrow=4)
try(solve(X))
## Error in solve.default(X) : 'a' (4 x 3) deve ser quadrada
X_menos=fractions(Ginv(X))
all.equal(X %*% X_menos %*% X , X)
## [1] TRUE
X_mais=fractions(ginv(X))
all.equal(X%*%X_mais%*%X,X)
## [1] TRUE
all.equal(fractions(X_mais%*%X %*%X_mais),X_mais)
## [1] TRUE
all.equal(t(X %*% X_mais) , X %*% X_mais)
## [1] TRUE
all.equal(t(X_mais%*%X), X_mais%*%X)
## [1] TRUE
Sua forma analítica é através de fatoração de matrizes. Seja \(\boldsymbol{A}_{m \times n}\) de posto igual a \(k\). Então podemos escrever: \[ \boldsymbol{A}_{m \times n}=\boldsymbol{B}_{m \times k}.\boldsymbol{C}_{k \times n}, \]
com posto coluna e posto linha completos, respectivamente. E a fórmula da inversa vem: \[ \boldsymbol{A}^+ = \boldsymbol{C}^t(\boldsymbol{CC}^t)^{−1}(\boldsymbol{B}^t.\boldsymbol{B})^{−1}B^t \]
Exemplo 1.2: Vamos utilizar a decomposição por valores singulares (svd)
A=matrix(c(5,6,11,14,78,20,24,30,36,38),byrow=T,ncol=2)
Rank(A)
## [1] 2
svd(A)
## $d
## [1] 100.74283 30.47756
##
## $u
## [,1] [,2]
## [1,] -0.0718395 -0.09633979
## [2,] -0.1617887 -0.23509976
## [3,] -0.7764540 0.62710432
## [4,] -0.3504422 -0.49715933
## [5,] -0.4929229 -0.54315699
##
## $v
## [,1] [,2]
## [1,] -0.8820292 0.4711948
## [2,] -0.4711948 -0.8820292
Lambda=diag(svd(A)$d[1:Rank(A)]) #pegar apenas as duas primeiras observações
U=svd(A)$u[,1:Rank(A)] #pegar apenas as duas primeiras colunas
V=svd(A)$v[,1:Rank(A)]
B=(U %*% Lambda)
C=t(V)
B
## [,1] [,2]
## [1,] -7.237315 -2.936201
## [2,] -16.299048 -7.165266
## [3,] -78.222172 19.112607
## [4,] -35.304543 -15.152202
## [5,] -49.658452 -16.554098
Rank(B)
## [1] 2
C
## [,1] [,2]
## [1,] -0.8820292 -0.4711948
## [2,] 0.4711948 -0.8820292
Rank(C)
## [1] 2
B %*% C
## [,1] [,2]
## [1,] 5 6
## [2,] 11 14
## [3,] 78 20
## [4,] 24 30
## [5,] 36 38
all.equal(B %*% C,A)
## [1] TRUE
t(C)%*%solve(C%*%t(C))%*%solve(t(B)%*%B)%*%t(B)
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.0008604771 -0.002218232 0.01649333 -0.004618064 -0.00408175
## [2,] 0.0031241090 0.007560573 -0.01451694 0.016027023 0.01802462
ginv(A)
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.0008604771 -0.002218232 0.01649333 -0.004618064 -0.00408175
## [2,] 0.0031241090 0.007560573 -0.01451694 0.016027023 0.01802462
Significa que existe ao menos uma solução para o sistema, e temos a seguinte igualdade: \[ \boldsymbol{A A}^{-}\boldsymbol{b}=\boldsymbol{b} \mbox{ onde} \boldsymbol{A}^{-}= \left\{ \begin{array}{l} \mbox{inversa de } \boldsymbol{A}=\boldsymbol{A}^{-1} \mbox{ quando existir, a solução é única}\\ \mbox{qualquer inversa generalizada de }\boldsymbol{A} \end{array} \right. \]
\[ \begin{array}{l} \left\{ \begin{array}{l} \mbox{ se o sistema é consistente então } \boldsymbol{x}^{\star}=\boldsymbol{ A}^{-}\boldsymbol{b} \mbox{ é uma solução deste sistema}\\ \mbox{ a recíproca é verdadeira!} \end{array} \right. \end{array} \]
A solução geral de um sistema consistente é composto pelos valores de x:
\[ \boldsymbol{x}=\boldsymbol{A^-}\boldsymbol{b}+(\boldsymbol{I}_n-\boldsymbol{A^-}.\boldsymbol{A}).\boldsymbol{h} \]
Exemplo 1.3: (Reinprecht, 2022) Seja o sistema de equações abaixo: \[ \begin{array}{l} \left\{ \begin{array}{l} x_1+x_2=1\\ x_2+x_3=3\\ 2x_1+x_2=0 \end{array} \right. \Rightarrow \underbrace{ \begin{pmatrix} 1 & 1 & 0\\ 0 & 1 & 1\\ 2 & 1 & 0 \end{pmatrix}}_{\boldsymbol{A}} \underbrace{ \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix}}_{\boldsymbol{x}} = \underbrace{ \begin{pmatrix} 1\\ 3\\ 0 \end{pmatrix}}_{\boldsymbol{b}}\\ \square \mbox{ Inversa: } \boldsymbol{A}^{-}=\boldsymbol{A}^{-1}=\begin{pmatrix} -1 & 0 & 1\\ 0 & 1 & 1\\ 2 & 1 & 0 \end{pmatrix} \mbox{ é única}\\ \square \mbox{ consistência:} \boldsymbol{A}\boldsymbol{A}^{-}\boldsymbol{b}=\boldsymbol{b} \mbox{ (verificado no R) }\\ \square \mbox{ solução (única): } \boldsymbol{x} =\boldsymbol{A}^{-1}.\boldsymbol{y}= \begin{pmatrix} -1 & 0 & 1\\ 0 & 1 & 1\\ 2 & 1 & 0 \end{pmatrix}.\begin{pmatrix} 1\\ 3\\ 0 \end{pmatrix}= \begin{pmatrix} -1\\ 2\\ 1 \end{pmatrix} \end{array} \]
A=matrix(c(1,1,0,0,1,1,2,1,0),byrow=T,nrow=3)
b=matrix(c(1,3,0),ncol=1)
showEqn(A, b) #matlib
## 1*x1 + 1*x2 + 0*x3 = 1
## 0*x1 + 1*x2 + 1*x3 = 3
## 2*x1 + 1*x2 + 0*x3 = 0
A_menos=solve(A)
all.equal(A %*% A_menos %*%b,b) #condição de consistência
## [1] TRUE
Solve(A, b, fractions=TRUE) #matlib
## x1 = -1
## x2 = 2
## x3 = 1
solve(A)%*%b
## [,1]
## [1,] -1
## [2,] 2
## [3,] 1
Exemplo 1.4: (Reinprecht, 2022) Seja o sistema de equações abaixo: \[ \begin{array}{l} \left\{ \begin{array}{l} x_1+2x_3=1\\ 3x_1+x_2+x_3=2\\ -x_2+3x_3=0 \end{array} \right. \Rightarrow \underbrace{ \begin{pmatrix} 1 & 0 & 2\\ 2 & 1 & 1\\ 0 & -1 & 3 \end{pmatrix}}_{\boldsymbol{A}} \underbrace{ \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix}}_{\boldsymbol{x}} = \underbrace{ \begin{pmatrix} 1\\ 2\\ 0 \end{pmatrix}}_{\boldsymbol{b}}\\ \square \mbox{ Inversa generalizada: } \boldsymbol{A}^{-}=\boldsymbol{A}^{+}=\begin{pmatrix} 2/21 & 13/42 & -5/42\\ 1/42 & 17/84 & -13/84\\ 5/42 & 1/84 & 19/84 \end{pmatrix} \\ \square \mbox{ consistência:} \boldsymbol{A}\boldsymbol{A}^{-}\boldsymbol{b}=\boldsymbol{b} \mbox{ (verificado no R) }\\ \square \mbox{ solução particular (uma das soluções): } \boldsymbol{x}^{\star} =\boldsymbol{A}^{-}.\boldsymbol{b}= \begin{pmatrix} 5/7 \\ 3/7 \\ 1/7 \end{pmatrix}\\ \square \mbox{ solução geral: } \boldsymbol{x} = \boldsymbol{x}^{\star}+(\boldsymbol{I}_3-\boldsymbol{A}^-.\boldsymbol{A}).\boldsymbol{h}\\ = \begin{pmatrix} 5/7 \\ 3/7 \\ 1/7 \end{pmatrix}+ \begin{pmatrix} 2/7 & -3/7 & -1/7\\ -3/7 & 9/14 & 3/14\\ -1/7 & 3/14 & 1/14 \end{pmatrix}.\begin{pmatrix} h_1\\ h_2\\ h_3 \end{pmatrix}\Rightarrow \left\{ \begin{array}{l} x_1=\frac{5}{7}+\frac{2}{7}.h_1-\frac{3}{7}.h_2-\frac{1}{7}.h_3\\ x_2=\frac{3}{7}-\frac{3}{7}.h_1+\frac{9}{14}.h_2+\frac{3}{14}.h_3\\ x_3=\frac{1}{7}-\frac{1}{7}.h_1+\frac{3}{14}.h_2+\frac{1}{14}.h_3 \end{array} \right.\\ \mbox{ com }h_1, h_2, h_3 \in \mathbb{R} \end{array} \]
A=matrix(c(1,0,2,2,1,1,0,-1,3),byrow=T,nrow=3)
b=matrix(c(1,2,0),ncol=1)
showEqn(A, b)
## 1*x1 + 0*x2 + 2*x3 = 1
## 2*x1 + 1*x2 + 1*x3 = 2
## 0*x1 - 1*x2 + 3*x3 = 0
try(solve(A))
## Error in solve.default(A) :
## Lapack routine dgesv: system is exactly singular: U[3,3] = 0
A_menos=fractions(ginv(A))
all.equal(A %*% A_menos %*%b,b)
## [1] TRUE
x_star=fractions(ginv(A) %*% b)
x_star
## [,1]
## [1,] 5/7
## [2,] 3/7
## [3,] 1/7
aux=diag(3)-A_menos%*%A
fractions(aux)
## [,1] [,2] [,3]
## [1,] 2/7 -3/7 -1/7
## [2,] -3/7 9/14 3/14
## [3,] -1/7 3/14 1/14
sols=function(h1,h2,h3)
{
h=matrix(c(h1,h2,h3),ncol=1)
p1=A_menos%*%b
p2=diag(3)-A_menos%*%A
p3=p2%*%h
aux=p1+p3
return(aux)
}
sol1=fractions(sols(0,0,0))
sol1
## [,1]
## [1,] 5/7
## [2,] 3/7
## [3,] 1/7
all.equal(A%*%sol1,b)
## [1] TRUE
sol2=fractions(sols(1,1,1))
sol2
## [,1]
## [1,] 3/7
## [2,] 6/7
## [3,] 2/7
all.equal(A%*%sol2,b)
## [1] TRUE
sol3=fractions(sols(-2,5,3))
sol3
## [,1]
## [1,] -17/7
## [2,] 36/7
## [3,] 12/7
all.equal(A%*%sol3,b)
## [1] TRUE
Exemplo 1.5: (Reinprecht, 2022) Seja o sistema de equações abaixo: \[ \begin{array}{l} \left\{ \begin{array}{l} x_1+2x_2+x_3=3\\ x_1+x_2=2\\ x_2+x_3=0 \end{array} \right. \Rightarrow \underbrace{ \begin{pmatrix} 1 & 2 & 1\\ 1 & 1 & 0\\ 0 & 1 & 1 \end{pmatrix}}_{\boldsymbol{A}} \underbrace{ \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix}}_{\boldsymbol{x}} = \underbrace{ \begin{pmatrix} 1\\ 2\\ 0 \end{pmatrix}}_{\boldsymbol{b}}\\ \square \mbox{ Inversa Generalizada: } \boldsymbol{A}^{-}=\begin{pmatrix} 1/9 & 5/9 & -4/9\\ 2/9 & 1/9 & 1/9\\ 1/9 & -4/9 & 5/9 \end{pmatrix} \\ \square \mbox{ consistência (não há):} \boldsymbol{A}\boldsymbol{A}^{-}\boldsymbol{b}\ne \boldsymbol{b} \mbox{ (verificado no R) }\\ \square \mbox{ solução: não há} \end{array} \]
A=matrix(c(1,2,1,1,1,0,0,1,1),byrow=T,nrow=3)
b=matrix(c(3,2,0),ncol=1)
showEqn(A, b)
## 1*x1 + 2*x2 + 1*x3 = 3
## 1*x1 + 1*x2 + 0*x3 = 2
## 0*x1 + 1*x2 + 1*x3 = 0
try(solve(A))
## Error in solve.default(A) :
## Lapack routine dgesv: system is exactly singular: U[3,3] = 0
A_menos=fractions(ginv(A))
A_menos
## [,1] [,2] [,3]
## [1,] 1/9 5/9 -4/9
## [2,] 2/9 1/9 1/9
## [3,] 1/9 -4/9 5/9
all.equal(A %*% A_menos %*%b,b)
## [1] "Mean relative difference: 0.1875"
Exemplo 1.6 Seja o sistema de equações abaixo: \[ \begin{array}{l} \left\{ \begin{array}{l} x_1+x_2=3\\ x_1-x_2=1\\ -2x_1 = -4 \end{array} \right. \Rightarrow \underbrace{ \begin{pmatrix} 1 & 1 \\ 1 & -1 \\ -2 & 0 \end{pmatrix}}_{\boldsymbol{A}} \underbrace{ \begin{pmatrix} x_1\\ x_2 \end{pmatrix}}_{\boldsymbol{x}} = \underbrace{ \begin{pmatrix} 3\\ 1\\ -4 \end{pmatrix}}_{\boldsymbol{b}}\\ \square \mbox{ Inversa generalizada: } \boldsymbol{A}^{-}=\boldsymbol{A}^{+}=\begin{pmatrix} 1/6 & 1/6 & -1/3\\ 1/2 & -1/2 & 0 \end{pmatrix} \\ \square \mbox{ consistência:} \boldsymbol{A}\boldsymbol{A}^{-}\boldsymbol{b}=\boldsymbol{b} \mbox{ (verificado no R) }\\ \square \mbox{ solução particular (uma das soluções): } \boldsymbol{x}^{\star} =\boldsymbol{A}^{-}.\boldsymbol{b}= \begin{pmatrix} 2 \\ 1 \end{pmatrix}\\ \square \mbox{ solução geral: } \boldsymbol{x} = \boldsymbol{x}^{\star}+(\boldsymbol{I}_3-\boldsymbol{A}^-.\boldsymbol{A}).\boldsymbol{h}\\ = \begin{pmatrix} 2 \\ 1 \end{pmatrix}+ \begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix}.\begin{pmatrix} h_1\\ h_2 \end{pmatrix}\Rightarrow \left\{ \begin{array}{l} x_1=2\\ x_2=1 \end{array} \right.\\ \mbox{ sumiram os termos }h_1 \mbox{ e } h_2 \Rightarrow \mbox{ a solução é única} \end{array} \]
A=matrix(c(1,1,1,-1,-2,0),byrow=T,nrow=3)
b=matrix(c(3,1,-4),ncol=1)
showEqn(A, b)
## 1*x1 + 1*x2 = 3
## 1*x1 - 1*x2 = 1
## -2*x1 + 0*x2 = -4
try(solve(A))
## Error in solve.default(A) : 'a' (3 x 2) deve ser quadrada
A_menos=fractions(ginv(A))
A_menos
## [,1] [,2] [,3]
## [1,] 1/6 1/6 -1/3
## [2,] 1/2 -1/2 0
all.equal(A %*% ginv(A) %*%b,b)
## [1] TRUE
x_star=fractions(ginv(A) %*% b)
x_star
## [,1]
## [1,] 2
## [2,] 1
fractions(diag(2)-A_menos%*%A)
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
Exemplo 1.7
A=matrix(c(4,2,2,2,2,0,2,0,2),byrow=T,nrow=3)
b=matrix(c(1,1,1),ncol=1)
showEqn(A, b)
## 4*x1 + 2*x2 + 2*x3 = 1
## 2*x1 + 2*x2 + 0*x3 = 1
## 2*x1 + 0*x2 + 2*x3 = 1
try(solve(A))
## Error in solve.default(A) :
## Lapack routine dgesv: system is exactly singular: U[3,3] = 0
A_menos=fractions(ginv(A))
A_menos
## [,1] [,2] [,3]
## [1,] 1/9 1/18 1/18
## [2,] 1/18 5/18 -2/9
## [3,] 1/18 -2/9 5/18
all.equal(A %*% A_menos %*%b,b)
## [1] "Mean relative difference: 0.375"
Exemplo 1.8
A=matrix(c(2,1,1,1),byrow=T,nrow=2)
b=matrix(c(7,5),ncol=1)
showEqn(A, b)
## 2*x1 + 1*x2 = 7
## 1*x1 + 1*x2 = 5
try(solve(A))
## [,1] [,2]
## [1,] 1 -1
## [2,] -1 2
all.equal(A %*% solve(A) %*%b,b)
## [1] TRUE
Solve(A, b, fractions=TRUE) #matlib
## x1 = 2
## x2 = 3
#solve(A)%*%b #forma equivalente
Exemplo 1.9
A=matrix(c(1,1,1,-1,-2,0),byrow=T,nrow=3)
b=matrix(c(3,1,2),ncol=1)
showEqn(A, b)
## 1*x1 + 1*x2 = 3
## 1*x1 - 1*x2 = 1
## -2*x1 + 0*x2 = 2
try(solve(A))
## Error in solve.default(A) : 'a' (3 x 2) deve ser quadrada
fractions(ginv(A))
## [,1] [,2] [,3]
## [1,] 1/6 1/6 -1/3
## [2,] 1/2 -1/2 0
all.equal(A %*% ginv(A) %*%b,b)
## [1] "Mean relative difference: 3"
Exemplo 1.10 (Fieller, 2015) p.125
Para as matrizes (e vetores) abaixo , \[ \boldsymbol{A_1}=\begin{pmatrix} 3 & 2 & 1 \\ 4 & 2 & 0 \\ 5 & 2 & -1 \\ -1 & 0 & 1 \end{pmatrix}, \boldsymbol{A_2}=\begin{pmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \end{pmatrix}, \boldsymbol{A_3}=\begin{pmatrix} 1 & 1 & 5 \\ 2 & 5 & 3 \\ 3 & 6 & 1 \\ 5 & 4 & 4 \end{pmatrix}, \boldsymbol{A_4}=\begin{pmatrix} 1 & 3 & 7 \end{pmatrix} \]
Resposta: \[ \begin{array}{l} a) \mbox{ Com o auxílio do R, temos que as formas escalonadas são:}\\ \boldsymbol{E_1}=\begin{pmatrix} 1 & 0 & -1 \\ 0 & 1 & 2 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix}, \boldsymbol{E_2}=\begin{pmatrix} 1 & 0 & -1 \\ 0 & 1 & 2 \end{pmatrix}, \boldsymbol{E_3}=\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \end{pmatrix},\\ \boldsymbol{E_4} \mbox{ não possui forma escalonada, é um vetor!} \end{array} \]
\[ \begin{array}{l} b) \mbox{ Olhando para as formas escalonadas e o cálculo do posto no R:}\\ \mbox{ posto }(\boldsymbol{A}_1)=2,\mbox{ posto }(\boldsymbol{A}_2)=2, \mbox{ posto }(\boldsymbol{A}_3)=3, \mbox{ posto }(\boldsymbol{A}_4)=1 \end{array} \]
\[ \begin{array}{l} c) \boldsymbol{A}_2 \mbox{ é de posto linha completo}\\ \boldsymbol{A}_3 \mbox{ é de posto coluna completo}\\ \end{array} \]
\[ \begin{array}{l} d) \boldsymbol{ A}_1: \left\{ \begin{array}{l} \mbox{a linha 4 é igual a subtração das linhas 1 e 2}\\ \mbox{ a linha 3 é igual a subtração das linhas 2 e 4} \end{array} \right.\\ \end{array} \]
\[ \begin{array}{l} e) \mbox{ Com o auxílio do R, temos que as inversas generalizadas são:}\\ \boldsymbol{M_1}=\begin{pmatrix} 0 & 0.0556 & 0.1111 & -0.0556\\ 0.1667 & 0.0556 & -0.0556 & 0.1111 \\ 0.3333 & 0.0556 & -0.2222 & 0.2778 \end{pmatrix}, \boldsymbol{M_2}=\begin{pmatrix} -1.3333 & 1.0833 \\ -0.3333 & 0.3333\\ 0.6667 & -0.4167 \end{pmatrix},\\ \boldsymbol{M_3}=\begin{pmatrix} -0.1336 & -0.1982 & -0.0276 & 0.3226\\ -0.0072 & 0.1541 & 0.1326 & -0.1398 \\ 0.1930 & 0.0640 & -0.0712 & -0.0215 \end{pmatrix}, \boldsymbol{M}_4=\begin{pmatrix} 0.0169 \\ 0.0508 \\ 0.1186 \end{pmatrix} \end{array} \]
A_1=matrix(c(3,2,1,4,2,0,5,2,-1,-1,0,1),nrow=4,byrow=T)
A_2=matrix(c(1,3,5,2,4,6),nrow=2,byrow=T)
A_3=matrix(c(1,1,5,2,5,3,3,6,1,5,4,4),nrow=4,byrow=T)
A_4=matrix(c(1,3,7),ncol=3)
f=function(matriz)
{
aux=list()
aux[[1]]=gaussianElimination(matriz,verbose=TRUE) #determina a forma escalonada da matriz
aux[[2]]=Rank(matriz) #acha o rank
return (aux)
}
f(A_1)
##
## Initial matrix:
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 4 2 0
## [3,] 5 2 -1
## [4,] -1 0 1
##
## row: 1
##
## exchange rows 1 and 3
## [,1] [,2] [,3]
## [1,] 5 2 -1
## [2,] 4 2 0
## [3,] 3 2 1
## [4,] -1 0 1
##
## multiply row 1 by 0.2
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 4 2.0 0.0
## [3,] 3 2.0 1.0
## [4,] -1 0.0 1.0
##
## multiply row 1 by 4 and subtract from row 2
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 0 0.4 0.8
## [3,] 3 2.0 1.0
## [4,] -1 0.0 1.0
##
## multiply row 1 by 3 and subtract from row 3
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 0 0.4 0.8
## [3,] 0 0.8 1.6
## [4,] -1 0.0 1.0
##
## multiply row 1 by 1 and add to row 4
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 0 0.4 0.8
## [3,] 0 0.8 1.6
## [4,] 0 0.4 0.8
##
## row: 2
##
## exchange rows 2 and 3
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 0 0.8 1.6
## [3,] 0 0.4 0.8
## [4,] 0 0.4 0.8
##
## multiply row 2 by 1.25
## [,1] [,2] [,3]
## [1,] 1 0.4 -0.2
## [2,] 0 1.0 2.0
## [3,] 0 0.4 0.8
## [4,] 0 0.4 0.8
##
## multiply row 2 by 0.4 and subtract from row 1
## [,1] [,2] [,3]
## [1,] 1 0.0 -1.0
## [2,] 0 1.0 2.0
## [3,] 0 0.4 0.8
## [4,] 0 0.4 0.8
##
## multiply row 2 by 0.4 and subtract from row 3
## [,1] [,2] [,3]
## [1,] 1 0.0 -1.0
## [2,] 0 1.0 2.0
## [3,] 0 0.0 0.0
## [4,] 0 0.4 0.8
##
## multiply row 2 by 0.4 and subtract from row 4
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 1 2
## [3,] 0 0 0
## [4,] 0 0 0
##
## row: 3
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 1 2
## [3,] 0 0 0
## [4,] 0 0 0
##
## [[2]]
## [1] 2
f(A_2)
##
## Initial matrix:
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## row: 1
##
## exchange rows 1 and 2
## [,1] [,2] [,3]
## [1,] 2 4 6
## [2,] 1 3 5
##
## multiply row 1 by 0.5
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 3 5
##
## subtract row 1 from row 2
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 1 2
##
## row: 2
##
## multiply row 2 by 2 and subtract from row 1
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 1 2
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 1 2
##
## [[2]]
## [1] 2
f(A_3)
##
## Initial matrix:
## [,1] [,2] [,3]
## [1,] 1 1 5
## [2,] 2 5 3
## [3,] 3 6 1
## [4,] 5 4 4
##
## row: 1
##
## exchange rows 1 and 4
## [,1] [,2] [,3]
## [1,] 5 4 4
## [2,] 2 5 3
## [3,] 3 6 1
## [4,] 1 1 5
##
## multiply row 1 by 0.2
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8
## [2,] 2 5.0 3.0
## [3,] 3 6.0 1.0
## [4,] 1 1.0 5.0
##
## multiply row 1 by 2 and subtract from row 2
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8
## [2,] 0 3.4 1.4
## [3,] 3 6.0 1.0
## [4,] 1 1.0 5.0
##
## multiply row 1 by 3 and subtract from row 3
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8
## [2,] 0 3.4 1.4
## [3,] 0 3.6 -1.4
## [4,] 1 1.0 5.0
##
## subtract row 1 from row 4
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8
## [2,] 0 3.4 1.4
## [3,] 0 3.6 -1.4
## [4,] 0 0.2 4.2
##
## row: 2
##
## exchange rows 2 and 3
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8
## [2,] 0 3.6 -1.4
## [3,] 0 3.4 1.4
## [4,] 0 0.2 4.2
##
## multiply row 2 by 0.2777778
## [,1] [,2] [,3]
## [1,] 1 0.8 0.8000000
## [2,] 0 1.0 -0.3888889
## [3,] 0 3.4 1.4000000
## [4,] 0 0.2 4.2000000
##
## multiply row 2 by 0.8 and subtract from row 1
## [,1] [,2] [,3]
## [1,] 1 0.0 1.1111111
## [2,] 0 1.0 -0.3888889
## [3,] 0 3.4 1.4000000
## [4,] 0 0.2 4.2000000
##
## multiply row 2 by 3.4 and subtract from row 3
## [,1] [,2] [,3]
## [1,] 1 0.0 1.1111111
## [2,] 0 1.0 -0.3888889
## [3,] 0 0.0 2.7222222
## [4,] 0 0.2 4.2000000
##
## multiply row 2 by 0.2 and subtract from row 4
## [,1] [,2] [,3]
## [1,] 1 0 1.1111111
## [2,] 0 1 -0.3888889
## [3,] 0 0 2.7222222
## [4,] 0 0 4.2777778
##
## row: 3
##
## exchange rows 3 and 4
## [,1] [,2] [,3]
## [1,] 1 0 1.1111111
## [2,] 0 1 -0.3888889
## [3,] 0 0 4.2777778
## [4,] 0 0 2.7222222
##
## multiply row 3 by 0.2337662
## [,1] [,2] [,3]
## [1,] 1 0 1.1111111
## [2,] 0 1 -0.3888889
## [3,] 0 0 1.0000000
## [4,] 0 0 2.7222222
##
## multiply row 3 by 1.111111 and subtract from row 1
## [,1] [,2] [,3]
## [1,] 1 0 0.0000000
## [2,] 0 1 -0.3888889
## [3,] 0 0 1.0000000
## [4,] 0 0 2.7222222
##
## multiply row 3 by 0.3888889 and add to row 2
## [,1] [,2] [,3]
## [1,] 1 0 0.000000
## [2,] 0 1 0.000000
## [3,] 0 0 1.000000
## [4,] 0 0 2.722222
##
## multiply row 3 by 2.722222 and subtract from row 4
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [4,] 0 0 0
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [4,] 0 0 0
##
## [[2]]
## [1] 3
f(A_4)
##
## Initial matrix:
## [,1] [,2] [,3]
## [1,] 1 3 7
##
## row: 1
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 3 7
##
## [[2]]
## [1] 1
g=function(matriz,inversa)
{
aux=list()
inversa=ginv(matriz)
aux[[1]]=fractions(round(inversa,4))
aux[[2]]=all.equal(round(matriz%*%inversa%*%matriz,0),matriz)
aux[[3]]=all.equal(round(inversa%*%matriz%*%inversa,4),round(inversa,4))
aux[[4]]=all.equal(round(matriz%*%inversa,4),round(t(matriz%*%inversa),4))
aux[[5]]=all.equal(round(inversa%*%matriz,4),round(t(inversa%*%matriz),4))
return(aux)
}
A matriz \(\boldsymbol{A}^+\) também pode ser expressa com seus elementos em forma de fração:
g(A_1)
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 0 139/2500 1111/10000 -139/2500
## [2,] 1667/10000 139/2500 -139/2500 1111/10000
## [3,] 1/3 139/2500 -1111/5000 1389/5000
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
g(A_2)
## [[1]]
## [,1] [,2]
## [1,] -4/3 10833/10000
## [2,] -1/3 1/3
## [3,] 2/3 -4167/10000
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
g(A_3)
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] -167/1250 -991/5000 -69/2500 1613/5000
## [2,] -9/1250 1541/10000 663/5000 -699/5000
## [3,] 193/1000 8/125 -89/1250 -43/2000
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
g(A_4)
## [[1]]
## [,1]
## [1,] 169/10000
## [2,] 127/2500
## [3,] 593/5000
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
[1] De Luna, J. G., & De Olinda, R. A. (2015). Introdução a Modelos Lineares. Livraria da Física, 1ª edição, 164 p.
[2] Fieller, N. (2016). Basics of Matrix Algebra for Statistics with R. (Chapman & Hall/CRC The R Series), 1ª edição, 244 p..
[3] Reinprecht, J.. (2022). A inversa generalizada de uma matriz e aplicações. Dissertação de mestrado da Universidade Estadual Júlio de Mesquita Filho, Rio Claro, SP.