# 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,ASingle 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.

Syntax:

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

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

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

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

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

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

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

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

**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