Moore - R 编程中的彭罗斯伪逆
用于推广线性方程解的概念称为矩阵的摩尔-彭罗斯伪逆。 Moore – Penrose 逆是最广为人知的矩阵伪逆类型。在线性代数中伪逆矩阵A是逆矩阵的推广。伪逆的最常见用途是计算缺乏唯一解的线性方程组的最佳拟合解。术语广义逆有时用作伪逆的同义词。 R 语言提供了一种非常简单的摩尔计算方法——彭罗斯伪逆。伪逆使用如下:
where,
A+: Single value decomposition used to calculate the pseudoinverse or the generalized inverse of a numerical matrix
x and b: vectors
Note: Moore – Penrose pseudoinverse solves the problem in the least squared error sense. In general, there is no exact solution to overdetermined problems. So if you cross check the solution you will not get the exact required b but an approx value of b.
R中的实现
R 提供了两个函数ginv()可用于MASS库和pinv()可用于pracma库以计算矩阵的 Moore-Penrose 广义逆。这两个函数使用 gaussianElimination 返回矩阵的任意广义逆。
Syntax:
ginv(A)
pinv(A)
Parameter:
A: numerical matrix
示例 1:
考虑以下 3 个线性方程:
等效地,可以将上述方程写成矩阵形式,如下所示:
# 使用 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:
同样,让我们有矩阵形式的线性方程,如下所示:
# 使用 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