# 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 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: 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: Equivalently one can write above equations in matrix form as shown below:  # Using 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)

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

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

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

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

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

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.