📜  Moore - R 编程中的彭罗斯伪逆

📅  最后修改于: 2022-05-13 01:55:14.570000             🧑  作者: Mango

Moore - R 编程中的彭罗斯伪逆

用于推广线性方程解的概念称为矩阵的摩尔-彭罗斯伪逆。 Moore – Penrose 逆是最广为人知的矩阵伪逆类型。在线性代数中伪逆A^{+} 矩阵A是逆矩阵的推广。伪逆的最常见用途是计算缺乏唯一解的线性方程组的最佳拟合解。术语广义逆有时用作伪逆的同义词。 R 语言提供了一种非常简单的摩尔计算方法——彭罗斯伪逆。伪逆使用如下:

x = A^{+} b

R中的实现

R 提供了两个函数ginv()可用于MASS库和pinv()可用于pracma库以计算矩阵的 Moore-Penrose 广义逆。这两个函数使用 gaussianElimination 返回矩阵的任意广义逆。

示例 1:
考虑以下 3 个线性方程:

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

等效地,可以将上述方程写成矩阵形式,如下所示:

A{x} = {b}\\

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

# 使用 ginv()

Python3
# 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)


Python3
# 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)


Python3
# 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)


Python3
# 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)


输出:

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

# 使用 pinv()

Python3

# 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)

输出:

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

示例 2:
同样,让我们有矩阵形式的线性方程,如下所示:

\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}

# 使用 ginv()

Python3

# 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)

输出:

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

# 使用 pinv()

Python3

# 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)

输出:

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