O conceito usado para generalizar a solução de uma equação linear é conhecido como Moore - Penrose Pseudoinverso de uma array. O inverso de Moore - Penrose é o tipo mais conhecido de pseudoinverso de array. Na álgebra linear, o pseudoinverso A ^ {+}de uma array A é uma generalização da array inversa. O uso mais comum de pseudoinverso é calcular a solução de melhor ajuste para um sistema de equações lineares que carece de uma solução única. O termo inverso generalizado às vezes é usado como sinônimo de pseudoinverso. A linguagem R fornece um método muito simples para calcular o Pseudoinverso Moore - Penrose. O pseudoinverso é usado da seguinte maneira:

x = A ^ {+} b

onde,
A + : decomposição de valor único usado para calcular o pseudoinverso ou o inverso generalizado de uma array numérica
x e b: vetores

Nota: O pseudoinverso Moore - Penrose resolve o problema no sentido de erro de menor quadrado. Em geral, não existe uma solução exata para problemas sobredeterminados. Portanto, se você verificar a solução, não obterá o b exato exigido, mas um valor aproximado de b.

Implementação em R

R fornece duas funções ginv()que estão disponíveis na biblioteca MASS e pinv()que estão disponíveis na biblioteca pracma para calcular o inverso generalizado de Moore-Penrose de uma array. Essas duas funções retornam uma inversa generalizada arbitrária de uma array, usando gaussianElimination.

Sintaxe:
ginv (A)
pinv (A)



Parâmetro:
A: array numérica

Exemplo 1:
Considere as 3 equações lineares abaixo:

 x_1 + 3x_2 = 17 \\ 5x_1 + 7x_2 = 19 \\ 11x_1 + 13x_2 = 23

Da mesma forma, pode-se escrever as equações acima em forma de array, conforme mostrado abaixo:

 A {x} = {b} \\
 \ begin {bmatrix} 1 e 3 \\ 5 & 7 \\ 11 & 13 \\ \ end {bmatrix}% \ begin {bmatrix} x_1 \\ x_2 \\ \ end {bmatrix} = \ begin {bmatrix} 17 \ \ 19 \\ 23 \\ \ end {bmatrix}

# Usando ginv()

# R program to illustrate 
# solve a linear matrix
# equation of metrics using 
# moore – Penrose Pseudoinverse
  
# Importing library for 
# applying pseudoinverse 
library(MASS) 
  
# Representing A in 
# matrics form in R
A = matrix( 
  c(1, 5, 11, 3, 7, 13), 
  nrow = 3,             
  ncol = 2,             
) 
cat("A = :\n") 
print(A) 
  
# Representing b in 
# matrics form in R
b = matrix( 
  c(17, 19, 23), 
  nrow = 3,             
  ncol = 1,             
) 
cat("b = :\n") 
print(b) 
  
# Calculating x using ginv()
cat("Solution of linear equations 
    using pseudoinverse:\n") 
x = ginv(A) %*% b
print(x)

Saída:

A = :
     [, 1] [, 2]
[1, ]    1    3
[2, ]    5    7
[3, ]   11   13
b = :
     [, 1]
[1, ]   17
[2, ]   19
[3, ]   23
Solution of linear equations 
    using pseudoinverse:
          [, 1]
[1, ] -7.513158
[2, ]  8.118421

# Usando pinv()

# R program to illustrate 
# solve a linear matrix
# equation of metrics using 
# moore – Penrose Pseudoinverse
  
# Importing library for 
# applying pseudoinverse 
library(pracma)
  
# Representing A in 
# matrics form in R
A = matrix( 
  c(1, 5, 11, 3, 7, 13), 
  nrow = 3,          
  ncol = 2,          
) 
cat("A = :\n") 
print(A) 
  
# Representing b in 
# matrics form in R
b = matrix( 
  c(17, 19, 23), 
  nrow = 3,          
  ncol = 1,          
) 
cat("b = :\n") 
print(b) 
  
# Calculating x using pinv()
cat("Solution of linear equations 
    using pseudoinverse:\n") 
x = pinv(A) %*% b
print(x)

Saída:

A = :
     [, 1] [, 2]
[1, ]    1    3
[2, ]    5    7
[3, ]   11   13
b = :
     [, 1]
[1, ]   17
[2, ]   19
[3, ]   23
Solution of linear equations 
    using pseudoinverse:
          [, 1]
[1, ] -7.513158
[2, ]  8.118421

Exemplo 2:
Da mesma forma, vamos ter equações lineares em forma de array, conforme mostrado abaixo:

 \ begin {bmatrix} 1 & 2 & 3 \\ 0 & 0 & 1 \\ \ end {bmatrix}% \ begin {bmatrix} x_1 \\ x_2 \\ x_3 \\ \ end {bmatrix} = \ begin {bmatrix} 2 \\ 1 \\ \ end {bmatrix}

# Usando ginv()

# R program to illustrate 
# solve a linear matrix
# equation of metrics using 
# moore – Penrose Pseudoinverse
  
# Importing library for 
# applying pseudoinverse 
library(MASS) 
  
# Representing A in 
# matrics form in R
A = matrix( 
  c(1, 0, 2, 0, 3, 1), 
  ncol = 3,
  byrow = F
) 
cat("A = :\n") 
print(A) 
  
# Representing b in 
# matrics form in R
b = matrix( 
  c(2, 1), 
) 
cat("b = :\n") 
print(b) 
  
# Calculating x using ginv()
cat("Solution of linear equations 
    using pseudoinverse:\n") 
x = ginv(A) %*% b
print(x)

Saída:

A = :
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    0    0    1
b = :
     [, 1]
[1, ]    2
[2, ]    1
Solution of linear equations 
    using pseudoinverse:
     [, 1]
[1, ] -0.2
[2, ] -0.4
[3, ]  1.0

# Usando pinv()

# R program to illustrate 
# solve a linear matrix
# equation of metrics using 
# moore – Penrose Pseudoinverse
  
# Importing library for 
# applying pseudoinverse 
library(pracma) 
  
# Representing A in 
# matrics form in R
A = matrix( 
  c(1, 0, 2, 0, 3, 1), 
  ncol = 3,
  byrow = F
) 
cat("A = :\n") 
print(A) 
  
# Representing b in 
# matrics form in R
b = matrix( 
  c(2, 1), 
) 
cat("b = :\n") 
print(b) 
  
# Calculating x using pinv()
cat("Solution of linear equations 
    using pseudoinverse:\n") 
x = pinv(A) %*% b
print(x)

Saída:

A = :
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    0    0    1
b = :
     [, 1]
[1, ]    2
[2, ]    1
Solution of linear equations 
    using pseudoinverse:
     [, 1]
[1, ] -0.2
[2, ] -0.4
[3, ]  1.0