Moore – Penrose Pseudoinverse in R Programming

The concept used to generalize the solution of a linear equation is known as Moore – Penrose Pseudoinverse of a matrix. Moore – Penrose inverse is the most widely known type of matrix pseudoinverse. In linear algebra pseudoinverse A^{+} of a matrix A is a generalization of the inverse matrix. The most common use of pseudoinverse is to compute the best fit solution to a system of linear equations that lacks a unique solution. The term generalized inverse is sometimes used as a synonym of pseudoinverse. R Language provides a very simple method to calculate Moore – Penrose Pseudoinverse. The pseudoinverse is used as follows:

x = A^{+} b

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.

Implementation in R

R provides two functions ginv() which is available in MASS library and pinv() which is available in pracma library to compute the Moore-Penrose generalized inverse of a matrix. These two functions return an arbitrary generalized inverse of a matrix, using gaussianElimination.

Synatx:
ginv(A)
pinv(A)



Parameter:
A: numerical matrix

Example 1:
Consider below 3 linear equations:

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

Equivalently one can write above equations in matrix form as shown below:

 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}

# Using ginv()

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

# Using pinv()

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:
Similarly, let we have linear equations in matrix form as shown below:

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

# Using ginv()

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

# Using pinv()

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.